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);
Related
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.
I'm trying to load and save a remote PDF to my server for a project, but the link has no file extension. It's a kind of secured link.
https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803
When I open the link in the browser I can download the PDF file, but saving it with my script has no success.
Is there a way to save the PDF on my server with a script?
I tried following code without success:
$url ="https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803";
getFileContents($url);
function getFileContents($url)
{
// Workaround: Save temp file
$img = tempnam(sys_get_temp_dir(), 'pdf-');
$img .= '.' . pathinfo($url, PATHINFO_EXTENSION);
$fp = fopen($img, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $result ? $img : false;
}
I found the script here : Downloading a large file using curl
To download a file (binary or not) you can use file_get_contents().
file_get_contents return the file content into a string.
//Download PDF content
$pdfContent = file_get_contents("https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=XXXXXXX-DDDD-4444-AAAA-XXXXXXX&LINK_PAGE=ITINERARYENC&TRANS_ID=AAAAAAAA-3333-5555-6666-222222222");
$fileName = "myPDF.pdf";
$fp = fopen($fileName, 'w+');
//Write content into the file
fwrite($fp, $pdfContent);
fclose($fp);
You can if necessary use options in file_get_contents to specify GET or POST method, Basic Authentication and more.
Thanks for your reply, but your code doesn't saves the PDF im looking for.
It safes a PDF with HTML content :
WWW.ENABLELOGISTICS Enable Logistics v95.4248
Welcome to Enable
Logistics Please enter your username and password to sign in.
Forgot Password?©2016 Bright People Technologies Pty Ltd
I have no clue how to dowload the pdf to my server.
Clicking the link only let's me download local.
I am trying to upload images from client to server. My Client and server machines are separate so I need to send the image data from client to server.
My Client side php code goes like (Full client-side code):
$filename = $_FILES["fileToUpload"]["name"];
$filedata = $_FILES['fileToUpload']['tmp_name'];
$imagedata = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$fields = array(
'filename' => $filename,
'filedata' => "#$filedata"
'imagedata' => "#$imagedata"
);
$field_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://10.2.16.102/temp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $field_string);
$server_output = curl_exec ($ch);
curl_close ($ch);
and my Server side code is (Full server-side code):
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
echo " and the filename is ". $_POST['filename'];
$uploadpath = "/uploads/";
$target_file = $_SERVER['DOCUMENT_ROOT']. $uploadpath . $_POST['filename'];
file_put_contents($target_file, $_POST['imagedata']);
//move_uploaded_file($_POST['filedata'],$target_file);
//copy($_POST['filedata'],$target_file);
?>
I am able to store the image in my uploads folder and the filesize is also the same but I am not able to see the image. The image viewer says "Couldn't load the png file". Do I have to convert the file into png format before storing. Also how to do that? Any help?
In a nutshell, I need a converter which can convert the file format into png or any other image format which can convert my file into an image at the server end.
Use $_FILES instead of $_POST in move_uploaded_file function e.g:
move_upload_file($_FILES[filedata][tmp_name], YOUR_PATH/$_FILES[filedata][name]);
Here's PHP doc:
http://php.net/manual/fr/function.move-uploaded-file.php
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);
?>
I followed the google api examples for uploading to youtube. However if i want to upload a file that hosted on another domain for example:
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
// create a new Zend_Gdata_App_MediaFileSource object
$filesource = $yt->newMediaFileSource('http://www.myotherdomain.com/.../file.mov');
$filesource->setContentType('video/x-flv');
// set slug header
$filesource->setSlug('http://www.myotherdomain.com/.../file.mov');
// add the filesource to the video entry
$myVideoEntry->setMediaSource($filesource);
Any help would be greatly appreciated
The Zend API probably doesn't support this. However, if you have write-permissions, you can download the file on your server and then send it.
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_URL, 'http://www.myotherdomain.com/.../file.mov');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
curl_close($curl);
$file = fopen('temp/file.mov', 'w'); // If file is not found, attempts to create it
fwrite($file, $contents);
fclose($file);
// Upload via YouTube