I'm keen to understand the difference between saving a .gif or .jpg (which seem to work fine using the code below), but does not work if I change it to .png
So this code works fine for .jpg or .gif images (links ending in .jpg or .gif)
$img = "http://www.anylocation.com/image.gif";
$ch = curl_init($img);
$fp = fopen($currentdir . 'image.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "<b>Image stored...<br>";
But if I do this:
$img = "http://www.anylocation.com/image.png";
$ch = curl_init($img);
$fp = fopen($currentdir . 'image.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "<b>Image stored...<br>";
It doesn't save the image, well, it tries to, but it just writes image.png with 0 byte size and no content.
Ideally I'd like to modify the above code so that it will work for any scenario or type of image. If not, and the code has to be different for .png, I could write in a 'check the extension of $img' and direct it to the correct piece of code.
Could anyone assist with the explanation of why this doesn't work with .png and if possible, any changes that I could make to make it work?
Your code can reduce to ...
$img = "http://www.anylocation.com/image.png";
file_put_contents($currentdir . 'image.png', file_get_contents($img));
Related
I'm trying to load and save a remote PDF to my server for a project, but the link has no file extension. It's a kind of secured link.
https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803
When I open the link in the browser I can download the PDF file, but saving it with my script has no success.
Is there a way to save the PDF on my server with a script?
I tried following code without success:
$url ="https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803";
getFileContents($url);
function getFileContents($url)
{
// Workaround: Save temp file
$img = tempnam(sys_get_temp_dir(), 'pdf-');
$img .= '.' . pathinfo($url, PATHINFO_EXTENSION);
$fp = fopen($img, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $result ? $img : false;
}
I found the script here : Downloading a large file using curl
To download a file (binary or not) you can use file_get_contents().
file_get_contents return the file content into a string.
//Download PDF content
$pdfContent = file_get_contents("https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=XXXXXXX-DDDD-4444-AAAA-XXXXXXX&LINK_PAGE=ITINERARYENC&TRANS_ID=AAAAAAAA-3333-5555-6666-222222222");
$fileName = "myPDF.pdf";
$fp = fopen($fileName, 'w+');
//Write content into the file
fwrite($fp, $pdfContent);
fclose($fp);
You can if necessary use options in file_get_contents to specify GET or POST method, Basic Authentication and more.
Thanks for your reply, but your code doesn't saves the PDF im looking for.
It safes a PDF with HTML content :
WWW.ENABLELOGISTICS Enable Logistics v95.4248
Welcome to Enable
Logistics Please enter your username and password to sign in.
Forgot Password?©2016 Bright People Technologies Pty Ltd
I have no clue how to dowload the pdf to my server.
Clicking the link only let's me download local.
I'm not allowed to use file_get_contents. Originally I got the file contents by simply doing this:
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filecontent = file_get_contents($filepath);
$encodedFile = base64_encode($filecontent);
Unfortunately, it's not allowed.
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filename = $_FILES['resume-attachment']['name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $filepath);
$filecontent = curl_exec($ch);
curl_close($ch);
$encodedFile = base64_encode($filecontent);
The code above is my attempt at using cURL. I get the filename uploaded, so I get some reaction, but the uploaded file is 0.0 bytes of size... which is not correct. I would think that perhaps the issue could be that I shouldn't treat $filepath as a URL, but what would the alternative be? I should also mention that I'm not trying to post it from this code. I simply want to get the file contents, and then encode it. It's part of an XML string later on.
trying to copy() .MP3 file from remote url but it always fails.
$link = str_replace(' ','%20','http://mp3hungama.com/music/download.php?song_id=80522');
if (!copy($link,'/home2/muser/tmp/newname.mp3')) {
echo 'copy failed !';
}
$link url redirects to http://mp3hungama.com/music/audio//Indian%20Movies/Indian%20Movies%20Hindi%20Mp3%20Songs/Singh%20Is%20Bling%20(2015)/songs/Cinema%20Dekhe%20Mamma%20#%20Mp3HunGama.Com.mp3
same code works for others random urls like www.example.com/download.php?id=2332. what's the specifically problem here or any other way to do this job ?
I've tested your code and I also couldn't download the file, then, I've used curl an it work as expected:
$local_file = "/home2/muser/tmp/newname.mp3";//This is the file where we save the information
$remote_file = "http://mp3hungama.com/music/download.php?song_id=80522"; //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);
NOTE:
Make sure /home2/muser/tmp/ has write permissions.
TIP:
In the future, if you need to encode/decode a url, use urlencode or urldecode instead of str_replace
This link
already redirects to second link. So it's working already.
PHP:
Any ideas where I could find a script similar to what stackoverflow uses? Or would it be easy to make something like that myself? I'm sure downloading the image is not a problem, but I'm more worried about security. I'm building an user avatar upload/remote upload system.
Jquery:
The reason I added jquery to the tags, perhaps it is possible to let the user point the URL of the image and somehow upload it via the normal file upload input himself (without having to manually download the image to the computer first)
You can use cURL to download the image and then use getimagesize() to check whether it's actually an image - for security purposes.
<?php
$limit = 1024*1024*10 // Max. file size in bytes (1024*1024*10 = 10MB)
$ch = curl_init();
$fh = fopen('image.jpg', 'w');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_RANGE, '0-' . $limit);
curl_exec($ch);
curl_close($ch);
if ($image = getimagesize ("image.jpg")) {
// It's an image
}
else {
// Not an image; delete!
}
I'm trying to download this image with PHP to edit it with GD. I found many solutions for image links, but this one is a download link.
Edit:
$curl = curl_init("http://minecraft.net/skin/Notch.png");
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
This is my current code. It displays "301 Moved Permanently". Are there CURLOPTs I have to set?
$curl = curl_init("http://minecraft.net/skin/Notch.png");
// Moved? Fear not, we'll chase it!
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Because you want the result as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
Here is an option to directly save the image to a file (instead of using imagecreatefromstring):
<?php
$fileName = '/some/local/path/image.jpg';
$fileUrl = 'http://remote.server/download/link';
$ch = curl_init($fileUrl); // set the url to open and download
$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded image
curl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointer
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirects
curl_exec($ch); // fetch the image and save it with curl
curl_close($ch); // close curl
fclose($fp); // close the local file pointer
fopen - depends on your php settings if url fopen is allowed.
or curl
see the fine php manual.