views:

37

answers:

2

Hi,

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?

Any enlightenment would be appreciated.

Thanks

A: 

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?

Pekka
Hi, error_get_last reports: Array ( [type] => 2048 [message] => Only variables should be passed by reference [file] => /application/modules/admin/forms/Page.php [line] => 211 ) which refers to the line $ext = array_pop.... Can't see that making the difference.
simnom
@simnom does `$file['tmp_name']` actually exist? See the manual: `If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.`
Pekka
Adding an echo $file['tmp_name'] before the move_uploaded_file statement produces "/tmp/phpfyaV0G" so based on that I'm assuming yes.
simnom
@simnom do a `is_readable()` to make sure
Pekka
@pekka - interestingly that returns false.
simnom
@simnom does it `file_exists()`?
Pekka
@pekka - wtf?? That also returns a false. Number of times I've created an upload form without problem - why now :-(
simnom
@simnom then do a `print_r($_FILES);`. I bet the `error` flag has a value
Pekka
@simnom if that is true, see http://php.net/manual/en/features.file-upload.errors.php
Pekka
print_r($_FILES) gives the following: Array ( [pageHeader] => Array ( [name] => test-jpg.jpg [type] => application/octet-stream [tmp_name] => /tmp/php7hQW3l [error] => 0 [size] => 6353 ) ) i.e. error = 0 - which indicates the file is uploading OK ?!?!?
simnom
@simnom that is really strange. Might be a server issue then, but it would have to be a gross misconfiguration... Have you tried with a different file and file type?
Pekka
OK worked out the problem, I'm using Zend and whilst not thinking that this would impact the $_FILES variables it appears that it does something quirky to these variables. Extending the Zend getValues function to run to process the files first does the trick. Thanks for all your help @pekka.
simnom
A: 

@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;
Gutzofter
Hi, Afraid not, tried that and it's made no difference.
simnom

related questions