Click to See Complete Forum and Search --> : Remote File Information


Nevermore
04-02-2004, 12:37 PM
Is it possible to get the size of a remote file and find out whether it is the file requested or a 404 error page without downloading the file to the server? I know it can't be done with functions like filesize() because HTTP doesn't support stat commands.

Thanks in advance.

sciguyryan
04-03-2004, 08:47 AM
I think this works: (from PHP.net - I did not write it):


<?

function showExtension($file){
if(is_file($file)){
$fileInfo = pathinfo($file);
$extension=$fileInfo["extension"];
} else {
$extension="";
}
return $extension;
}

function fsize($size) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");

$pos = 0;
while ($size >= 1024) {
$size /= 1024;
$pos++;
}

return round($size,2)." ".$a[$pos];
}


function ls ($curpath) {
$dir = dir($curpath);
$file_namearr = array("ETC","PPT","XLS","DOC","PDF");


echo("<b>$curpath</b>");
echo "<blockquote>";
$file_sizearr = array(1,1,1,1,1);
while ($file = $dir->read()) {
if($file != "." && $file != "..") {
if (is_dir($curpath.$file)) {
ls($curpath.$file."/");
}
else {
$filext= strtoupper(showExtension($curpath.$file));
$curr_key = 0;
$curr_key = array_search($filext,$file_namearr);
if($curr_key){
//echo "<br>$filext:$curr_key";
$curr_file_size = $file_sizearr[$curr_key];
if(filesize($curpath.$file)>$curr_file_size){
$file_sizearr[$curr_key]= intval(filesize($curpath.$file));
}
}

//echo"<br> $file : ".fsize($curpath.$file);
}
}

}//while ends
for($k=0;$k<5;$k++){
echo"<br>".$file_namearr[$k].":".fsize($file_sizearr[$k]);
}
$dir->close();
echo "</blockquote>";
return;
}

$startpath = "Your path";
ls($startpath);


?>