Tom Wilson
Tom Wilson

Reputation: 253

Laravel File Exception "Could not move the file"

So I'm working on a basic file upload system that for the most part seems to be working. Most files go through perfectly and upload without a hitch, but for some reason, other files do not and I get the following error:

could not move file

This isn't a permissions error as it does work for some files - I don't believe it to be a filesize or filetype issue either.

My upload method is as follows:

$file = Input::file('photo');

$destinationPath    = 'user_img/';
$extension          = $file->getClientOriginalExtension();
$rand               = str_random(12);
$filename           = 'usr_'.  Auth::user()->id . '_str=' . $rand . '_file='. Crypt::encrypt($file->getClientOriginalName()) .'.'. $extension;
$upload_success     = $file->move($destinationPath, $filename);

I'm not finding any solution on the web, and I can't figure out why it's throwing this exception. Any ideas?

Upvotes: 1

Views: 16707

Answers (4)

pomporum
pomporum

Reputation: 36

Something similar happened to me, the context was this: through the view I was loading a file through an input. When I tried to save the file to a folder, I got this error. I use laravel 9 with livewire. In the end I just had to use the methods provided by liveware:

$filename = time() . $this->documento->getClientOriginalName();
$this->documento->storeAs('documents', $filename, 'public');

Fuentes: https://laravel-livewire.com/docs/2.x/file-uploads

Upvotes: 1

gthuo
gthuo

Reputation: 2566

In my case, this was the issue: using reserved characters in a file name.

This is how i was getting the file name:

$photo_name = "User_".md5($user->id).'_'.date('Y-m-d H:i:s').".$ext";

This would mean that the eventual file name would have characters like -,: and _. On reading this Wikipedia article https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words , i realised that the : (colon) is a reserved character and once i got rid of it (by amending the timestamp section to date('Ymd_His'), the error was gone and the upload was successful.

Upvotes: 2

Tom Wilson
Tom Wilson

Reputation: 253

Ah damn, it seems as my filelengths were over 255 characters and thus the filesystem wasn't liking it. I've changed from Crypt to MD5 and the issue is now resolved.

Upvotes: 0

Chris Magnussen
Chris Magnussen

Reputation: 1527

I don't want to count all characters in the filename in the screenshot, but there could be an issue with the length of your file name. Wikipedia Filename - Length Restrictions

Upvotes: 9

Related Questions