/    Sign up×
Community /Pin to ProfileBookmark

file_exists() question..NOT READING THE FILE CORRECTLY!

Hello, I am working on a script that reads a record from a datbase and that becomes the image file name. If it is not found then it should display another default image. Here is my script:

[code=php]
$imagefile = “/images/”. $row[“id”] . “.jpg”;
if (file_exists($imagefile)){
echo “<td colspan=”2″ rowspan=”3″><img class=”border” src=”.images”. $row[“id”] .”.jpg” width=”120″ height=”111″ />n”;
}
else {
echo “<td colspan=”2″ rowspan=”3″><img class=”border” src=”/images/defimage.jpg” width=”120″ height=”111″ />n”;
}
[/code]

I get a display of only the default image (“defimage.jpg”) and I do have files for some of the products. It displays fine if i take out that if else and file_exists(). Does anyone know if im using the correct function? I tried fopen() but that is definetely not the way to go…any suggestions or feedback is greatly appreciated..thanks!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 19.2005 — I'm guessing you probably do not want the leading "/" in front of your "images" directory name (if that directory is right under the directory where your PHP page is).
Copy linkTweet thisAlerts:
@vicpal25authorApr 19.2005 — Well I am using relative links for my site and it seems that the function is not finding the file name correctly so the flag is setting off to display the default image "defimage.jpg".
Copy linkTweet thisAlerts:
@NogDogApr 19.2005 — "/images/filename.jpg" is not a relative link - it is an absolute link to a directory named "images" immediately under the root directory of the active filesystem. "images/filename.jpg" is a relative link to a directory named "images" under the currently active directory. "../images/filename.jpg" is a relative link to a directory which is at the same level as the currently active directory (".." means up one level in the directory tree).
Copy linkTweet thisAlerts:
@vicpal25authorApr 19.2005 — Gotcha! Thanks! But the flag aint being set up correctly with absolute or relative linking.
Copy linkTweet thisAlerts:
@NogDogApr 19.2005 — Just for the helluvit, try using is_readable() instead of file_exists(). The notes for file_exists() seem to indicate it could return false if running in safe_mode and if the file is not actually owned by the PHP user name (typically "nobody" under Apache).
Copy linkTweet thisAlerts:
@vicpal25authorApr 19.2005 — dang it..nope..i condensed my code just to get the functionallity and it is not working..im using

[code=php]
<?php
$imagefile = "/home/content/h/i/s/hispanonatura/html/images/1.jpg";
if (is_readable($_FILES[$imagefile])){
echo "success! it does exist";
}
else {
echo "it does not exist";
}
?>
[/code]


and

[code=php]
<?php
$imagefile = "/home/content/h/i/s/hispanonatura/html/images/1.jpg";
if (file_exists($_FILES[$imagefile])){
echo "success! it does exist";
}
else {
echo "it does not exist";
}
?>

[/code]


AND NO SUCCCESS..HUMMM..THIS IS BECOME FEISTY..

[code=php]
<?php
$imagefile = "/images/1.jpg";
if (is_readable($_FILES[$imagefile])){
echo "success! it does exist";
}
else {
echo "it does not exist";
}
?>
[/code]
Copy linkTweet thisAlerts:
@vicpal25authorApr 19.2005 — yess! i got it to work...i took off $_FILE from the variable and it worked. I had to use the server root but it worked..nice!

[code=php]
<?php
$imagefile = "/home/content/h/i/s/hispanonatura/html/images/1.jpg";
if (file_exists($imagefile)){
echo "success! it does exist";
}
else {
echo "it does not exist";
}
?>
[/code]
×

Success!

Help @vicpal25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...