我在PHP中使用微调器,并发生了一些错误。当我在后端使用stream_copy_to_stream()时,它总是返回0。
下面是后端的代码:
private function upload_file($file_name, $tmp_name)
{
$result = array(
'is_successful' => true,
'extra_message' => ''
);
$target_path = $this->get_target_file_path($file_name, $tmp_name);
move_uploaded_file($tmp_name, $target_path);
$result['is_successful'] = $this->handle_upload_request($target_path);
if ( $result['is_successful'] ) {
$result['extra_message'] = $target_path;
} else {
$result['extra_message'] = 'Unknown error occured.<br />';
}
return $result;
}
private function handle_upload_request($path)
{
$input = fopen("php://input", "r");
$temp = tmpfile();
$real_size = stream_copy_to_stream($input, $temp);
fclose($input);
echo $real_size;
if ($real_size != $this->get_size()){
return false;
}
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
return true;
}但是,$real_size总是等于0。奇怪的是,有时可以成功地上传文件,但有时却不能。
我想这可能是因为Linux的许可。因为我发现当我上传一个文件时,文件的模块是644 (但我认为644就足够了)。这个问题在Windows中也存在。
它有什么问题?
发布于 2013-08-19 23:49:10
您不应该使用php://input。php://input用于访问原始请求体。对于多部分编码请求,它是空的。所有上传请求发送的精细Uploader是多部分编码默认。相反,您应该使用$_FILES超级全局获取与请求关联的文件。有一个函数PHP示例将在精细Uploader服务器Github repo中为您演示这一点和更多内容。
如果您坚持编写自己的PHP代码来处理请求,那么确实需要先读取传统的精细Uploader服务器端文档,这告诉您,默认情况下,所有上传请求都是多部分编码的。这被设置为默认设置,以使跨浏览器处理上传请求更加容易,因为无论如何我们都需要从IE9和更早版本发送MPE请求中的文件,因为IE9和IE9不支持通过ajax请求(XHR2)上传文件。
https://stackoverflow.com/questions/18324592
复制相似问题