Rohith Raveendran
Rohith Raveendran

Reputation: 410

How to know if a image is completely uploaded to a server before creating its thumbnail?

I have script that create image gallery automatically from a folder. It also generates thumbnails if it doesn't exist. Everything works finely except a particular situation. thumbnail generation is invoked by user accessing the page (Psudo CRON).

I was trying to download a image to the folder of gallery in my server using wget (A big image) and at the same time some one accessed the web page and thumbnail was generated from the partial downloaded image, which created a partial thumbnail like below.

Partially generated thumbnail

To fix this before creating thumbnail I started to check file last modification time and modification time after 600 micro second. if they are different I will skip the image thumbnail generation. This should work if the upload is not stuck anywhere and file is being constantly updated. But on other hand it will fail if the upload is not updating file in specified time interval

My code

//loop
$atime = filemtime($images_dir . $file);
usleep(600);
$btime = filemtime($images_dir . $file);
/* file uploading checking */
if ($atime != $btime) {
    continue;
}
// code to generate the thumbnail

Is there any other way to solve this issue ?

Please note here user has direct access to folder and he can populate it any way using ftp/another script etc, that is uploading is not controlled by script.

Upvotes: 1

Views: 174

Answers (1)

Colin M
Colin M

Reputation: 13348

Solution seems pretty simple to me. Don't put the images in your upload directory until the process of downloading the image is complete. You move them into the directory after they're on your filesystem.

Upvotes: 3

Related Questions