1

When I try and upload a file as follows:

$name = $img->getClientOriginalName();
$fullPath = Product::getUploadPath(); // Returns public_path(); with custom directory appended
$uploaded = $img->move($fullPath, $name);

I get the below error:

Could not move the file
"/private/var/folders/1l/fxl7spqj2p113fpffm3k0sy00000gn/T/phpIgpWGy" to 
"/dir/subdir/subsubdir/subsubdir/public/backend/images/products/pic.jpg" ()

For interestsake, I built the entire resolve path for the image, and passed that as an argument to the move() method. Memory served that it has worked previously.

$uploaded = $img->move($fullPath.'/'.$name);

No exceptions are thrown, however, the uploaded file, now becomes a directory:

"/dir/subdir/subsubdir/subsubdir/public/backend/images/products/pic.jpg/"

I'm very close to driving my fist through my screen.

7
  • It could be sleep deprivation, but I'm also quite certain that this code has worked previously, without any known issues, I've recently installed intervention plugin to resize images, however, while debugging, I've removed the plugin from my App Kernel. Commented Aug 15, 2016 at 22:23
  • 1
    Without digging too far, did you check the permissions on the target directory? The move() method of the UploadedFile class calls PHP's move_uploaded_file() function. They suppress errors from that call but throw a FileException with that custom message you're seeing. The file being created as a directory with the second attempt makes sense as the first parameter is the target directory, and the second name is the file name - without a filename, only a directory is resolved. Commented Aug 15, 2016 at 22:30
  • Could it be possible that, in the case of a directory not existing, and laravel creating the path when move() is called, that the given directory could have insufficient permissions? Also, when it does create directory, the directory isn't empty, seems to be the temporary PHP residing within. Commented Aug 15, 2016 at 22:36
  • Guess could mean it has write permissions and not execute permissions to rename the file. However, again, I refer to 'dynamically' created directory having insufficient permissions. Commented Aug 15, 2016 at 22:38
  • 1
    It actually came down to permissions? I'm skeptical, but hey, I'll take it. Doesn't look to me like Laravel attempts to create any missing directories - or the Symfony components its built on. Oh well, I'll throw both into an answer. Commented Aug 15, 2016 at 22:47

1 Answer 1

1

Could be a permissions issue - the user running the PHP process will need to have write access to the directory where the file will be saved. If you can stomach chmod'ing the directory to 777 for testing, that would at least provide an immediate yes or no to the permissions possibility.

Also, be sure that the directory that file is to be moved to exists - Symfony's UploadedFile::move() method falls back to PHP's move_uploaded_file() function, which according to a comment on the documentation will not create missing directories when moving a file.

When it comes to uploading files, we've all been there before. Can be maddening.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.