PHP Curl Upload Image and Get Image Uploaded Url - php

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.

Related

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.

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

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,

Saving thumbnails images to certain directory

I'm willing to use thumbnails into my website which is mainly like websites directory.
I've been thinking to save url thumbnails into certain directory !
Example :-
I'm going to use free websites thumbnails service that gives me code to show thumbnail image of any URL as follow
<img src='http://thumbnails_provider.com/code=MY_ID&url=ANY_SITE.COM'/>
This would show the thumbnail of ANY_SITE.COM
i want to save the generate thumbnail image into certain directory my_site.com/thumbnails
Why i'm doing this ?
in fact my database table is like my_table {id,url,image} where i'm going to give the image thumbnail random name and store its new name into my_table related to its url then i can call it back anytime and i know how to do it but i don't know how to save it into certain directory.
any help ~thanks
Using cURL should work for you:
$file = 'the URL';
$ch = curl_init ($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
$fullpath = 'path to destination';
$fp = fopen($fullpath);
fwrite($fp, $rawdata);
fclose($fp);
You could use curl to fetch the remote image. You can save it with curl_setopt($handler, CURLOPT_FILE, '/my/image/path/here.jpg');. The id could be something simple like a hash of the original URL. Obviously you'd have to check to make sure the directories exist before you save the file (using is_dir() and creating them with mkdir() if they don't).

Grab frame without downloading whole file?

Is this possible using php + ffmpeg?
ffmpeg-php has the ability to:
Ability to grab frames from movie files and return them as images that
can be manipulated using PHP's built-in image functions. This is great
for automatically creating thumbnails for movie files.
I just don't want to download the whole file before doing so.
So lets say i want to grab a frame # 10% of the movie:
First lets get the size of remote file:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url); //specify the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$size = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD);
Then it's quite easy to download only 10% of the .flv or .mov file using curl.
But the framegrab trick using ffmpeg-php probably won't work because the file probably is corrupted?
Any other ideas?
Yes I believe this will work. For video files as long as you do have the start of the file, processing like this should be possible. (If you only had, for example, a chunk of the file from the middle, it probably wouldn't work.)
On the command line I downloaded the first part of an .FLV file with Curl, then grabbed frames using ffmpeg and it worked correctly. Doing the same in PHP should work as well.

Using cURL to save external files to my Server

I have a website to show opensource movies and videos.
I have saved urls in mysql and linked both videos as well as the images to the content server.
But users are complaining of slow website as images are getting fetched from outside and most of time Internet Explorer is not even displaying the image.
I just learnt about cURL and would like to save images as well as videos to my own server and provide mirror to original website.
I got " curl -O ('') ; " syntax at many places to do the task but don't know how to use it inside my php script.
In short:
I already have my form for url saving in mysql. I wish it to also save save file to a directory on my webserver and save file path to another column in mysql.
Any sort of help is welcome.
Thanx in Advance
$local_file = "/tmp/filename.flv";//This is the file where we save the information
$remote_file = "http://www.test.com/filename.flv"; //Here is the file we are downloading
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
I've decided to update this answer almost 7 years later.
For those who have copy() enabled for remote hosts, you can simply use:
copy("http://www.test.com/filename.flv", "/some/local/path/filename.flv");

Categories