Click to See Complete Forum and Search --> : is_file() or file_exists() - URL


tripwater
06-10-2005, 01:58 PM
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:


//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>";
}



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.

ShrineDesigns
06-10-2005, 02:23 PM
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(), examplefunction 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;
}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 variationfunction 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.1\r\nHost: {$u['domain']}\r\nConnection: Close\r\n\r\n";
socket_write($sp, $in, strlen($in));
$out = socket_read($sp, 1024);
socket_close($sp);
$out = explode("\r\n", $out, 2);
unset($out[1]);
$out = explode(" ", $out[0]);
return array($out[1], $status[$out[1]]);
}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

tripwater
06-10-2005, 02:27 PM
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

ShrineDesigns
06-11-2005, 11:46 PM
the function i post will tell you if the file exists or not, example<?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";
}
?>

SpectreReturns
06-12-2005, 12:59 AM
If you are trying to figure out if somethign on the same server exists, make sure you have a path for file_exists.