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...
Related
I know I can save an image using the following method:
$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg'; // << How to save the image with proper extension?
file_put_contents($output, file_get_contents($input));
But what if I don't know the format of the downloaded image? What if it's "png"? How can I figure out the type of target image before saving it?
The best thing to do is just download it and rename the file later. That way, you only have to make one request.
Another thing you can do however is make an HTTP HEAD request. This gets all of the response headers, including the Content-Type header, so you can decide if you want that data or not before you download the file. However, not all servers support HEAD requests.
In any case, cURL is the easiest way to do this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/something');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$headers = curl_exec($ch);
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);
}
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);
?>
I have a URL like that (basic URL) :
"http://blablablbalbalka.jpg"
which allows me to retrieve an image on a website.
I would like with PHP, retrieve that image and create a BLOB for an insert in a database.
How can i do that ?
Do I need to use function like file_get_contents ? fopen ?
Thanks :)
You could use as you say file_get_contents (depending on security on the webserver - see manual for info) or you could use CURL to fetch the image.
$img = file_get_contents("theurl");
Curl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "theurl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$img = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
then insert $img into a BLOB field...
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.