执行命令move_uploaded_file
时出现500错误。(请注意,我不会返回false
)。不知道如何从中获得任何错误消息,但这显然会有所帮助!
我从一个数组$_FILES
开始,在我的测试中,它包含两个文件上的元数据(这是一个非常受控制的测试)。当我只上传一个文件时,这一行过去工作得很好,所以我知道这不是服务器权限问题。
<?php
print_r($_FILES);
$target_dir = "client_resources/";
foreach ($_FILES as $thisFile) {
print_r($thisFile);
$target_file = $target_dir . basename($thisFile['name']);
echo $thisFile['tmp_name'];
echo $target_file;
if (move_uploaded_file($thisFile['tmp_name'], $target_file)) {
echo 'uploaded';
} else {
// don't know how to get the error message from here
}
}
?>
如果我注释掉move_uploaded_file
块,我会得到以下输出:
Array
(
[file0] => Array
(
[name] => back.jpeg
[type] => image/jpeg
[tmp_name] => C:WindowsTempphp8030.tmp
[error] => 0
[size] => 3936
)
[file1] => Array
(
[name] => images.jpeg
[type] => image/jpeg
[tmp_name] => C:WindowsTempphp8041.tmp
[error] => 0
[size] => 8257
)
)
Array
(
[name] => back.jpeg
[type] => image/jpeg
[tmp_name] => C:WindowsTempphp8030.tmp
[error] => 0
[size] => 3936
)
C:WindowsTempphp8030.tmp
client_resources/back.jpeg
Array
(
[name] => images.jpeg
[type] => image/jpeg
[tmp_name] => C:WindowsTempphp8041.tmp
[error] => 0
[size] => 8257
)
C:WindowsTempphp8041.tmp
client_resources/images.jpeg
我也试过把这条线放在try-catch
块中,但我还是崩溃了。
有人明白为什么这不再有效了吗?谢谢
创建脚本时应始终打开错误报告,以确保不会遇到任何问题。这可以用来完成
ini_set('display_errors', 1);
error_reporting(E_ALL);
现在您面临的另一个问题是,您试图将文件存储在相对路径中。要移动文件,您需要一个绝对路径。你可以做你已经做了:
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/client_resources/';
或者研究getcwd()
。(获取站点的当前工作目录)
使用它来获取错误
$_FILES["pictures"]["error"];
如果您正在使用多个文件上传,那么请使用类似数组的上传函数,如下所示。
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
这是错误的描述文件上传中的错误
这样尝试,
$move = @ move_uploaded_file($thisFile['tmp_name'], $target_file);
if(!$move)
echo 'Not uploaded';
else
echo 'uploaded';