0

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.

Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.

The error:

Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301

The code:

if (file_exists($filename)) {
    $image = imagecreatefrompng($filename);
}

I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Johan
  • 189
  • 3
  • 12
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 11 '16 at 09:45

6 Answers6

1

I suggest you use is_readable

if (is_readable($filename)) {
    $image = imagecreatefrompng($filename);
}
Ruel
  • 15,438
  • 7
  • 38
  • 49
  • I'm starting to wonder if it has something to do with a slow computer, because I get it way more often on my developing machine than on my test server – Johan Nov 11 '10 at 17:05
0

bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

jcobhams
  • 796
  • 2
  • 12
  • 29
0

The file may "exist" but is the file accessible? what does file_exists actually do?

if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.

it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.

maybe try is_readable() or is_writable() instead?

Patrick
  • 7,512
  • 7
  • 39
  • 50
  • i know NOTHING of java... but when it comes to files i seem to be running into this stuff A LOT – Patrick Nov 11 '10 at 16:44
  • file_exists is a native PHP-function – Johan Nov 11 '10 at 16:44
  • yeah i see that from Ruel's comment... thanks.. just threw out there is_readable and is_writable... – Patrick Nov 11 '10 at 16:45
  • Yeah, I've thought of running a loop and trying several times since it works sometimes, but it seems like a ugly hack :p – Johan Nov 11 '10 at 16:46
  • it may be an "ugly" hack... but not knowing java well enough i couldnt give a better answer... in c# i would use events to make sure the file was done before even trying. – Patrick Nov 11 '10 at 16:47
  • besides... GOOD error handling in this environment will save you a lot of heartache later on. – Patrick Nov 11 '10 at 16:48
  • @Patrick, why are you talking about Java? This is a PHP question. :) – Ruel Nov 11 '10 at 16:49
0

Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.

Ioannis Karadimas
  • 7,746
  • 3
  • 35
  • 45
0

Use is_readable() to check whatever you have permission to access that file.

Shoe
  • 74,840
  • 36
  • 166
  • 272
0

You can try GD :

IF($img = @GETIMAGESIZE("testimage.gif")){ 
     ECHO "image exists"; 
}ELSE{ 
     ECHO "image does not exist"; 
} 
Conex
  • 832
  • 8
  • 17