【发布时间】:2014-06-20 16:38:14
【问题描述】:
这是我尝试通过 ajax 上传时收到的错误:
move_uploaded_file(/public/assets/uploads/photodune-5351272-asian-traditional-bell-l.jpg): failed to open stream: Permission denied
我的上传文件夹已经使用 chmod 设置为 777。这是我的文件夹的状态:
drwxrwxrwx 2 ec2-user ec2-user 4096 Jun 20 10:46 uploads
这是我在 PHP 端使用的代码:
if (Input::hasFile('file')) {
$storeFolder = '/assets/uploads/';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = public_path() . $storeFolder;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile, $targetFile);
echo $targetPath . " / " . $targetFile;
}
} else {
echo "There has been an error in creating the file";
}
为什么我在尝试使用 move_uploaded_file 时仍然会被拒绝?
为了测试,您可以从这里查看文件上传和错误:http://www.relatient.net/relatientv2/public/portal/upload
【问题讨论】:
-
move_uploaded_folder?你是什么意思?move_uploaded_folder不是函数,还是基于 Laravel 的函数? -
不要使用此代码。您直接使用
['name'],恶意用户可以利用它在您的服务器上任意位置乱写他们的文件。而且您似乎根本没有验证上传,只是假设它成功,或者首先执行了上传。 -
我正在使用这个语句来检查文件是否被上传:if (Input::hasFile('file'))。当我得到要实际上传的文件时,我将添加验证和干净的代码。而 move_uploaded_folder 应该是 move_uploaded_file。大脑无处不在。
-
我记得昨天看到一个类似的问题,使用
public_path()是问题所在,给出答案的人建议使用asset等。只是不记得所有细节。 -
Fred,这不是问题,我实际上已经给了它完整的路径,而没有使用任何函数,它仍然给了我与 public_path() 相同的响应。资产,它给你一个 html URL,而不是服务器上的本地路径。