简体   繁体   中英

How to fix "getimagesize(): Read error!" in PHP

I want to upload images to a server using ajax and 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!"

Does anybody know, why this happens? When i look into the filesystem on the server the file 'FILENAME.JPG' exists ...

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. 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. So, when the $result is empty, means there is an error occurred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM