I want to get remote file content using socket accept, what should my simple code look like for that?
I think this may be what you are looking for The following code is found at: http://php.net/manual/en/function.fsockopen.php <?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>
Thanks littlejohn199 But I doubt for GET, I am going to get big files, will GET method be able to do that?
I want to add in same file, I mean get remote x.zip and add it in current file system as x.zip etc... same with all types of files.
k sorry, here is correct question say we have http://host1.com/somefile.zip somefile.zip on server1 ( http://host1.com ) I want to create same file on my second server http://host2.com/somefile.zip somefile.zip on server2 ( http://host2.com ) Php script is located on server2 ( http://host2.com/getremotefile.php )
<?php $fileLocation="http://host1.com/somefile.zip"; $fileName="somefile.zip"; file_put_contents($fileName, file_get_contents($fileLocation)); ?> PHP:
thanks TimK, but as far as I know the remote file access is not allowed via fopen() or file_get_contents() on all hosting servers, this will not work on many of them, am I correct?
You are correct, in which case you could use cURL. <?php function curl_get_file_contents($URL) { $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_URL, $URL); $contents = curl_exec($c); curl_close($c); if ($contents) return $contents; else return FALSE; } file_get_contents_curl($fileName); ?> PHP:
Here is a function to see the size before downloading on server2. function remotefsize($url) { global $CONF, $DB, $FORM, $LNG, $TIMER; $urlpar = parse_url($url); $sch = $urlpar['scheme']; if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) return false; if (($sch == "http") || ($sch == "https")) { $headers = $this -> my_get_headers($url, 1); if ((!array_key_exists("content-length", $headers))) return false; return $headers["content-length"]; } if (($sch == "ftp") || ($sch == "ftps")) { $server = $urlpar['host']; $port = $urlpar['port']; $path = $urlpar['path']; $user = $urlpar['user']; $pass = $urlpar['pass']; if ((!$server) || (!$path))return false; if (!$port)$port = 21; if (!$user)$user = "anonymous"; if (!$pass) $pass = "phpos@"; switch ($sch) { case "ftp": $ftpid = ftp_connect($server, $port); break; case "ftps": $ftpid = ftp_ssl_connect($server, $port); break; } if (!$ftpid) return false; $login = ftp_login($ftpid, $user, $pass); if (!$login) return false; $ftpsize = ftp_size($ftpid, $path); ftp_close($ftpid); if ($ftpsize == -1) return false; return $ftpsize; } } Code (markup): -clonepal
ah, great algorithm! but seems it need to fix this row $headers = $this -> my_get_headers($url, 1); PHP: