php - How to check if an image is incompleted (missing data)? -


so have program on raspberry pi supposed regularly backup blog onto it. i'm running manually. today internet connection extremely slow killed program in middle of download. did save of downloaded data managed , program reads image exists , skips it. sure can delete , have program re-download me want make sure doesn't happen again in future.

i'm working php on server side. command use saving images is

copy($url, $path); 

i'm doing simple check if file exists.

if(!file_exists($path)) 

the image files on server of png , jpg file formats.


dumb me, forgot write had tried. have found multiple questions solutions don't seem work. of them claim imagecreatefromtype($img) should return false in situations.

php manual:

returns image resource identifier on success, false on errors.

i'm getting "premature end of jpeg file" seemed should have returned false didn't. returns same value if image not corrupt,

resource id #6 

it great have sort of quick way of determining if images whole or not.

you can download temporary file (on same partition), , rename file when download has finished. renaming file atomic operation - long source , destination located on same partition - make sure image valid.

like this:

// create tempfile $tempfile = tempnam("/path/to/tempfolder", "download");  // download tempfile $ret = copy($url, $tempfile);  if($ret) {     // move tempfile final location.     // atomic operation (with restrictions named above)     rename($tempfile, "path/to/image.png"); } else {     unlink($tempfile);     die("download broken"); } 

even if copy not finish - because of power outage or whatever, final image not getting created in broken state.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -