【发布时间】:2016-02-27 12:33:04
【问题描述】:
虽然我没有更改任何内容,但自从我更改了网络主机后,此工作文件上传已停止工作。
我检查了我是否可以上传文件,并且最大文件大小设置为 256MB,所以问题不存在。我上传的目录也将 CHMOD 设置为 777。
PHP 版本为 5.6.15
将文件上传到我的服务的客户端位于同一个域中。
这是我的客户端代码:
error_reporting(E_ALL);
$uploaddir = 'tempUploads/';
$random_file_name = uniqid(rand(), true);
$file_extension = pathinfo(basename($_FILES['fileToUpload']['name']), PATHINFO_EXTENSION);
$uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);
$optional_id = $_POST['optional_id'];
$upload_type = $_POST['upload_type'];
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile))
{
$ch = curl_init();
$filepath = str_replace('\\', '/', realpath(dirname(__FILE__))) . "/";
$target_path = $filepath . $uploadfile;
var_dump(file_exists($target_path));
$data = array('optional_id' => $optional_id, 'file' => '@'.$target_path, 'upload_type' => $upload_type);
curl_setopt($ch, CURLOPT_URL, 'http://domain.dk/webservice/v1/uploadFile');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($ch);
if ($curl_response === false)
{
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
}
else
{
echo "Kunne ikke uploade filen";
}
如您所见,我在 target_path 上做了一个 var_dump,这表明文件已上传到客户端临时文件夹。我检查了文件夹,文件已按预期上传到该文件夹。
我的服务函数如下所示:
$app->post('/uploadFile', 'authenticate', function() use ($app)
{
$optional_id = $app->request->post('optional_id');
$upload_type = $app->request->post('upload_type');
$file = $app->request->post('file');
$uploaddir = "";
// set directory where file needs to be saved
switch($upload_type)
{
case 1:
$uploaddir = '../uploadedContent/calibrationCertificate/';
break;
case 2:
$uploaddir = '../uploadedContent/educationCertificate/';
break;
default:
}
// random file name to avoid overwriting existing files.
$random_file_name = uniqid(rand(), true);
// extension of the file
$file_extension = pathinfo(basename($_FILES['file']['name']), PATHINFO_EXTENSION);
// full path and name
$uploadfile = $uploaddir . $random_file_name.".".$file_extension;
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
$db = new DbHandler();
switch($upload_type)
{
case 1:
$db->insertEquipmentDocumentRef($optional_id,$uploadfile,basename($_FILES['file']['name']));
break;
case 2:
$db->insertEducationDocumentRef($optional_id,$uploadfile,basename($_FILES['file']['name']));
break;
default:
}
$response["error"] = false;
$response["message"] = "filen blev uploadet";
$response["optional_id"] = $optional_id;
$response["upload_type"] = $upload_type;
$response["uploadfile"] = $file;
}
else
{
$response["error"] = false;
$response["message"] = "filen kunne ikke uploades";
$response["dir"] = $uploadfile;
$response["dir"] = $uploadfile;
$response["move from"] = pathinfo(basename($_FILES['file']['name']));
if(!$_FILES)
{
$response["file"] = "File does not exist";
}
}
echoRespnse(201, $response);
});
您应该再次注意到,在 move_uploaded_files 失败后,我在代码底部检查了 $_FILES 是否为空。检查我的浏览器调试器显示 $_FILES 为空。
$_FILES 变量怎么设置为 null ? .. 在我更换虚拟主机之前它运行顺利...
我认为可能出错的唯一地方是我通过 postfields 数组将文件“中继”到服务但我不确定:
$data = array('optional_id' => $optional_id, 'file' => '@'.$target_path, 'upload_type' => $upload_type);
【问题讨论】:
-
错误 #1 也是最糟糕的:您从不费心检查上传是否成功,您只是假设没有任何事情会失败并继续犯错误。 $_FILES 中有一个
['error']参数是有原因的 - 它应该是您检查的 FIRST 内容,然后再对上传的内容执行任何其他操作。 -
@MarcB 好点.. 但 $_FILES 为空.. 所以我无法得到那个错误
标签: php file-upload