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
Related
I've got a website A that holds an image, and I am now using PHP to grab that image and I want to post it via form-data media to another website.
My solution now is to save the image as a file and then use CURLFile to auto-generating these form-media post fields.
Is there a way that I don't need to save this image to my local file system?
What I am doing now:
$img = urlcurl::geturl($this->url); // seems to be an raw image data (png?)
$fp = fopen('./tmp/1.png','x');
fwrite($fp, $img);
$t = new CURLFile("/tmp/1.png","image/png"); // get from local file storage
$filedata = array("media" => $t);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$response = curl_exec($ch);
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);
I have a site that I am working on and it uses the imgur api to upload user's profile picture, but it needs to be base64 to use the function I have:
function imgur($image) {
$client_id = "client_id_some_numbers_and_stuff";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
$link = $reply->data->link;
return $link;
}
I get the image from a form input (<input type="file" id="pp" name="pp">). When I get the data from $_POST["pp"], it is set to the string of the filename, and nothing useful.
How can I get the image file and either pass it into the imgur() function, or convert it into base64 and then pass that into the imgur() function, and just remove the base64 encoding from the function.
Thanks :)
Edit: This post is different that the one marked as duplicate becuase I want to know how to send the form data elsewhere, and not have it uploaded to my site.
info of upload file contains $_FILES array
you need to get your upload file path in server and translate it into base64
$image = $_FILES["pp"]["tmp_name"];
$type = pathinfo($image, PATHINFO_EXTENSION);
$data = file_get_contents($image);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
imgur($base64);
I'm not allowed to use file_get_contents. Originally I got the file contents by simply doing this:
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filecontent = file_get_contents($filepath);
$encodedFile = base64_encode($filecontent);
Unfortunately, it's not allowed.
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filename = $_FILES['resume-attachment']['name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $filepath);
$filecontent = curl_exec($ch);
curl_close($ch);
$encodedFile = base64_encode($filecontent);
The code above is my attempt at using cURL. I get the filename uploaded, so I get some reaction, but the uploaded file is 0.0 bytes of size... which is not correct. I would think that perhaps the issue could be that I shouldn't treat $filepath as a URL, but what would the alternative be? I should also mention that I'm not trying to post it from this code. I simply want to get the file contents, and then encode it. It's part of an XML string later on.
I have the follow code:
$image = imagecreatefromstring(file_get_contents('http://externaldomain.com/image.png'));
And want to make a curl request:
$files = array();
$files[] = '#' . $image['tmp_name'] . ';filename=' . $image['name'] . ';type=' . $image['type'];
$ch = curl_init('index.php?route=upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
The main problem is, how to get the file name and extension from $image variable?
I can replace $image['tmp_name'] with tempnam(), but other things?
Have another way to do?
imagecreatefromstring returns a GD handle. There is no tmp_name, name, OR type associated with it. You're trying to treat that GD handle as if it was the result of a standard POST-based $_FILES data structure.
a GD handle cannot be passed to curl for upload, because it's not really an image, and won't be until you use imagejpeg/imagepng/imagewhatever to save it out to a file, which you can then point curl at, e.g.
$image = imagecreatefromstring(...);
imagejpeg($image, 'tempfile.jpg');
$files[] = '#tempfile.jpg;filename=tempfile.jpg;type=image/jpeg';
curl...