php - curlFile uploading live progressbar - php

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.

Related

Check if a url offers a downloadable file?

I am currently working on some affiliate feeds where most are offered as raw .csv formats. I am using file_get_contents to generate .csv files along with fputcsv().
Unfortunately there is also a link between my affiliate url's that instantly downloads a csv file when you visit the url in the browser. This needs no further work since it's a perfect .csv file as is.
Since I just put my url's in a array I need to check for when a file is offered as a download link. How can I check for this so I can skip all my default .csv logic and not mess this file up?
I don't know what to search for since I don't know what exactly happens when a file straight up downloads instead of seeing raw csv data. Hopefully somebody can help me out.
You can check if a file is downloadable using CURL :
PHP
function checkDownloadable($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch) !== FALSE) {
return true;
}
else {
return false;
}
}

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.

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.

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