I've got the following code and it works perfectly fine for uploading one image to Imgur using their API:
$client_id = $myClientId;
$file = file_get_contents($_FILES["file"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
$json = json_decode($json_returned, true);
curl_close ($curl);
However I need to upload multiple images at once. On the client side, the user will have multiple <input type="file" /> fields. I'm completely stuck now with figuring out where and how I will need to modify this code in order to handle multiple image upload when they come through to the server in the form of an array. Does anyone have any ideas?
Change the markup as follows:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="file[]" type="file" multiple="multiple" /><br />
<input type="submit" value="Send files" />
</form>
Now, you can loop through the $_FILES array using a foreach, like so:
foreach ($_FILES['file']['tmp_name'] as $index => $tmpName) {
if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
{
// $tmpName is the file
// code for sending the image to imgur
}
}
Related
i'm trying to use imgur as a uploading backend - i guess it secure my website if there's upload picture (is it right?) so
i went with this way :
<?php
$client_id = 'xxxxx';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
HTML form
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
when i click on submit the result is fine as i guess ^^
Result: {"data":{"id":"TvFtE29","title":null,"description":null,"datetime":1585015712,"type":"image\/png","animated":false,"width":900,"height":940,"size":48902,"views":0,"bandwidth":0,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":0,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"Z9xFH8mrSH8lRDB","name":"","link":"https:\/\/i.imgur.com\/TvFtE29.png"},"success":true,"status":200}
but my problem i want to collect the https://i.imgur.com/TvFtE29.png into specific variable like $uploaded = 'https://i.imgur.com/TvFtE29.png'; to added into my database -> user pic
i went with json_decode but it's not completed with me in the right way, if someone can help with this issue 🌹
thanks.
json_decode worked fine here:
$json_returned = json_decode($json_returned, true);
$uploaded = $json_returned['data']['link'];
I want to upload some pictures with the imgur-API to imgur, but I am always getting an Error 400 Bad Request. Whats the issue?
<?php
if (isset($_POST['uploadprofileimg'])) {
$image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer *MY_ACCESS_TOKEN*\n".
"Content-Type: application/x-www-form-urlencoded",
'content'=>$image
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
$response = file_get_contents($imgurURL, false, $context);
}
?>
<h1>My Account</h1>
<form action="upload-pb.php" method="post" enctype="multipart/form-data">
Upload a profile image:
<input type="file" name="profileimg">
<input type="submit" name="uploadprofileimg" value="Upload Image">
</form>
Looking at the endpoint for /image here, it needs a parameter of image. You're passing it in as content, and not properly encoded as image. Looking here, I borrowed how to properly upload content using stream_context_create/file_get_contents:
<?php
if (isset($_POST['uploadprofileimg'])) {
$image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));
$postdata = http_build_query(
array(
'image' => $image,
)
);
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer *MY_ACCESS_TOKEN*\n".
"Content-Type: application/x-www-form-urlencoded",
'content' => $postdata
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
$response = file_get_contents($imgurURL, false, $context);
Does anyone know how to upload a video to Wistia using PHP cURL from a file upload input specified within a form? This is my code but I cannot seem to get this to work with the current API. I’ve looked at a few similar posts on this subject but still no luck...
<?php
$pathToFile = $_FILES['fileName']['tmp_name']; /* /tmp/filename.mov */
$nameOfFile = $_FILES['fileName']['name']; /* filename.mov */
$data = array(
'api_password' => '********',
'file' => '#'.$pathToFile,
'project_id' => '********'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
?>
UPDATE - FULL CODE (still not working)
cURL returns false for both curl_exec and curl_error when using "'file' => '#'.$pathToFile" but works fine when using "'url' = 'http://example.com/file.mov'... Perhaps I'm not processing the form properly? Here's the full code:
<?php
if ($_POST['submit']) {
$filePath = $_FILES['fileUploaded']['tmp_name'];
$fileName = $_FILES['fileUploaded']['name'];
$fileSize = $_FILES['fileUploaded']['size'];
echo("File Path: ");
echo $filePath;
echo '<br>';
$data = array(
'api_password' => '******',
/* 'url' => 'http://example.com/file.mov', WORKS */
'file' => '#'.$filePath, /* DOES NOT WORK */
'project_id' => '******'
);
// WORKING CMD LINE
// curl -i -F api_password=****** -F file=#file.mov https://upload.wistia.com/
/* cURL */
$chss = curl_init('https://upload.wistia.com');
curl_setopt_array($chss, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => http_build_query($data)
));
// Send the request
$KReresponse = curl_exec($chss);
$KError = curl_error($chss);
// Decode the response
$KReresponseData = json_decode($KReresponse, TRUE);
echo '<br>';
echo("Response:");
print_r($KReresponseData);
echo '<br>';
echo("Error:");
print_r($KError);
}
?>
<form name="upload-form" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" name="fileUploaded">
<input type="submit" name="submit" value="Upload">
</form>
Any help would be much appreciated!
Try this:
$filePath = "#{$_FILES['fileUploaded']['tmp_name']};filename={$_FILES['fileUploaded']['name']};type={$_FILES['fileUploaded']['type']}";
I'm trying to write a very simple test to upload an image to imgur and then redirect to the imgur page once it's done. (haven't gotten to that point yet)
I've looked all around these forums and tried so many different codes posted here, as well as cobbled some together from various posts. Finally, I've stopped getting errors in the php.
But.. in every code I've seen, it displays the image once it's done. Using that exact same code, my image does not appear, all I get is a page with Result: and nothing more. Does anyone have any idea what could be the problem?
<html>
<body>
<form action="upload_img.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
</body>
</html>
and the php file:
<?php
$client_id = '$clientIDHERE';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
Before you close the $curl handle you should check for errors:
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
wondering if anyone can help me. I've been searching for a few days for help on how to publish photos to Facebook using the API. I came across the following script that seems to work for everyone however I am unsure how to connect this to a form where users can select the photo from their hard drive and upload it. Can anyone point me in the right direction?
PHP Code:
$token = $session['access_token'];
$file= 'photo.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Code for the form:
<form action="<?=$PHP_SELF;?>" enctype="multipart/form-data" method="POST">
<input name="MAX_FILE_SIZE" type="hidden" value="10000000" />
<input id="file" name="file" type="file" />
<input name="submit" type="submit" value="Upload" />
</form>
Clark,
The php script receives the file and its details in the $_FILES variable.
For Eg. If you are uploading a file names Image1.jpg then the $_FILES array would have the following values
array(1) {
["file"]=> array(5) {
["name"]=> string(21) "Image1.jpg"
["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(23) "C:\wamp\tmp\phpD1DF.tmp
["error"]=> int(0)
["size"]=> int(355315)
}
}
Here, name = actual file name
type = file type
tmp_name = path of the temp location where the file is uploaded on the server
size = file size
For uploading the file to facebook the values that you should be interested in the "name" and the "tmp_name".
So the arguments that you should send to facebook for the photo upload should look something like this
$args = array(
'message' => 'Photo from application',
);
$args[$_FILES['file']['name']] = '#' . $_FILES['file']['tmp_name'];
I think this should work for you.
Btw, i checked out the facebook doc for photo upload # http://developers.facebook.com/docs/reference/api/photo they say the file name should be passed in the param "source", so if the above arguments dont work for you, you can try
$args = array(
'message' => 'Photo from application',
'source' => '#' . $_FILES['file']['tmp_name']
);
Give it a try :)
Hope this helps.