Download a file from a redirected URL - php

I'm trying to download a video from a URL which is not the direct link, but a link which is forcing the download. Now I want to download this to my server and convert it with ffmpeg. (I already know how to do this part)
So my question is, how to download a file via php from an indirect link?

Do you mean the server redirects you?
Downloading something in PHP is well done with CURL and is having the CURLOPT_FOLLOWLOCATION option.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.mywebdownloadurl.com/dl?id=someId");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // Videos are needed to transfered in binary
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // That's the clue!
$result = curl_exec($ch); // $result will have your video.
curl_close($ch);
?>

Related

Download a file from Dropbox with cURL

i would like to get some help about the following problem. I'm under windows and i'm able to download any files using the cURL, but when it comes to download from Dropbox i'm unable to do it. Even if i use ?raw=1 or ?dl=1 which is responsible to redirect me to the file i still can't do it.
Here is the script i'm using:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'any url?raw=1');
$fp = fopen('backup.wpress', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
Thanks in advance. I would be very grateful for any suggestions and help.
There's a 302 redirect on that URL, so you'll need to add the line
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
(You also might want to edit that URL out the post, not sure if it's sensitive data...)

php how can get and download image from feelgrafix

i want to download image from this url
http://feelgrafix.com/959413-rococo.html
and this the source image
http://feelgrafix.com/data_images/out/28/959413-rococo.jpg
but when i download image from this source
file Download page url not download source image
this the code i used it
$url = 'http://feelgrafix.com/data_images/out/28/959413-rococo.jpg';
$ch = curl_init($imgURL);
$fp = fopen('image.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
and this Other way
$content = file_get_contents($url); file_put_contents('sadsdasd.jpg',$content);
i think that this the protect from server ..
no one can download OR see the image direct before open the page home
so what can i do ?
Instead of using curl function i recommend you to user file_get_content($url) to fetch the file and file_put_contents($path) to save it in your desired path and in case if any error it throws just usin # before both the function.

Download image using PHP but image is htaccess redirected?

I want to download an image to my server using PHP. This image's html only allows target="_self" meaning it can only be downloaded from the browser apparently. I try to access the image directly in the browser and I get redirected. Is there any way to download this image onto my server via PHP? Maybe I'm missing an option in cURL?
Thanks!
Yes, you have to tell CURL to follow redirects --- try this function:
function wgetImg($img, $pathToSaveTo) {
$ch = curl_init($img);
$fp = fopen($pathToSaveTo, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}

PHP, image from FTP to BLOB

I have an image on my FTP, here : /home/www/myImage.jpg
I need, in PHP, to retrieve this image and create a BLOB.
I use CURL and i don't know how do to that.
I have try this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "ftp://MY_IP/home/www/myImage.jpg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "LOGIN:PASSWORD");
$myBlobImage= curl_exec($ch);
curl_close($ch);
But with this, i retrieve nothing... Any ideas ? Thanks !
If You are running the code on the same server as the file is stored at, then You simply could do:
file_get_contents('PATH/TO/MyFile.Ext');
which loads the file's content. Now You could easily store it to the database to a BLOB column. You could base64_encode the data first or instead of file_get_contents use a byte stream to read stream of bytes...

Can I use a URL as the source for imagecreatefromjpeg() without enabling fopen wrappers?

I know it’s possible to use imagecreatefromjpeg(), imagecreatefrompng(), etc. with a URL as the ‘filename’ with fopen(), but I'm unable to enable the wrappers due to security issues. Is there a way to pass a URL to imagecreatefromX() without enabling them?
I’ve also tried using cURL, and that too is giving me problems:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.../image31.jpg"); //Actually complete URL to image
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
var_dump($image);
imagepng($image);
imagedestroy($image);
You can download the file using cURL then pipe the result into imagecreatefromstring.
Example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
You could even implement a cURL based stream wrapper for 'http' using stream_wrapper_register.
You could always download the image (e.g. with cURL) to a temporary file, and then load the image from that file.

Categories