简体   繁体   中英

PHP move_uploaded_file/copy issue

Interesting issue I seem to have come across. I have a form that uploads an image and stores the value in a database table. The form uploads the image file OK and makes it available for processing. The issue is as follows; using move_uploaded_file to the specified directory does not work, however using copy() to this directory does.

The code currently is as follows:

$file = $_FILES['doc_path'];

  $ext = array_pop(explode('.', $file['name']));
  $filename = uniqid() . '.' . $ext;

  if ($file['error'] == UPLOAD_ERR_NO_FILE && ! strlen($this->filename)) {
   throw new Exception('Please select a file to upload');
  } elseif ($file['error'] == UPLOAD_ERR_NO_FILE) {
   return true; // already have a file
  } elseif ($file['error']) {
   throw new Exception('File upload error');
  } elseif (! $file['size']) {
   throw new Exception('File is of zero length');
  } else {

   $path = 'uploads/' . $filename;


   if (! move_uploaded_file($file['tmp_name'], $path)) {
    throw new Exception('Could not upload file');
   }

   return $filename;
  }

I have checked that the target directory exists, and the directory is writable. No error is produced using move_uploaded_file() just the "Could not upload file" exception is caught.

Would have thought if this was a permissions issue then substituting move_uploaded_file for copy wouldn't work?

No error is produced using move_uploaded_file() just the "Could not upload file" exception is caught.

That is probably because of your error reporting settings.

What does a error_reporting(E_ALL); or echo error_get_last(); show?

@simnon -

I think it might be a path issue. Either in your source or your target.

Try changing:

$path = 'uploads/' . $filename;

To:

$path = '/uploads/' . $filename;

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