Laravel 文件异常“无法移动文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24727290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:48:23  来源:igfitidea点击:

Laravel File Exception "Could not move the file"

phpimagefilelaravelupload

提问by Tom Wilson

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?

我没有在网上找到任何解决方案,我无法弄清楚它为什么会抛出这个异常。有任何想法吗?

回答by Chris Magnussen

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

我不想计算屏幕截图中文件名中的所有字符,但文件名的长度可能存在问题。维基百科文件名 - 长度限制

回答by gthuo

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.

这意味着最终的文件名将包含诸如-,:和 之类的字符_。在阅读这篇维基百科文章https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words 时,我意识到:(冒号)是一个保留字符,一旦我摆脱了它(通过将时间戳部分修改为date('Ymd_His'),错误是消失了,上传成功。

回答by Tom Wilson

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.

该死的,似乎我的文件长度超过 255 个字符,因此文件系统不喜欢它。我已从 Crypt 更改为 MD5,问题现已解决。