Skip to content Skip to sidebar Skip to footer

Http Transfer File From Server To Server

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

Solution 1:

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&para2=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 leasthelp 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 :

  • are using curl
  • have to set the destination URL
  • indicate you want curl_exec to return the result, and not output it
  • are using POST, and not GET
  • are posting some data, including a file -- note the @ before the file's path.

Solution 2:

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>
         <inputtype="text" name="var1" />
         <inputtype="text" name="var2" />
         <inputtype="file" name="inputname" />
         <inputtype="submit" />
  </form>

And as a script to send the file on the first server:

<?phpfunctionPostToHost($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 Datenforeach($postdataas$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 Antwortwhile(!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

Solution 3:

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!';

Solution 4:

Ex. if you have a file called mypicture.gif on server A and want to send it to server B, you can use CURL.

  1. Server A reads the content as a String.
  2. Post the string with CURL to Server B
  3. Server B fetches the String and stores it as a file called mypictyre-copy.gif

See http://php.net/manual/en/book.curl.php

Some sample PHP code:

<?php$ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">$post = array(
        "file_box"=>"@/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>

Solution 5:

It is Very easy.This is very easy & simple code

<html><body><?php/*the following 2 lines are not mandatory but we keep it to 
avoid risk of exceeding default execution time and mamory*/
ini_set('max_execution_time', 0);
ini_set('memory_limit', '2048M');

/*url of zipped file at old server*/$file = 'http://i.ytimg.com/vi/Xp0DOC6nW4E/maxresdefault.jpg';

/*what should it name at new server*/$dest = 'files.jpg';

/*get file contents and create same file here at new server*/$data = file_get_contents($file);
$handle = fopen($dest,"wb");
fwrite($handle, $data);
fclose($handle);
echo'Copied Successfully.';
?></body></html>


Type your Url in this variable - $file ="http://file-url"

Type your file save as in hosting sever- $dest = 'files.jpg';

Post a Comment for "Http Transfer File From Server To Server"