with html forms we can upload a file from a client to a server with enctype="multipart/form-data"
, input type="file"
and so on.
Is there a way to have a file already ON the server and transfer it to another server the same way?
Thanks for hints.
// WoW! This is the fastest question answering page i have ever seen!!
This is a simple PHP script I frequently use for while moving big files from server to servers.
set_time_limit(0); //Unlimited max execution time
$path = 'newfile.zip';
$url = 'http://example.com/oldfile.zip';
$newfname = $path;
echo 'Starting Download!<br>';
$file = fopen ($url, "rb");
if($file) {
$newf = fopen ($newfname, "wb");
if($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
echo '1 MB File Chunk Written!<br>';
}
}
if($file) {
fclose($file);
}
if($newf) {
fclose($newf);
}
echo 'Finished!';
When the browser is uploading a file to the server, it sends an HTTP POST request, that contains the file's content.
You'll have to replicate that.
With PHP, the simplest (or, at least, most used) solution is probably to work with curl
.
If you take a look at the list of options you can set with curl_setopt
, you'll see this one : CURLOPT_POSTFIELDS
(quoting) :
The full data to post in a HTTP "POST" operation.
To post a file, prepend a filename with @ and use the full path.
This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value.
If value is an array, the Content-Type header will be set to multipart/form-data.
Not tested,but I suppose that something like this should do the trick -- or at least
help you get started :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => '@/..../file.jpg',
// you'll have to change the name, here, I suppose
// some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);
Basically, you :
curl_exec
to return the result, and not output itPOST
, and not GET
@
before the file's path.you can do it in the same way. Just this time your server who received the file first is the client and the second server is your server. Try using these:
For the webpage on the second server:
<form>
<input type="text" name="var1" />
<input type="text" name="var2" />
<input type="file" name="inputname" />
<input type="submit" />
</form>
And as a script to send the file on the first server:
<?php
function PostToHost($host, $port, $path, $postdata, $filedata) {
$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";
}
$data .= "--$boundary\n";
// Sammeln der FILE Daten
$data .= "Content-Disposition: form-data; name=\"{$filedata[0]}\"; filename=\"{$filedata[1]}\"\n";
$data .= "Content-Type: image/jpeg\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $filedata[2]."\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'=>'test', 'var2'=>'test');
$data = file_get_contents('Signatur.jpg');
$filedata = array('inputname', 'filename.jpg', $data);
echo PostToHost ("localhost", 80, "/test3.php", $postdata, $filedata);
?>
Both scripts are take from here: http://www.coder-wiki.de/HowTos/PHP-POST-Request-Datei
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With