0

I'm uploading an image using WinSCP, which gets sent to a web server using PHP. Then the site refreshes automatically every X seconds and notices that a new image file is present, and displays it to the user. However, during the loading time, it shows the following image, which doesn't look very nice:

http://s10.postimg.org/fwz0ok16h/imageupload.png

How can I ensure it only shows the image after it is completely uploaded and ready? Is there maybe some sort of "pre-loader" or fade effect you could use in PHP that really only shows the picture once it's done? It needs to be PHP because Javascript can't find out the exact image name. Here's how I currently display the image:

foreach($files as $key => $value)
    {
        if($count >= 1)
            break;

        echo '<th><img id="image" height="250px" width="250px" src="files/'."$key".'"><br />'."$value".'</img></th>';

        $count++;
    }
searchforit
  • 123
  • 2
  • 3
  • 10

2 Answers2

1

Open the Image using

$png = @imagecreatefrompng('stamp.png');

// Or:
$jpg = @imagecreatefromjpeg('photo.jpeg');

If it doesn't fail, the image is complete. If it fails you either don't have the GD lib installed and enabled or the image is corrupt / incomplete.

But it is the best way to check if the Image upload is complete using Javascript event handlers, like onreadystate attached to the upload, and only refresh the page when that event is triggered.

Further links:

jQuery: Check if image exists

Taking control of image loading

Propably the best one: Is there any way to have PHP detect a corrupted image?

Community
  • 1
  • 1
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0

Before outputting a file, check its filemtime. If that modification time is less than a few seconds ago, you can assume that it's still being uploaded and skip over it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592