How to use php upload file from host to another host?

Discussion in 'PHP' started by zeroonea, Mar 26, 2007.

  1. #1
    - Description: i have a file test.zip at here: http://host_1.com/test.zip
    - I have a php host at here: http://host_2.com
    - And i need a php code put in host_2.com to upload file test.zip to http://host_3.com/upload_process.php
    - upload_process.php is php file, it process file upload (HTTP POST)

    - With some knowledge about php, i think use fsockopen, read file test.zip at http://host_1.com/test.zip, send some header, and send it to http://host_3.com/upload_process.php as same as when use html form post to upload file normally

    - But i don't know in detail how to do it?
    - Can anyone help me?
    - Thanks.
     
    zeroonea, Mar 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Mar 26, 2007 IP
  3. zeroonea

    zeroonea Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    - Target host is not mine
    - And can you give me some detail or example to do it with curl?
     
    zeroonea, Mar 26, 2007 IP
  4. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just follow the links nico provided and you'll get the help you need. You should use FTP to do this.
     
    klown, Mar 26, 2007 IP
  5. zeroonea

    zeroonea Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    @nico_swd, klown: Thanks, i founded it, thanks.
     
    zeroonea, Mar 26, 2007 IP
  6. zeroonea

    zeroonea Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    - I found a way to upload file, but it only do well with local file:

    
    $post_data = array();
      
    //$post_data['file'] = "http://mirror2.internetdownloadmanager.com/idman509.exe";
    
    $post_data['file'] = "@test.zip";
      
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://host.com/upload_process.php" );
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $postResult = curl_exec($ch);
    
    if (curl_errno($ch)) {
     print curl_error($ch);
    }
    curl_close($ch);
    print "$postResult";
    
    Code (markup):
    - I have a idea is transfer file need upload to host and upload load it as local file, but seem it waste a lot of bw.

    - any suggestion? Thanks
     
    zeroonea, Mar 26, 2007 IP
  7. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #7
    use ftp......
     
    klown, Mar 26, 2007 IP
  8. zeroonea

    zeroonea Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    {@:

    - Target host is not mine, i don't have ftp account >.<

    :mad:}
     
    zeroonea, Mar 26, 2007 IP
  9. kids

    kids Active Member

    Messages:
    411
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    68
    #9
    You can do:
    1. Create a finction like this:
    function url_copy($source)
    {
    	$d=explode("/",$source);
    	$dest=$d[count($d)-1];
    	$f1=fopen($source,"rb");
    	$f2=fopen($dest,"w");
    	echo "<SCRIPT>window.status='Copying \'$source\' to \'$dest\'...';</SCRIPT>\n";
    	flush();
    	while ($buff=fread($f1,1024))
    	fwrite($f2,$buff);
    	fclose($f1);
    	fclose($f2);
    	$size=filesize($dest);
    	echo "<SCRIPT>window.status='$size bytes copied.';</SCRIPT>\n";
    }
    PHP:
    2. And do as you wish!
     
    kids, Apr 9, 2007 IP