Downloading large files using PHP - php

I am using following code to download files from some remote server using php
//some php page parsing code
$url = 'http://www.domain.com/'.$fn;
$path = 'myfolder/'.$fn;
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
// some more code
but instead of downloading and saving the file in the directory it is showing the file contents (junk characters as file is zip) directly on the browser only.
I guess it might be an issue with header content, but not know exactly ...
Thanks

I believe you need:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
to make curl_exec() return the data, and:
$data = curl_exec($ch);
fwrite($fp, $data);
to get the file actually written.

As mentioned in http://php.net/manual/en/function.curl-setopt.php :
CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
So you can simply add this line before your curl_exec line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
and you will have the content in $data variable.

Use the following function that includes error handling.
// Download and save a file with curl
function curl_dl_file($url, $dest, $opts = array())
{
// Open the local file to save. Suppress warning
// upon failure.
$fp = #fopen($dest, 'w+');
if (!$fp)
{
$err_arr = error_get_last();
$error = $err_arr['message'];
return $error;
}
// Set up curl for the download
$ch = curl_init($url);
if (!$ch)
{
$error = curl_error($ch);
fclose($fp);
return $error;
}
$opts[CURLOPT_FILE] = $fp;
// Set up curl options
$failed = !curl_setopt_array($ch, $opts);
if ($failed)
{
$error = curl_error($ch);
curl_close($ch);
fclose($fp);
return $error;
}
// Download the file
$failed = !curl_exec($ch);
if ($failed)
{
$error = curl_error($ch);
curl_close($ch);
fclose($fp);
return $error;
}
// Close the curl handle.
curl_close($ch);
// Flush buffered data to the file
$failed = !fflush($fp);
if ($failed)
{
$err_arr = error_get_last();
$error = $err_arr['message'];
fclose($fp);
return $error;
}
// The file has been written successfully at this point.
// Close the file pointer
$failed = !fclose($fp);
if (!$fp)
{
$err_arr = error_get_last();
$error = $err_arr['message'];
return $error;
}
}

Related

Trying to get data from a webhook with php json

I'm trying to download a file, which URL I get from a webhook:
<?php
$webhookResponse = json_decode(file_get_contents('php://input'), true);
//The resource that we want to download.
$fileUrl = $webhookResponse["data"]["orderDetails"][0]["detailFiles"][0]["fileUrl"];
//The path & filename to save to.
$saveTo = 'test.pdf';
//Open file handler.
$fp = fopen($saveTo, 'w+');
//If $fp is FALSE, something went wrong.
if($fp === false){
throw new Exception('Could not open: ' . $saveTo);
}
//Create a cURL handle.
$ch = curl_init($fileUrl);
//Pass our file handle to cURL.
curl_setopt($ch, CURLOPT_FILE, $fp);
//Timeout if the file doesn't download after 20 seconds.
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
//Execute the request.
curl_exec($ch);
//If there was an error, throw an Exception
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
//Get the HTTP status code.
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Close the cURL handler.
curl_close($ch);
//Close the file handler.
fclose($fp);
if($statusCode == 200){
echo 'Downloaded!';
} else{
echo "Status Code: " . $statusCode;
}
?>
When I try to run it, it gives me an error:
Trying to access array offset on value of type null
This is how my array looks that I get from webhook:
Interesting thing, if I put this same script on another server, it works fine, it downloads the file, if I put it one the actual one I need to use (ubuntu, nginx) it gives me this error, could this be a little server related?

Copy img from url to server: no such file or directory

I am using php to get an image from url and copy it to my server but got an error saying there is no such file
Image example:
http://www.google.co.in/intl/en_com/images/srpr/logo1w.png
Here is the solution I am using:
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
I am getting the following error. Did i do anything wrong?
fopen(/location/to/save/image.jpg): failed to open stream: No such file or directory
✓ This worked for me.
Try it without the /location/to/save/
The file will be saved in the same folder you run the script in.
Such as:
<?php
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("image_google.jpg", "w");
fwrite($fp, $content);
fclose($fp);
?>
function download_image($url,$destination_path = '')
{
// CHECKS IF CURL DOES EXISTS. SOMETIMES WEB HOSTING DISABLES FILE GET CONTENTS
if (function_exists('curl_version'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else
{
$content = file_get_contents($url);
}
// CHECKS IF DIRECTORY DOESNT EXISTS AND DESTINATION PATH IS NOT EMPTY
if(!file_exists($destination_path) && $destination_path != ''){
mkdir($destination_path, 0755, true);
}
// ATTEMPT TO CREATE THE FILE
$fp = fopen($destination_path.'/'.date('YmdHis').".jpg", "a+");
fwrite($fp, $content);
fclose($fp);
}
download_image('http://davidwalsh.name/wp-content/themes/jack/images/treehouse-1.png','images');
The folder (/location/to/save in here) should exist. You also need write permissions in it.

How to check a url exist or not in php

I want to check the url
http://example.com/file.txt
exist or not in php. How can I do it?
if(! # file_get_contents('http://www.domain.com/file.txt')){
echo 'path doesn't exist';
}
This is the easiest way to do it. If you are unfamiliar with the #, that will instruct the function to return false if it would have otherwise thrown an error
The would use the PHP curl extension:
$ch = curl_init(); // set up curl
curl_setopt( $ch, CURLOPT_URL, $url ); // the url to request
if ( false===( $response = curl_exec( $ch ) ) ){ // fetch remote contents
$error = curl_error( $ch );
// doesn't exist
}
curl_close( $ch ); // close the resource
Try this function on Ping site and return result in PHP.
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}
$filename="http://example.com/file.txt";
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
OR
if (fopen($filename, "r"))
{
echo "File Exists";
}
else
{
echo "Can't Connect to File";
}
I am agree with the response, I got success by doing this
$url = "http://example.com/file.txt";
if(! #(file_get_contents($url))){
return false;
}
$content = file_get_contents($url);
return $content;
You can follow the code to check the file exist at location or not.

CURL uploads blank file by FTP

I am using this curl script to try to upload user selected files by FTP. It uploads the files to the server but they are all blank. Why is this happening?
if (!empty($_FILES['userfile']['name'])) {
$ch = curl_init();
$localfile = $_FILES['upload']['tmp_name'];
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp-addy-here'.$_FILES['userfile']['name']);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
} else {
$error = 'Please select a file.';
}
echo $error;
Line 3, right now it says:
$localfile = $_FILES['upload']['tmp_name'];
Change it to:
$localfile = $_FILES['userfile']['tmp_name'];
You didn't check if the initial file upload succeeded. Checking for the presence of the remote filename is NOT an indication that it got uploaded. The 100% reliable method to ensure the upload worked is
if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
... ftp stuff ...
} else {
die("Upload failed with error code: {$_FILES['userfile']['error']}");
}
The codes/constants are documented here: http://php.net/manual/en/features.file-upload.errors.php

Get JPG Dimensions from Partial Extract Without Writing to Disk

I wrote a PHP script that allows me to get the dimensions (width and height) of a remotely hosted JPG without having to download it in full (just the first 10K).
The problem with this is I write the partial download to a file, then read that file to extract the information I need (using getImageSize).
I know this can be down without writing to disk, but I do not know how.
Anyone have suggestions/solutions?
Here is my original code:
function remoteImage($url){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_RANGE, "0-10240");
$fn = "partial.jpg";
$raw = curl_exec($ch);
$result = array();
if(file_exists($fn)){
unlink($fn);
}
if ($raw !== false) {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status == 200 || $status == 206) {
$result["w"] = 0;
$result["h"] = 0;
$fp = fopen($fn, 'x');
fwrite($fp, $raw);
fclose($fp);
$size = getImageSize($fn);
if ($size===false) {
// Cannot get file size information
} else {
// Return width and height
list($result["w"], $result["h"]) = $size;
}
}
}
curl_close ($ch);
return $result;
}
My original question, which lead to this, is here - and might be helpful.
It may be possible to use a memory file stream.
$fn = 'php://memory';
See: http://php.net/manual/en/wrappers.php.php

Categories