Jump to content

Recommended Posts

I have this page which displays images from a folder based on entries to a mysql table. with this code everything works fine:

 

while($query_row = mysql_fetch_assoc($query)) {
echo "<img src='uploads/".$cat."/".urlencode($query_row['url'])."'/>&nbsp&nbsp";
if ($i==5){echo"<br><br>"; $i=0;}
$i++;
}

 

the problem occurs when i try and add this code instead

 

while($query_row = mysql_fetch_assoc($query)) {

//this gets the images information and sets the height and width if too high!
$info = getimagesize("uploads/".$cat."/".urlencode($query_row['url'])."");
$height = $info[1];
$width = $info[0];

if ($height > 500){
$height = 500;
}
if ($width > 500){
$width = 500;
}
echo "<img src='uploads/".$cat."/".urlencode($query_row['url'])."' height='".$height."' width='".$width."'/>&nbsp&nbsp";
if ($i==5){echo"<br><br>"; $i=0;}
$i++;
}

 

it throws out errors like this:

 

 

Warning: getimagesize(uploads/cakes/imagejpg.jpg) [function.getimagesize]: failed to open stream: No such file or directory on line 31

 

so whats wrong with this line?

 

 

$info = getimagesize("uploads/".$cat."/".urlencode($query_row['url'])."");

 

 

i know i have a double quotation but im tired and i cant think how to fix that atm, although im not sure if thats the problem or not?

 

Sean

Link to comment
https://forums.phpfreaks.com/topic/259385-why-am-i-getting-this-error/
Share on other sites

have you tried absolute path or just "../" or "/"

 

i had the same error (not finding my directory) so where's your uploads folder? in the same root as the file where this code is or somewhere else?

 

 

yes i have tried both ../ and / (neither worked)

 

the uploads folder is in the same root folder that the script file is in, i know that it shouldn't be a not finding directory error because ALL the images are in the exact same folder, however not ALL the images are giving this error, some in fact most are working fine, the page shows 100 images and only around 2-3 are failing with this error which i find weird.

 

Sean

do you have firebug or an other inspector?

try inspecting the path of an image that's not showing, that way you can see where is trying to pull the image from and if the path is correct, make sure the images are uploaded to the server.

 

no i dont have firebug or any other inspector, ill look into it though...

The image is STILL showing, but its show along with the error.

 

live example in my signature...

 

the error is appearing on every image now, however the images are still displaying.

The browser decodes the string automatically, PHP doesn't.

 

Warning: getimagesize(uploads/cakes/CreamCakes_%2810%29.jpg) [function.getimagesize]: failed to open stream: No such file or directory in

 

the error is pretty obvious, do you have a file named 'CreamCakes_%2810%29.jpg'?

And yeah, keep your file names simple to avoid this all together.

HTM Tag img src="X" takes from website root

 

Whereas getimagesize() taks from Web Server Root

 

Try something like

 

$imageFile = $_SERVER['DOCUMENT_ROOT']."uploads/".$cat."/".urlencode($query_row['url']);

if(file_exists($imageFile){
$info = getimagesize("uploads/".$cat."/".urlencode($query_row['url'])."");
// Continue rest of code;
}else{
// add debug for file not found
}

 

The browser decodes the string automatically, PHP doesn't.

 

Warning: getimagesize(uploads/cakes/CreamCakes_%2810%29.jpg) [function.getimagesize]: failed to open stream: No such file or directory in

 

the error is pretty obvious, do you have a file named 'CreamCakes_%2810%29.jpg'?

And yeah, keep your file names simple to avoid this all together.

 

yes that image exsists and is being displayed just above the error message

 

HTM Tag img src="X" takes from website root

 

Whereas getimagesize() taks from Web Server Root

 

Try something like

 

$imageFile = $_SERVER['DOCUMENT_ROOT']."uploads/".$cat."/".urlencode($query_row['url']);

if(file_exists($imageFile){
$info = getimagesize("uploads/".$cat."/".urlencode($query_row['url'])."");
// Continue rest of code;
}else{
// add debug for file not found
}

 

 

the file does exist though and is being displayed...

just as a test to show what is being said about the filename, change 1 filename in your database and in the image directory, i suggest this one

 

uploads/cakes/CreamCakes_%281%29.jpg

 

to

 

uploads/cakes/CreamCakes_1.jpg

 

and I bet the error goes away for that first image, the web browser treats the %28 and %29 as brackets but the php script does not do this causing the error so in the script that file does not exist but in the web browser it does

just as a test to show what is being said about the filename, change 1 filename in your database and in the image directory, i suggest this one

 

uploads/cakes/CreamCakes_%281%29.jpg

 

to

 

uploads/cakes/CreamCakes_1.jpg

 

and I bet the error goes away for that first image, the web browser treats the %28 and %29 as brackets but the php script does not do this causing the error so in the script that file does not exist but in the web browser it does

 

after looking into my directory i realised that there was not a image called that... its actually called:

 

CreamCakes_(1).jpg

 

 

thats right and your urlencode is changing () to %28 and %29, while the web browser translates that back to brackets the php script wont, change the image name to remove the brackets and it should resolve your problem

 

Hmm is there no way to handle that?

Don't use urlencode on the value you put into the getimagesize() statement. Like its name indicates, urlencode is used in URLs. The value you output on your page in the <img src="..."> tag is a URL, the value you use in getimagesize() is a file system path.

yes removing the urlencode worked a treat and i no longer get any errors, i didnt wish to change the image names because i currently have over 5k images and although im sure i could create some type of renaming script, this seemed much easier!

 

Thanks! solved

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.