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);
Related
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 am using cURL for the first time. I have to send one image file and one audio file posted by the user.
My cURL code is working, but instead of an image file and an audio file my code is sending a .tmp file.
I googled it, but in every example I found they have used realpath of file directly.
I tried to find real path of the file, but I didn't find any solution.
Here is my code block in which I am collecting all data in an array to pass it to cURL:
$name = $_POST['name'];
$image = $_POST['image']['name'];
$imagetmp = $_POST['image']['tmp_name'];
$imagesize = $_POST['image']['size'];
$imagepath = '#'.$imagetmp;
$audio = $_POST['audio']['name'];
$audiotmp = $_POST['audio']['tmp_name'];
$audiosize = $_POST['audio']['size'];
$audiopath = '#'.$audiotmp;
$data = array("name" => $name, "image"=> $imagepath, "audio" => $audiopath); //array to sned data using cURL
//my cURL code to post data
Where am I doing wrong? How to send files using cURL?
This is what I used to post file data:
$filedata = file_get_contents('file_location/filename.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($ch);
curl_close($ch);
You can use file=#path with curl
curl -kiSs -X POST https://<domain>/path/to/api/files/ \
-F "param1=param2" \
-F "file=#/path/to/test.xlsx" \
-H "Authorization":"bearer eyJhbGciOiJIUzI1NiIsIn"
File successfully uploaded (test.xlsx!)
i am using yii boilerplate and set /pimages as web alias for backend images directory , i am using this alias for getting frontend images
Now my problem statement is , i want to copy image of specific user when use login my site my site using facebook , i am using below code to get image of the user .
First method using file get content is
$facebookimage = "http://graph.facebook.com/" . $facebook_id . "/picture/?type=large";
$image = #file_put_contents(Yii::app()->request->baseUrl . '/pimages/user/' , file_get_contents($facebookimage));
and other method using curl is below but i really cant understand this that why the author is using myimg123.jpg in example
$path = Yii::app()->request->baseUrl . '/pimages/user/'; // your saving path
$headers = get_headers($profile_Image, 1);
$profile_Image = $headers['Location'];
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
try to use curl with the following option:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
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 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...