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 :
- 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…