Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php overwrite.

Options
  • 05-09-2007 12:37pm
    #1
    Closed Accounts Posts: 2,175 ✭✭✭


    Hi. I have an upload script here, but I want it to overwrite the existing file when uploaded. No idea what the hell I'm doing and am pressed for time. Not yer problem, but I'd appreciate some help ;)
    <div id="main">
    
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" enctype="multipart/form-data">
            <h1>Image Slide to upload:</h1>
            <table>
              <tr><td><input name="upfile" type="file"></td></tr>
              <tr><td><input type="submit" name="submitBtn" value="Upload"></td></tr>
            </table>  
          </form>
    <?php    
        if (isset($_POST['submitBtn'])){
    
            // Define the upload location
            $target_path = "c:\\Inetpub\\wwwroot\\scroll\\img\\";
    
            // Create the file name with path
            $target_path = $target_path . basename( $_FILES['upfile']['name']); 
    
            // Try to move the file from the temporay directory to the defined.
            if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) {
                echo "The file ".  basename( $_FILES['upfile']['name']). 
                     " has been uploaded";
            } else{
                echo "There was an error uploading the file, please try again!";
            }
        }
    ?>
    
    </div>
    

    It's all done locally; there is no network connection.
    We're talking about some dude editing a file and uploading it to the folder where it is an external file that another page calls for it's content. When he wants to change it, he edits the file again and uploads it again. Problem is this script won't overwrite files.

    I know it's unusual to WANT to have php overwrite files, but in this case I need it :rolleyes:

    Any help guys. I know it's probably something simple and stupid, but I just don't have the knowledge or the time to figure it out.

    Thanks


Comments

  • Registered Users Posts: 3,594 ✭✭✭forbairt


    Could I suggest a rename of the existing file
    <?php
    rename("/myfile.txt", "/backup/my_file.txt");
    ?>
    
    Then if there have been any problems you've still got the backup ... and now you should be able to do what you were trying to do ?

    Its quick ... its got some measure of ... thank god we didn't delete the original copy ... but it isn't exactly what you're after ...


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    BTW have you got the correct permissions set up to overwrite files ? as this should happen through the move_uploaded_file function


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    I'll check this out, thanks.

    Permissions are correct (as far as I know), because before setting the folder permissions, nothing worked o_o


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    chamlis wrote:
    Permissions are correct (as far as I know), because before setting the folder permissions, nothing worked o_o

    Hmm... from the sounds of it you are able to just able to open existing files and not write to them ? ...

    or are you able to create new files as well ?


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    It's a powerpoint presentation. ppt or pps

    It opens in an iframe on a webpage. So it's always got to be called "powerpoint.ppt" or it won't work. But dude has to be able to change the content of the ppt and upload it to the folder it's called from by the webpage. Therefore, replacing the existing one with the new updated one whenever he wants. Not idiot-proof enough for him to browse to the actual file and edit it then save it in place.

    There are other elements on the webpage aswell, so they are all changed from a user interface incorporating this upload feature.

    It's a digital signage project for a client, but my Boss doesn't understand the words "no" or "it can't be done" :rolleyes:


  • Advertisement
Advertisement