PHP cURL setup to send to remote host - php

I have a few elementary questions about cURL that I can't find answered in the cURL docs (probably because they are obvious to everyone else...). I have a file type input in a form that needs to send that file to a remote server. Does the cURL code go on the page with the form, or is the cURL on the page that the form sends you to, then it gets sent to the remote server?
Here is the form html:
<form action="send.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
The cURL php I have so far which I don't even know if it's correct for what I'm trying to do, or if this goes on the same page or the send.php file the form goes to:
$ch = curl_init("http://remoteServer/upload_file.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CUROPT_POSTFIELDS, array('fileupload' => '#'.$_FILES['theFile'] ['tmp_name']));
echo curl_exec($ch);`
And on the remote server I have this file to receive it:
$folder = "files/";
$path = $folder . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
Is this even remotely close?

you don't need curl to upload a file from a browser, you can submit the form directly to the remote server to upload - just make sure the form submits to whatever file has that third block of code

Try this
$curlPost = array('fileupload' => '#'.$_FILES['theFile'] ['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://remoteServer/upload_file.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec();
curl_close($ch);
Another Example
How to upload image file to remote server with PHP cURL
http://www.maheshchari.com/upload-image-file-to-remote-server-with-php-curl/

Related

php send file to remote server using cURL

I want to send file from local to remote server and after saving file to server I want to output the response. I am using cURL to send and upload file. It is working when I try it on local but not on remote server.
I use sftp protocol with public authentication key for connection.
what I need to change to send file to server.
Here is my code.
$target_url = 'https://example.com/accept.php';
$file_name_with_full_path = realpath('ss.zip');
$post = array('file' => new CurlFile($file_name_with_full_path, 'application/zip' /* MIME-Type */, 'ss.zip'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
If you want to upload images to an external server which is uploaded to your site by a client, you are at the right tutorial.
For this submission we will use 2 files :
form.php – The Page Where we will show the client the form. This file also sends the uploaded data to the external server.
handle.php – The Page on the external server which receives the uploaded data from form.php using cURL.
We won’t copy the uploaded file by the client to our server, instead we will directly send the file to the external server. For sending we will encrypt the file with base64.
OK. Lets’ Start. First, Let’s create the FORM page :
<form enctype="multipart/form-data" encoding='multipart/form-data' method='post' action="form.php">
<input name="uploadedfile" type="file" value="choose">
<input type="submit" value="Upload">
</form>
<?
if ( isset($_FILES['uploadedfile']) ) {
$filename = $_FILES['uploadedfile']['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$POST_DATA = array(
'file' => base64_encode($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://extserver.com/handle.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
echo "<h2>File Uploaded</h2>";
}
?>
Now the code of the handle.php in external server where we sent the data using cURL :
$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
/* Now you can copy the uploaded file to your server. */
file_put_contents('subins', $decoded_file);
The above code will receive the base64 encoded file and it will decode and put the image to its server folder. This might come in handy when you want to have your own user file storage system. This trick is used by ImgUr and other file hosting services like Google.

Uploading .tmp file instead of desired file to server

I am working on uploading a file to box.net.
I am sending the file using Curl,
But the problem i am facing is it does not upload the file I selected, Instead it uploads some .tmp file.
following is my code:
<?php
$upload_url = 'Server-Url';
$tmpfile = $_FILES['new_file1']['tmp_name'];
$_POST['new_file1'] = '#'.$tmpfile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
<form action=""
enctype="multipart/form-data" accept-charset="utf-8" method="POST">
<input type="file" name="new_file1" />
<input type="text" name="share" value="1" />
<input type="submit" name="upload_files" value="Upload File" />
</form>
Am i doing some thing wrong?
Please can any one help me out to solve this problem.
tmp_name is supposed to contain a temporary name. The actual file name is in the name element. See the manual.
However, with the code you have, if you use the name element, you introduce a huge vulnerability into your code, as an attacker may be able to make your script upload local files from your server. Use move_uploaded_file() to prevent that.
As far as I've seen curl does not offer to choose one file yet send a different name for it during upload. Unless you want to craft the whole HTTP request yourself to allow you for this flexibility, you'll have to rename the file on disk to give it the right name. Renaming a file to an arbitrary user-provided name is always a giant security risk though. Below I'm creating a unique temporary directory and am double-checking that the file will be moved into that directory to avoid path injection attacks and to avoid overwriting other files. There may or may not be more attack vectors I'm not thinking about though!
do {
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('upload');
} while (file_exists($tmpDir));
$uploadFile = realpath($tmpDir . DIRECTORY_SEPARATOR . basename($_FILES['new_file1']['name']));
if (strpos($uploadFile, $tmpDir) !== 0) {
trigger_error('File path not within expected directory', E_USER_ERROR);
exit;
}
mkdir($tmpDir, 0600);
move_uploaded_file($_FILES['new_file1']['tmp_name'], $uploadFile);
...
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => "#$uploadFile"));
...
unlink($uploadFile);
rmdir($tmpDir);

Sending File with cURL

I've sent some post data with cURL and am now trying to send a file, getting lost along the way. I'm using a form with a file input. Then would like cURL to send it off to my server. Here is my upload page.
<form action="curl_page.php" method="post" enctype="multipart/form-data">
Filename: <input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="Submit" />
</form>
Here is my curl page with some issues.
<?php
$tmpfile = $_FILES['file']['tmp_name'];
$filename = basename($_FILES['file']['name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://my_server/file_catch.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'uploaded_file' => '#'.$tmpfile.';filename='.$filename,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
?>
The file_catch.php on my server looks like this.
<?php
$folder = "audio/";
$path = $folder . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
Thanks for any help!
You are not sending the file that you got through the post request. Use the below code for posting the file through CURL request.
$tmpfile = $_FILES['image']['tmp_name'];
$filename = basename($_FILES['image']['name']);
$data = array(
'uploaded_file' => '#'.$tmpfile.';filename='.$filename,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// set your other cURL options here (url, etc.)
curl_exec($ch);
For more reference check the below link
Send file via cURL from form POST in PHP

PHP Curl upload file with file browse button

I want to remote upload my file into a server that accept uploads
using curl , but without typing the full file path every time with "#" symbol
can i make a browse button to select files then proceed to curl upload
this is my code ::
$post = array("file_box"=>"#/path/to/myfile.jpg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://remote-site");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$output = curl_exec($ch);
curl_close($ch);
return $output;
just want to change "#/path/to/myfile.jpg" by browse button which passes it's value to php variable
I want to change this [[ $post = array("file_box"=>"#/path/to/myfile.jpg"); ]]
to something like that
[[ $post = array("file_box"=>"#".$variable_contains_file_path_from_browse_button); ]]
to prevent upload the file to middle server(host this script) in the temp path just from the client to the remote server directly
is there any solutions around this
thanks all for any help.
There are several questions in SO which deals with this
here are some of the links
uploading files using curl
Curl php file upload
And also google search strategy is http://bit.ly/PMUf2b
But still since you are asking for a browse button upload from front end, here is the complete set along with the tutor link
you can follow the tutor here which has both front end and php code
upload using curl
Here is the php code
$request_url = ‘http://www.example.com/test.php’;
$post_params['name'] = urlencode(’Test User’);
$post_params['file'] = ‘#’.'demo/testfile.txt’;
$post_params['submit'] = urlencode(’submit’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
curl_close($ch);
here is the html code
<form method=”post” action=”test.php” enctype=”multipart/form-data”>
<input type=”text” name=”name” value=”Test User” />
<input type=”file” name=”file” />
<input type=”submit” name=”submit” value=”submit” />
</form>

my php curl upload dosn't work, display nothing?

i'm trying to test my server, and to do that,
i make a robot to upload one file (in future, i'll make it repeatedly)...
but, the upload file did not send...
here the curl
<?php
$postdata = array();
$postdata ['fieldname'] = "#/home/egy/Downloads/BIO11_0201001D.DBF"; //fieldname should be same as file input box name
$post_url = 'form-handler.php'; //url to upload file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$response = curl_exec($ch);
echo $response;
?>
and this is the handler:
<?php
if(move_uploaded_file($_FILES['fieldname']['tmp_name'], "upload"))
{
echo "success";
}
?>
in my php, i set display_error = on and error_reporting = E_ALL
my DBF never send to server, but there is no error or warning....
am i doing wrong?
NB: Sorry for my english
This is likely becuase you need to use $_FILES['fieldname']['name'] to get what the name was in the file input box named fieldname
Did you make sure you had an enctype in your form. Mistake a lot of people make, including my self.
<form method="post" action="" enctype="multipart/form-data">
Assuming you are trying to upload a file local to the box the script is running from.. see this stackoverflow question

Categories