php send file to remote server using cURL - php

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.

Related

Send image to java service from php

I have a situation, where in my project for file upload, i have to send that image to java service(in multipart format), so that they can upload image from their side.
I have tried sending that image to java service in multiple ways,
I have used curl methods to send file to java service.
But no method worked for me.
If we do File uploading via postman, its hitting java service and successfully uploading the image.
I have mentioned a sample code which i have tried.
If anyone knows how to do this.
$path = 'http://10.10.10.113:8765/cojoin-service/api/image/upload';
$arrInputs = $arrKeyset;
if ( isset($_FILES['fileToUpload']) ) {
$filename = $_FILES['fileToUpload']['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $path);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close ($curl);

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>

PHP cURL setup to send to remote host

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/

How to implement imgur api (image host) on a website?

I came across http://api.imgur.com and thought that would be a usefull tool to use on my website. I then noticed that StackOwerflow uses it too, so it must be good )))
Although I'm struggling when I try to implement it. I took a look at
http://api.imgur.com/examples the PHP section, but it didn't help me much.
What I'm interested in is includin imgur api on my website so users can upload their images. I would than need to store img url/path so I can display it on a website.
e.g. have a form that will allow users to upload photo, then store url/path of the uploaded image in a database (VARCHAR).
Has anyone had a success with this system and could help me understand how to implement it like StackOwerflow uses it (only store image url in database not post).
Code I tried:
<form enctype="multipart/form-data" method="post" action="upload_img.php">
Choose your file here:
<input name="uploaded_file" type="file"/>
<input type="submit" value="Upload It"/>
</form>
upload_img.php
<?
$filename = "image.jpg";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>
I see that you have copy and pasted the example from the imgur API site, which gives the example of a filename which is contained within $filename. You need to point this variable at the file that PHP has uploaded.
Modify the $filename part of your script:
$filename = $_FILES['uploaded_file']['tmp_name'];
Source: POST method file uploading

Want to Transfer Remove image to Image hosting site

I try to find a solution to transfer image to remove server with php..
For Example i have image http://example.com/image.jpg.. and want to transfer this link/file to http://postimage.org or any other image hosting service.. if you have any solution in PHP Please let me know..
EDIT:
After transfer image file its shows me the download link.. Special for http://postimage.org site..
Thanks,
Post image has an upload from url feature:
http://postimage.org/index.php?um=url
Enter the url to your image there and submit.
This is a very open question, but I'll try to answer it.
If you want to upload pictures to a image hosting site, you need to search for one that has an API.
I would recommend imgur for starting.
It has free (limited) access to its API by just registering and it is very simple to use.
Read about it here.
It even has an example for uploading images in PHP:
<?
$filename = "image.jpg";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>

Categories