Help with rename and unlink script please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dynamo

    Help with rename and unlink script please

    I am trying to upload a file and then rename the file as something else.
    Everything worked fine unless the renamed filename already existed. So I added
    some extra code to check if the filename already existed and if so to delete it
    before renaming the uploaded file. Everything now works fine in the following
    code:

    $uploaddir = 'images/';
    $uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
    echo '<pre>';
    if (move_uploaded_ file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    $imagefilename = $_POST['imagefilename'];
    $renamedfile = $uploaddir . $imagefilename;
    if (file_exists($r enamedfile)) {
    unlink($renamed file);
    }
    rename($uploadf ile , $renamedfile);
    }
    else {
    echo "There was an error in your file upload. \n";
    echo 'Here is some more debugging info:';
    print_r($_FILES );
    }

    That seems like a lot of code to do something relatively simple. So my question
    is, is there a simpler way like forcing the rename function to overwrite an
    existing file with the same filename if it exists.

    Regards
    Dynamo

  • Sjoerd

    #2
    Re: Help with rename and unlink script please

    Dynamo wrote:[color=blue]
    > I am trying to upload a file and then rename the file as something else.
    > Everything worked fine unless the renamed filename already existed.[/color]

    Are you sure? The documentation for move_uploaded_f ile says:[color=blue]
    > If the destination file already exists, it will be overwritten.[/color]

    You can do an unlink and then move the file, even if the destination
    does not exist:
    @unlink($destin ation);
    move_uploaded_f ile($source, $destination);

    The @ is there to avoid any errors if the $destination file does not
    exist.

    Comment

    • Dynamo

      #3
      Re: Help with rename and unlink script please

      Yes I'm sure. It is not move_uploaded_f ile that is causing the problem. It is
      the rename function that is causing the headache. To explain better, lets say
      I'm uploading a picture called mypicture.jpg. There is nothing in the uploaded
      filename that associates it with a record in a mysql table. So once the file has
      been uploaded I then rename it as 21.jpg by using
      rename('mypictu re.jpg','21.jpg ')
      I can now associate 21.jpg with a record in my table where the id=21
      The problem lies in the fact that at some point in time I might want to change
      the picture that is asscociated with this record. In that instance if I use
      rename('mynewpi cture.jpg','21. jpg')
      then 21.jpg already exists and I get a warning message that the file already
      exists and hence is not renamed.

      Hope that explains it more fully.
      Regards
      Dynamo

      In article <1148544647.197 533.326110@u72g 2000cwu.googleg roups.com>, Sjoerd
      says...[color=blue]
      >
      >Are you sure? The documentation for move_uploaded_f ile says:[color=green]
      >> If the destination file already exists, it will be overwritten.[/color][/color]

      Comment

      • Rik

        #4
        Re: Help with rename and unlink script please


        Dynamo wrote:[color=blue]
        > Yes I'm sure. It is not move_uploaded_f ile that is causing the problem. It is
        > the rename function that is causing the headache. To explain better, lets say
        > I'm uploading a picture called mypicture.jpg. There is nothing in the uploaded
        > filename that associates it with a record in a mysql table. So once the file has
        > been uploaded I then rename it as 21.jpg by using
        > rename('mypictu re.jpg','21.jpg ')[/color]

        Why store it with the actual filename and rename it afterwards? You'll
        have 2 files, one of which you aren't going to use... Or are you doing
        something else with it?

        $uploaddir = 'images/';
        if (!move_uploaded _file($_FILES['userfile']['tmp_name'], $uploaddir.
        $_POST['imagefilename'])) {
        echo "There was an error in your file upload. \n";
        echo 'Here is some more debugging info:';
        print_r($_FILES );
        }

        Comment

        • otrWalter@gmail.com

          #5
          Re: Help with rename and unlink script please


          Dynamo wrote:
          [color=blue]
          > That seems like a lot of code to do something relatively simple. So my question
          > is, is there a simpler way like forcing the rename function to overwrite an
          > existing file with the same filename if it exists.[/color]

          Yes, file upload can be a PITA, if not done right or well.

          After searching, digging through scripts, I finally sat down and made a
          class for myself to handle all this.

          A few friends tol me to shar eit, so I did...

          Download php-yacs [Yet Another Class System] for free. Series of PHP Classes (like PEAR, but with documentation). Curently have 4 classes under development, 3 others in planning.


          the DOWNLOAD has nothing behind it yet, as I can't figure out how to do
          that yet. But you go to CVS link, you can get the FILE directory (which
          contains the php files) from there.

          Hope it helps you.

          It renames, moves, copies files as you decide. defines a new name for
          an upload if you like. Will overwrite files, if you like, etc.

          contact tme if you have any questions (or can tell me how to get SF to
          have my set of files download via thier DOWNLAOD button.

          Walter

          Comment

          Working...