Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POSTING from PHP

Status
Not open for further replies.

elck

Programmer
Joined
Apr 19, 2004
Messages
176
Location
NL
Code:
print <<<end
<form enctype="multipart/form-data" action="[URL unfurl="true"]http://site.com/chessup.php"[/URL] method="post"  id="pform">
<input type="hidden" name="oldid" value=$oldid >
<input type="hidden" name="link" value=$link >
<input type="hidden" name="title" value='$title' >
<input type="hidden" name="pongid" value=$pongid >
<input name="userfile" type="file"  size=60 value="mypic.jpg">
</form>
<script>document.getElementById("pform").submit()</script>
end;
Now that is the Javascript way of posting some file to another server,I guess.
The question is:
Cannot I do this in a PHP manner?
Using fopen for instance?
Do I have to send a special http header?
 
You need consider the fact that PHP is server side - it cannot upload any files. What it can do is to receive POST data from a form and accept a file posted that way.
Reading the suggested portion of the manual (see dkdude's post) will help.
 
I (think) I understand that PHP is serverside,
yet that is exactly what I want to do: upload a file,
but not to myself but to another server.

The file resides on my server (where the script also resides) and has to be send to another server, that also runs php. (I have access to that other server too)

Can it be done?
 
Yes.

What you would do is make a PHP script that behaves like a browser sending the POST data to the destination.
You have several choices:
a) if you have cURL you can establish a connection to the receiving server using that.
b) PEAR has a HTTP/Request module that allows to send the appropriate headers. Just send the file data along as the data.
c) hand code your socket/stream connection using fsock or stream commands.

Have a look at the HTTP protocol definition to see which headers to send and how to encode data. There are plenty od examples out there.

Cheers.
 
I see -you want to "POST" a file from your server to another server using PHP, right?

AFAIK that's not supported by PHP (yet). But there should be a way to do it anyway. You're looking for socket functions, I recon.

Have a look here :


That's some TCP/IP client/server examples. Maybe you could adapt it to fit your needs.

Another ting - are the two servers on the same LAN (and running Windows)? In that case you simply map a folder and let PHP copy the file to the mapped folder (ie. drive) ....


Jakob §:O)
 
Ok!
I didn't know I had curl, but I do, so I will try this one:

Code:
<?php
$file = "file_to_upload.txt";
$submit_url = "[URL unfurl="true"]http://www.url_to_upload_to.com/upload_page.php";[/URL]
$formvars = array("cc"=>"us \n");
$formvars[variable_1] = "something \n";
$formvars[variable_2] = "something else \n";
$formvars[variable_3] = "more \n";
$formvars[upfile] = "@$file"; // "@" causes cURL to send as file and not string (I believe) 
// init curl handle
   $ch = curl_init($submit_url);
curl_setopt($ch, CURLOPT_REFERER, "[URL unfurl="true"]http://www.last_url_for_referer_logs.com");[/URL]  //if server needs to think this post came from elsewhere
curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
   // perform post
   echo $pnp_result_page = curl_exec($ch);
curl_close ($ch);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top