/    Sign up×
Community /Pin to ProfileBookmark

file_exists problem

I wrote a little script to check if an image exists using file_exists but it didn’t work, I also tried another “is_file” I think, neither worked. I found the below and used it but I’m getting errors obviously when the file doesn’t exist, although from the user end it works well:

[code=php]
$cached = “/”.$cache.”.jpg”;
$AgetHeaders = @get_headers($cached);
if (preg_match(“|200|”, $AgetHeaders)) {
$bimage=$cached;
} else {
$bimage=”thumb.php?img=”.$value.”.jpg”;
}
[/code]

the first one I tried is below, but it kept returning the file name does not exist although i could type it into the browser and see the image. any ideas? does it not work with images?

[code=php]<?php
$filename = ‘/path/to/foo.txt’;

if (file_exists($filename)) {
echo “The file $filename exists”;
} else {
echo “The file $filename does not exist”;
}
?>
[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderDec 04.2007 — Is it a permission problem?

Is the file stored within your local sites base dir?

Maybe you need open_base_dir setup in php.ini?

What OS are you running, is "/" correct or should you be using ""?
Copy linkTweet thisAlerts:
@72newbieauthorDec 04.2007 — when you permissions I take it you mean the folder I'm checking? it's folder is 755 and it is directly off the root ie, "site.com/images/"...in this line "$cached = "/".$cache.".jpg";" I have tried http://site[dot]com/images/ , /images and images/...none work?

Maybe you need open_base_dir setup in php.ini?[/QUOTE]Yes, I do have access to the php.ini file, how should I set the open_base_dir?

machine is Unix Apache version 1.3.37 PHP is 5.0.5
Copy linkTweet thisAlerts:
@MrCoderDec 04.2007 — What is the full path to the file?

If you have shell access, cd to the folder and type "pwd".

On linux this will give you the full path to the folder you are in.
Copy linkTweet thisAlerts:
@72newbieauthorDec 04.2007 — What is the full path to the file?[/QUOTE]hehe, Thanks ?
Copy linkTweet thisAlerts:
@TJ111Dec 04.2007 — In PHP, a path like "/path/to/foo.txt" is from the root directory of the server, not the web root. You either have to use relative paths to the images, or full paths from the server root.

"../path/to/foo.txt"

"/users/web/httpdocs/path/to/foo.txt"
Copy linkTweet thisAlerts:
@MrCoderDec 04.2007 — hehe, Thanks ?[/QUOTE]

Glad I could help.
Copy linkTweet thisAlerts:
@72newbieauthorDec 05.2007 — Much appreciated. Thank you sir ?
×

Success!

Help @72newbie 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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