/    Sign up×
Community /Pin to ProfileBookmark

is_file() or file_exists() – URL

Hello

Here is what I am trying to do. I have a directory on my server that holds modules for download. Users can upload modules to this dir.

Now I query my DB for all of the Module names and list them out. Now it is possible that a old module is still in the DB but the actual file is not in the modules directory. So if it is not in the directory, I jsut want to show the name, otherwise I want to show a link so they can download the latest module.

Here is some basic code:

[code]
//here I query for the URL of the site from the company table in order to put it in the download URL

$query = “select URL from company”;

$tempresult = @mysql_query($query);

$temprow = @mysql_fetch_array($tempresult);

for ($i = 0; $i < @mysql_num_rows($result); $i++)
{
$row = @mysql_fetch_array($result);

$file = $temprow[“URL”].”/modules/”.$row[“ModuleName”];

$value .= “<tr>
<td width=300>”;

//here I check to see if the file exists in the dir. If not then I do not show a link
//to download it

if (!is_file($file))
$value .= $row[“ModuleName”].” (File Missing).”;
else
$value .= “<a href=””.$file.””>”.$row[“ModuleName”].”</a>”;

$value .= “</td>
<td width=300>
“.$row[“Version”].”
</td>
<td>
“.$row[“LastChangeDate”].”
</td>
</tr>”;
}
[/code]

As of right now if I use either is_file() or file_exists() and pass in $file, everything is normal text not a link. I know for a fact most of these are in the dir. I just know that I am doing something wrong. Can you not pass in the full URL to a file into one of these functions? If not, then how do I determine if the file does in fact exist at this location?

Thank you for any help with this.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJun 10.2005 — yes and no, file_exists(): php 3/4 you can't use a url, php 5 you can

there are several methods i have used to check if a link is valid, apache_lookup_uir(), example[code=php]function lookup_uri($uri)
{
$status = array
(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
if(strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false && strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') === false)
{
$info = apache_lookup_uri($uri);
return array($info->status, $status[$info->status]);
}
if(($fp = @fopen($uri, 'rb')) !== false)
{
socket_set_timeout($fp, 0, 0);
$status = socket_get_status($fp);
fclose($fp);
list(, $sc, $sm) = explode(' ', $status['wrapper_data'][0], 3);
return array($sc, $sm);
}
return false;
}[/code]
though experience that this is has a lot of overhead and slows a script down dispite try everything to speed it up, this is my latest variation[code=php]function lookup($uri)
{
$status = array
(
100 => 'Continue', 101 => 'Switching Protocols',
200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',
300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed',
500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported'
);
$u = parse_url($uri);

if(!isset($u['path']))
{
$u['path'] = '/';
}
if(!isset($u['port']))
{
$u['port'] = getservbyname('www', 'tcp');
}
$addr = gethostbyname($u['domain']);
$sp = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if(!$sp)
{
return false;
}
socket_set_timeout($sp, 0, 0);
$result = @socket_connect($sp, $addr, $u['port']);

if(!$result)
{
return false;
}
$in = "HEAD {$u['path']} HTTP/1.1rnHost: {$u['domain']}rnConnection: Closernrn";
socket_write($sp, $in, strlen($in));
$out = socket_read($sp, 1024);
socket_close($sp);
$out = explode("rn", $out, 2);
unset($out[1]);
$out = explode(" ", $out[0]);
return array($out[1], $status[$out[1]]);
}[/code]
i haven't tested this much, but is showing signs of vast improvement over the other one

returned value is an array index 0 is the status code, and index 1 is the status message, anything 400 and over basically says the uri doesn't exist or false if the connection failed
Copy linkTweet thisAlerts:
@tripwaterauthorJun 10.2005 — WOW. Thanks for the response.

I don't need to see if a link is valid. I need to see if the file actually exists on the server in the designated directory. If it does then I show a link to download it if not then I show the name of the file with (file missing) beside it.

Is this not possible?

I thought for sure that file_exists() would be my answer but I cannot get it to work passing in the full path to the file.

Thanks again
Copy linkTweet thisAlerts:
@ShrineDesignsJun 12.2005 — the function i post will tell you if the file exists or not, example[code=php]<?php
$status = lookup('http://www.domain.com/path/to/file');

if(!$status)
{
echo "failed to connect to website";
}
else if($status[0] >= 400)
{
echo "file does not exist on that website or is not accessible";
}
else
{
echo "all is good";
}
?>[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsJun 12.2005 — If you are trying to figure out if somethign on the same server exists, make sure you have a path for file_exists.
×

Success!

Help @tripwater 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.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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