Need to transfer files from http url to ftp folder or local

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Need to transfer files from http url to ftp folder or local

Post by danjapro »

I got this script:

But it give me error, file_get_contents cannot open stream.

I need to add the FTP connection with user/pass paramaters.

then look in set http url, to get the file contents(images) and transfer to ftp server location.

Can Anyone take alook and tell me if I am going down the right path and how to get there. Please

Code: Select all

function postToHost($host, $port, $path, $postdata = array(), $filedata = array()) {
	$data = "";
	$boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
	$fp = fsockopen($host, $port);
	
	fputs($fp, "POST $path HTTP/1.0\n");
	fputs($fp, "Host: $host\n");
	fputs($fp, "Content-type: multipart/form-data; boundary=".$boundary."\n");
	
	// Ab dieser Stelle sammeln wir erstmal alle Daten in einem String
	// Sammeln der POST Daten
	foreach($postdata as $key => $val){
		$data .= "--$boundary\n";
		$data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
	}
	
	// Sammeln der FILE Daten
	if($filedata) {
		$data .= "--$boundary\n";
		$data .= "Content-Disposition: form-data; name=\"".$filedata['name']."\"; filename=\"".$filedata['name']."\"\n";
		$data .= "Content-Type: ".$filedata['type']."\n";
		$data .= "Content-Transfer-Encoding: binary\n\n";
		$data .= $filedata['data']."\n";
		$data .= "--$boundary--\n";
	}
	
	// Senden aller Informationen
	fputs($fp, "Content-length: ".strlen($data)."\n\n");
	fputs($fp, $data);
	
	// Auslesen der Antwort
	while(!feof($fp)) {
		$res .= fread($fp, 1);
	}
	fclose($fp);
	
	return $res;
}

$postdata = array('var1'=>'today', 'var2'=>'yesterday');
$filedata = array(
	'type' => 'image/png',
	'data' => file_get_contents('http://xxx/tdr-images/images/mapping/dynamic/deals/spot_map')
	);

echo '<pre>'.postToHost ("localhost", 80, "/test3.php", $postdata, $filedata).'</pre>';


User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need to transfer files from http url to ftp folder or lo

Post by Celauran »

Have you checked if allow_url_fopen is on?
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: Need to transfer files from http url to ftp folder or lo

Post by danjapro »

Yes, it is, there is another script, I am using to call each image from that directory and echo to page for dispaly.

Can anyone, help me here. I am lost with this one.
Post Reply