[英]How to fix "getimagesize(): Read error!" in PHP
I want to upload images to a server using ajax and php.我想使用 ajax 和 php 将图像上传到服务器。
On client-side i hava the following code:在客户端,我有以下代码:
var reader = new FileReader();
reader.readAsDataURL(file, 'UTF-8');
reader.onload = function (event) {
var result = event.target.result;
$.ajax({
type: "POST",
url: "url",
data: {
data: result,
name: file.name,
mimeType: file.type
}
});
};
On server-side my code looks like the following:在服务器端,我的代码如下所示:
$path = 'somehow/'
$fp = fopen( $path . $_POST['name'], 'w');
$data = explode( ',', $_POST['data'] );
fwrite($fp, base64_decode( $data[ 1 ] ));
fclose($fp);
chmod($path . $_POST['name'], 7777);
list($width, $height) = getimagesize($path . $_POST['name']);
Now the method "getimagesize" always runs into an error "getimagesize(): Read error!"现在方法“getimagesize”总是遇到错误“getimagesize():读取错误!”
Does anybody know, why this happens?有谁知道,为什么会这样? When i look into the filesystem on the server the file 'FILENAME.JPG' exists ...当我查看服务器上的文件系统时,文件“FILENAME.JPG”存在......
There are two possible reasons for this error : either the url path is incommplete/incorrect or the picture file is corrupt and cannot be opened.此错误有两种可能的原因:url 路径不完整/不正确或图片文件已损坏且无法打开。 A file may be corrupted, for example, if the image is not fully/correctly loaded onto the server.例如,如果图像未完全/正确加载到服务器上,则文件可能已损坏。
You can use $result = @getimagesize($file)
to suppress the error.您可以使用$result = @getimagesize($file)
来抑制错误。 So, when the $result
is empty, means there is an error occurred.因此,当$result
为空时,意味着发生了错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.