Hi there all
However I have opened a similar topic, but this one's purpose is sharing all of our experiences, tutorials, etc. with fsockopen() function...
It seems it's a pretty tricky function...:-)
But i also have an exact problem. I want a php script that can check file sizes on both FTP and HTTP remote servers even if they require login/pass or diffrent port than default.
I have this code:
error_reporting(E_ALL);
$kb = 1024;
$mb = 1024 * $kb;
$gb = 1024 * $mb;
$tb = 1024 * $gb;
if (!isset($f)) {
$fileurl = "http://digitalspacenet.tucows.com/files5/12wash.exe";
} else {
$fileurl = $f;
}
$pu = @parse_url($fileurl);
$filename = basename($fileurl);
$domain = $pu['host'];
$file = $pu['path'];
$host = $pu['host'];
$http = "GET ".$file." HTTP/1.0\r\nHost: ".$host."\r\n\r\n";
$fp = fsockopen ($domain, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
}
else {
fputs ($fp, $http);
while (!feof($fp)){
$chunk= fgets ($fp,128);
if (strpos($chunk, "ength:"))
{
$size = explode (':', $chunk);
if($size[1] < $kb) {
$s = $size[1]." B";
}
else if($size[1] < $mb) {
$s = round($size[1]/$kb,2)." KB";
}
else if($size[1] < $gb) {
$s = round($size[1]/$mb,2)." MB";
}
else if($size[1] < $tb) {
$s = round($size[1]/$gb,2)." GB";
}
else {
$s = round($size[1]/$tb,2)." TB";
}
echo "A(z) $filename fájl mérete: $s";
break;
}
}
fclose ($fp);
}
This works with the following url formats:
http://www.example.com/files/file.zip
http://example.com/files/file.zip
[url]ftp://ftp.example.com/files/file.zip[/url]
[url]ftp://example.com/files/file.zip[/url]
but doesn't work with these formats:
[url]http://user:needpass@www.example.com/files/file.zip[/url]
http://user:needpass@example.com/files/file.zip
[url]ftp://user:needpass@ftp.example.com/files/file.zip[/url]
[url]ftp://user:needpass@example.com/files/file.zip[/url]
[url]ftp://user:needpass@ftp.example.com:666/files/file.zip[/url]
[url]ftp://user:needpass@example.com:666/files/file.zip[/url]
So, my primary question is how can i get this code to check filesizes on both HTTP and FTP servers even if they are required to use user/pass and/or different port than default?
Any help would be appreciated!
Includes docs, tutorials, sample codes, everything about fsockopen() function!
Dedix