How to send images from my server to an image hosting server - php

I want to make a PHP script that let user browse image from their computer, then upload it to an image hosting server, and by image hosting server, I mean image hosting services like imageshack.us, photobucket, or blogger.com... I think there are four steps for this:
Display upload form which let user browse image
Upload image to my server
Copy them to image hosting server (use my image hosting account)
Get uploaded image link (from hosting server, not mine)
How ever, I only know what to do with the first and second step, I don't have any idea for the rest. Please give me some suggestion, what should I do to get step 3 and step 4 done, what function should I use, is it possible to have php just open the page and click the button I want to upload image, something like that. Thank you for any help!

For imageshack.us
check http://api.imageshack.us/ and for request the access http://imageshack.us/api_request/
and to send files check this answer https://stackoverflow.com/a/14571212/829533
<?php
$url = 'http://imageshack.us/upload_api.php';
$key = KEY;
$max_file_size = '5242880';
$temp = $_FILES["fileupload"]["tmp_name"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
$post = array(
"fileupload" => '#' . $temp,
"key" => $key,
"format" => 'json',
"max_file_size" => $max_file_size,
"Content-type" => "multipart/form-data"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$json_a=json_decode($response,true);
echo $json_a[links][image_link];
?>
the form will be like
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="fileupload"/>
<input type="submit" value="Go"/>
</form>

According to your question, I found that you have a server, I think that ImageS3 may be an option for your to upload images to.
If you use ImageS3 as your image hosting service, then the steps would be looks like:
Display upload form for users to browse image.
Directly upload image with ImageS3 rest api.
POST image to http://you-server-url/rest/v1/imageplants/<imageplant id>/images/
Get uploaded image (origin or thumbnails) from the ImageS3 rest api.
GET http://you-server-url/rest/v1/imageplants/<imageplant id>/images/imageId?template=
You can check out this project at https://github.com/images3/images3-play, and REST API reference at http://docs.images3api.apiary.io/.
Cheers,

Related

Can't copy images from Tumblr to my site, connection timed out error

Tumblr image example:
https://69.media.tumblr.com/ec3874e541a767ab578495b762201a53/tumblr_ph9je0Akck1r89eh5o1_1280.jpg
Code:
<form method="get">
<input type="text" name="url"/>
<input type="submit" value=" загрузить "/>
</form>
<?php
$name = md5(date('Y-m-d H:i:s').rand(0, 1000));
$folder = 'upload/';
$source = ($_GET['url']);
$dest = $folder.$name.'.png';
copy($source, $dest);
echo 'http://mysite.example/'.$folder.$name.'.png';
?>
I found this in another question on this site:
If you can view the image in a browser that is on the same machine as your program, then it might be that the server won't send the picture unless you look like a user rather than a program. In that case, modifying the browser identification string might fix your problem.
If you cannot view the image from a browser running on the program's PC, you will need to look elsewhere for the source of your problem.
I think, a problem i have is similar to this. Tumblr gives picture to view in a browser, but doesn't allow to copy it with a script.
How to fix that? For example, sites like imgur can upload Tumblr images by url with no any problem.
P.S. For images from other sites copying with this script goes normally.
Addition 01:
As it turned out, the problem is with my site. When i run this code on another site, it works normally with Tumblr images. I have a free domain .ml and free hosting Byethost. I have two guessings. The first is, my domain or hosting is in a blacklist on Tubmlr. The second one, i have some wrong settings on my site. If first guessing is right, is there any way to make it works without changing domain or hosting? If the second is true, what a settings i must check and change?
Tumblr appears to be inspecting the HTTP request and generating different responses depending on how you get it. Your code is fine, as you know, for most sites. When I run it as-is, I get a 403 denied error.
Changing the code to use Curl instead allowed me to download your file. My guess is the default headers used in PHP's copy() are blocked.
<?php
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
$name = md5(date('Y-m-d H:i:s').rand(0, 1000));
$folder = 'upload/';
$source = ($_GET['url']);
$dest = $folder.$name.'.png';
grab_image($source, $dest);
The grab_image() function was a SO answer for another question.

php - curlFile uploading live progressbar

Rrecently I'm creating a telegram bot using telegram Bot API.
My plan is to place a single button in a page for uploading a specific folder's files, to telegram with cUrlFile uploading;
so far, I'm done my work with uploading but my purpose is large-sized files. To do this I need to show user some kind of progress bar that shows user how much MBs or KBs Uploaded.
My upload function is looks something like this:
function sendFile($token,$photosArray,$chatsArray){
$ch = curl_init('https://api.telegram.org/bot'.$token.'/sendDocument');
$cfile = new CURLFile(realpath($photosArray['path']),'image/jpg',$photosArray['name']);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
foreach($chatsArray as $chatId){
$data = array('chat_id' => $chatId, 'document' => $cfile);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res=curl_exec($ch);
}
return $res;
}
For now its works but I don't have any idea how can I get uploaded size from this curl and show it to user. of course i mean real-time progress bar.
In other questions I found questions like :
cURL download progress in PHP
But it's not my answer.
Whats I'm looking for is to do this:
1- Click on a button to upload a folder files
2- Upload one file at a time and show a live progress bar to user
3- When upload is completed, start uploading next file and show another progress bar for it and so on.
Is it needs to implemented by AJAX or something else?
I'm pretty new in php so if someone can help me in a simple way I'll all ears.

PHP Curl Upload Image and Get Image Uploaded Url

Im new in PHP Curl Library , So i don't have alot of experience ,
i have website of funny pics and im tired of uploading images to hostingpics.net and get url then put it in my website , i think there is option in curl upload file to another remote server , ang get link with DOM or something like that !
here is the site to upload image : here
thanks,
This topic answers your question
As suggested, you could try this:
$ch = curl_init("http://www.remotepage.com/upload.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CUROPT_POSTFIELDS, array('fileupload' => '#'.$_FILES['theFile'] ['tmp_name']));
echo curl_exec($ch);
Here is the catch, your website is quite big and complicated. A simple snippet like this won't help you much other than getting an idea about the file upload. You will need to pass your parameters along with the upload like username, password etc.

using CURL to accept file uploads php

I am looking to write a function on one server to accept files uploaded from any other server in other words similar to a api.
Assuming on www.upload.com there is a upload script to upload a file. Instead of doing the post and saving on that server i would like them to curl my script on fileserver.com to save the files to.
My function on fileserver.com to save the file looks like this
function upload($data) {
$uploaddir = '/data/';
$uploadfile = $uploaddir . 'filename.jpg';
if(move_uploaded_file($data['upload_file'], $uploadfile)) {
return 'saved successfully';
} else {
return 'bad file';
}
}
and on the upload.com server i am testing the example with this:
if($_POST) {
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://fileserver.com/upload.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'upload_file=#'.$data['img_file']['tmp_name']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
Please keep in mind that this is intended as a API so the functionality for doing the curl is strictly for testing. the upload function is the api function. Does anyone know how to do this or what I am doing wrong and is this even possible.
Well, actually I have coded something for that: https://github.com/chris-l/urImgAPI
My need was like this:
I had a site, hosted on a virtual machine, and there I had a form that allowed to upload images, but I didn't wanted to waste the bandwidth of the virtual machine by serving images, so I got a cheap shared hosting service (which only had php) for the images and developed a RESTful API on php that is installed there, that allowed me to save images from anywhere by using this API.
The API has a security feature to prevent that a random person could save things on my file server, by requiring a signature. The signature works like this:
$signature = md5(md5sum(your_image_file) . "your secret key");
And you must pass the signature as parameter on the request. That way, you never transmit the secret key and help to prevent that some other person could store images on your file server.
And while I created the API for uploading images, it can easily changed to host any kind of file. (maybe I will change it for that in the future)
Is released under the AGPL3 license, so check it and I hope it could be useful for you.

PHP: Send uploaded file with curl without uploading to initial server

I currently have a site which allows file uploads. When the user submits the form, I run a php script that sends off a curl request to my api.
Currently the php request looks like this:
$params = array(
'media' => "#" . $_FILES['media']['tmp_name']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
And the api just checks the $_FILES field and then grabs media and runs move_uploaded_file.
So the file goes straight from user submitted form to curl to api server, with the file never being actually uploaded (besides being placed in a tmp folder between form submit and curl request) until it hits the api server.
This all works fine for uploading the file to the api server, but the problem is that the server thinks the file's extension is .tmp, as opposed to something like a png, because that's the file the curl request is sending.
How can I send the file without first uploading it pre-curl so that the api server knows what file is actually being sent?
Two ways:
Rename the tmp file. It is accessible to you, so renaming it is trivial: rename(oldname, newname)
Send a mimetype. The format is: #filename;type=image/png for a png.
Prefer the first option if you care about the filename, option two if you only care about the mime type.

Categories