1

I'm using the following tutorial to upload an image to imgur. I want to know whether it's necessary to have base64_encode when sending the data.

Here's a small code snippet:

$img=$_FILES['img'];
$filename = $img['tmp_name'];
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data)); // Here's the base64_encode
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);

Is it necessary to have base64_encode and why?

To see the full code, you can go here.

userlond
  • 3,632
  • 2
  • 36
  • 53
Horay
  • 1,388
  • 2
  • 19
  • 36
  • 2
    Possible duplicate of [What is base 64 encoding used for?](http://stackoverflow.com/questions/201479/what-is-base-64-encoding-used-for) – userlond Nov 20 '15 at 00:39

2 Answers2

1

According to the Imgur documentation, there are alternative methods.

enter image description here

So, to answer your question, no it is not required as long as it is one of the other two supported options.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • 1
    Another important note is base64 encoding something increases the size by 4/3, so binary should be preferred if possible, especially if the 10MB limit is on the base64 encoded string. – Mike Nov 20 '15 at 00:53
  • Which one is recommended? – Horay Nov 20 '15 at 00:58
  • Oh... Thanks! So I can just replace `$pvars` with `$data`? – Horay Nov 20 '15 at 01:23
  • I don't think cURL will do it like that directly, take a look at this: http://ryansechrest.com/2012/07/send-and-receive-binary-files-using-php-and-curl/. Out of curiosity, what is the opposition to base64? Is the file to big? – Jeremy Harris Nov 20 '15 at 01:27
  • @cillosis 2 things. 1st: I will be passing either an image or a url. I don't think I can do base64 to a url. 2nd: The user will be able to upload a larger file. Which leads to another question. If I do base64, then how can I know how many megabits to limit the user? – Horay Nov 20 '15 at 01:32
  • Well, you can give a URL no problem to imgur according to their docs. As for calculating the size of base64 encoded, check this out: http://stackoverflow.com/questions/4715415/base64-what-is-the-worst-possible-increase-in-space-usage – Jeremy Harris Nov 20 '15 at 01:33
  • Right, but I don't think I can give a base64 URL. – Horay Nov 20 '15 at 01:36
  • Right. That's an option. So are you suggesting I use base64? – Horay Nov 20 '15 at 01:37
  • I'm suggesting it would be *easier*. Feel free to play with cURL and see if you can get the binary post to go through as that would increase the maximum file size you can support, but it can be tricky is all I am saying. – Jeremy Harris Nov 20 '15 at 01:38
  • Got it! Thanks! Feel free not to answer the next question because it ins't in the scope of the original question. I'm trying to figure out the maximum file size for anonymous uploads (for GIFs and other formats (like (jpg)). The image in your answer says it's 10 mb. But in https://help.imgur.com/hc/en-us/articles/201476437-What-is-the-maximum-file-size-for-uploads- it says 1mb for regular images, and 200mb for GIFs. Which one is it? (I'm using anonymous uploads only.) – Horay Nov 20 '15 at 01:47
  • My answer was for the API which, I assume, means you need an account. The article you linked to might be more accurate for anonymous uploads. As always, give it a try and see ;-) – Jeremy Harris Nov 20 '15 at 01:50
  • 1 more question. If it works with a couple of uploads without base64, does that mean it will always work? – Horay Nov 20 '15 at 02:41
  • This is IT, it will never *always* work! Heh. Be sure to check the return value of curl for false and handle the error available using `curl_error($curl)` appropriately. – Jeremy Harris Nov 20 '15 at 02:42
0

No it´s not necessary, and I don't know if this is a common practice for posting huge images.

You can also use the cURL option CURLOPT_INFILE

BEWARE: The below examples, may not work, they are just untestet examples.

// This is the entire file that was uploaded to a temp location.
$localFile = $_FILES[$fileKey]['tmp_name']; 

$fp = fopen($localFile, 'r');

// Connecting to website.
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $target_url );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;

Or just a simple POST

$post = array('extra_info' => '123456','file_contents'=>'@'.$_FILES['upload']['tmp_name']);

$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;
swidmann
  • 2,787
  • 1
  • 18
  • 32