简体   繁体   中英

PHP move_uploaded_file to directory and create file

I have a script that uploads a file and then moves it to a directory. However the script does not know the name of the file its creating because it hasn't created it yet and cannot find the file to update.

So either one requires a way to make the file first or there is another way of doing this. The code.

<?php

$filename = '/home/divethe1/public_html/update/z-images/admin/upload/test/';

if ($_FILES['thumbfile']['error'] === UPLOAD_ERR_OK) {
    $info = getimagesize($_FILES['thumbfile']['tmp_name']);
    if (($info[2] !== IMG_GIF) && ($info[2] !== IMG_JPEG)) {
       die("not a gif/jpg");
    }
    if (filesize($_FILES['thumbfile']['tmp_name']) > 100000) {
       die("larger than 100000");
    }
    move_uploaded_file($_FILES['thumbfile']['tmp_name'], $filename . $_FILES['thumbfile']['name']);

      echo '<script type="text/javascript">
parent.document.getElementById("thumbprogress").innerHTML = "Archiving"</script>Archiving';

  }
else
  {
  echo '<script type="text/javascript">
parent.document.getElementById("thumbprogress").innerHTML = "Invalid File Format"</script>Invalid File Format';
  }
?>

Any ideas?

I think you're misunderstanding how move_uploaded_file() works. It doesn't create a file for you. It:

  1. Takes the temporary filethat PHP created for you to hold the upload (the filename/path for which is in $_FILES['thumbfile']['tmp_name'] )
  2. does a few security checks to make sure no one's tampered with the file between the time the upload completed and the move_uploaded_file call was issued
  3. then MOVES the file to the location you specify.

It doesn't handle the upload, or receive the file - by the time your upload-handling script gets fired up, the upload has already been completed and the file is waiting in that tmp_name location.

If the move can't be completed for any reason, move_uploaded_file() returns false. It won't warn you if you're overwriting a file in the destination, on the assumption that you know what you're doing.

My mistake. I left the directory test in place. That should have gone. Thanks anyway for all help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM