问题
I have an Apache server running on a client's computer, with PHP installed. I have scheduled a task to automatically create a database backup, and it's working fine. But it makes no sense keeping the backups on the same HD the system's running, so I have to send it somewhere else.
At first, I tried doing a PHP FTP Upload, but the client's firewall's blocking all FTP connections, and I cannot unblock it (his company won't allow me).
Then, I tried sending the backup using SMTP, to an e-mail account. Also didn't work. All SMTP connections are also blocked (I know...).
I'm trying now to access a webpage (on my server), via a HTTP POST REQUEST, with the file attached on the page-header. It should be possible, seeing that's pretty much what the browser does, with a file-input object, right? I just sends the multipart/data using the page header.
Will I have to create the page header manually? Or are there any scripts that already do that?
回答1:
You can use curl to send it via http. Assuming your file is '/tmp/backup.tar.gz', it'd be:
$ch = curl_init('http://clientserver.com/upload.php');
$ch = curl_setopt($ch, CURLOPT_POST, true);
$ch = curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/tmp/backup.tar.gz'));
$ch = curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
$result = curl_exec($ch);
if ($result === FALSE) {
die(curl_error($ch));
}
That's the basics. you'd probably need to make it a bit more robust. Assuming the upload script on the receiving server is done in php, you'd access the file as $_FILES['file'].
回答2:
You can use this script to generate a POST request with file upload(use Content-Type: multipart/form-data) on the server side.
On the receiving server, you can use another script to load the file via POST request.
But don't forget that file uploads are usually a weak place in web application security, so read this pdf before applying your code on production server.
UPDATE:
After thinking for a minute I realized that simple php copy function is also a good solution for transmitting a file from one server to another! That will work if using POST request is not a must for you.
This approach has two advantages:
1) It's more simple
2) The file transfer can be done after the post request, downloading the file asynchrounously
3) Can be implemented if the transmitted file is dynamically generated
4) The server can ask for the file once more if the connection fails
<?php
//you can run this code in a separate process
//don't forget to urlencode the file name if it's not encoded yet
$remote = urlencode( $_POST['filename'] );
$local = "tempdir_outside_your_http_root/local_file.dat";
if(!copy($remote, $local)){
throw ....
}
if(check_for_security($local)){
//if all is ok, move to the desired folder
$final = "uploads/safe_file.dat";
rename($local, $final);
}else{
unlink($local);
}
//PS: this code is an example, it has strong security vulnerabilities
//use single-use tokens so that $_POST['filename'] could not be faked
?>
回答3:
You could also use scp to copy it over, that is what I use. You could set it to copy automatically using cron too.
来源:https://stackoverflow.com/questions/7690086/uploading-file-from-one-server-to-another-via-http-post