I came across http://api.imgur.com and thought that would be a usefull tool to use on my website. I then noticed that StackOwerflow uses it too, so it must be good )))
Although I'm struggling when I try to implement it. I took a look at
http://api.imgur.com/examples the PHP section, but it didn't help me much.
What I'm interested in is includin imgur api on my website so users can upload their images. I would than need to store img url/path so I can display it on a website.
e.g. have a form that will allow users to upload photo, then store url/path of the uploaded image in a database (VARCHAR).
Has anyone had a success with this system and could help me understand how to implement it like StackOwerflow uses it (only store image url in database not post).
Code I tried:
<form enctype="multipart/form-data" method="post" action="upload_img.php">
Choose your file here:
<input name="uploaded_file" type="file"/>
<input type="submit" value="Upload It"/>
</form>
upload_img.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 see that you have copy and pasted the example from the imgur API site, which gives the example of a filename which is contained within $filename. You need to point this variable at the file that PHP has uploaded.
Modify the $filename part of your script:
$filename = $_FILES['uploaded_file']['tmp_name'];
Source: POST method file uploading
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 am working on uploading a file to box.net.
I am sending the file using Curl,
But the problem i am facing is it does not upload the file I selected, Instead it uploads some .tmp file.
following is my code:
<?php
$upload_url = 'Server-Url';
$tmpfile = $_FILES['new_file1']['tmp_name'];
$_POST['new_file1'] = '#'.$tmpfile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
<form action=""
enctype="multipart/form-data" accept-charset="utf-8" method="POST">
<input type="file" name="new_file1" />
<input type="text" name="share" value="1" />
<input type="submit" name="upload_files" value="Upload File" />
</form>
Am i doing some thing wrong?
Please can any one help me out to solve this problem.
tmp_name is supposed to contain a temporary name. The actual file name is in the name element. See the manual.
However, with the code you have, if you use the name element, you introduce a huge vulnerability into your code, as an attacker may be able to make your script upload local files from your server. Use move_uploaded_file() to prevent that.
As far as I've seen curl does not offer to choose one file yet send a different name for it during upload. Unless you want to craft the whole HTTP request yourself to allow you for this flexibility, you'll have to rename the file on disk to give it the right name. Renaming a file to an arbitrary user-provided name is always a giant security risk though. Below I'm creating a unique temporary directory and am double-checking that the file will be moved into that directory to avoid path injection attacks and to avoid overwriting other files. There may or may not be more attack vectors I'm not thinking about though!
do {
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('upload');
} while (file_exists($tmpDir));
$uploadFile = realpath($tmpDir . DIRECTORY_SEPARATOR . basename($_FILES['new_file1']['name']));
if (strpos($uploadFile, $tmpDir) !== 0) {
trigger_error('File path not within expected directory', E_USER_ERROR);
exit;
}
mkdir($tmpDir, 0600);
move_uploaded_file($_FILES['new_file1']['tmp_name'], $uploadFile);
...
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => "#$uploadFile"));
...
unlink($uploadFile);
rmdir($tmpDir);
PHP:
Any ideas where I could find a script similar to what stackoverflow uses? Or would it be easy to make something like that myself? I'm sure downloading the image is not a problem, but I'm more worried about security. I'm building an user avatar upload/remote upload system.
Jquery:
The reason I added jquery to the tags, perhaps it is possible to let the user point the URL of the image and somehow upload it via the normal file upload input himself (without having to manually download the image to the computer first)
You can use cURL to download the image and then use getimagesize() to check whether it's actually an image - for security purposes.
<?php
$limit = 1024*1024*10 // Max. file size in bytes (1024*1024*10 = 10MB)
$ch = curl_init();
$fh = fopen('image.jpg', 'w');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_RANGE, '0-' . $limit);
curl_exec($ch);
curl_close($ch);
if ($image = getimagesize ("image.jpg")) {
// It's an image
}
else {
// Not an image; delete!
}
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 have a question about the imgur api.
I want to create a gallery for my website using the imgur api, but how can i create a file uploader that uploads to the imgur servers?
Here is what i created:
<?php
include 'xmlparser.php'; // From http://www.criticaldevelopment.net/xml/doc.php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$data = file_get_contents($_FILES["file"]['tmp_name']);
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => HERE_MY_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);
$parser = new XMLParser($xml);
$parser->Parse();
echo $parser->images->item->links->original;
curl_close ($curl);
}
else
{
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
But this doesn't seem to work...?
I get this error:
Parse error: syntax error, unexpected T_STRING, expecting ')' in C:\data\home\www\test.php on line 7
And line 7 is this row:
$pvars = array('image' => base64_encode($data), 'key' => HERE_MY_API_KEY);
What is wrong?
The documentation of the imgur api is here:
http://api.imgur.com/examples
Can you guys help me?
And yes, i already searched through these topics:
HTML Upload Form will only upload files found in the directory of the PHP file
Using jQuery to parse XML returned from PHP script (imgur.com API)
But it didn't help me...
Greetings
Put the API key in quote marks. The way they put it in all caps and outside quote marks is to signify a constant value.
Make sure you register for anonymous API and not Authenticated API here http://imgur.com/register/api
Pulled my hair off for this