The display of some images through the readfile does not work - php

I have this simple function that I use to display images on a website (due to mixed content) but it doesn't work for this particular one. How to view it?
Code:
<?php
$remoteImage = "http://data.rcm-pelikan.cz/products2/KAV0152&5/b_0_779bc.jpg";
$imginfo = getimagesize($remoteImage);
header("Content-type: ".$imginfo['mime']);
readfile($remoteImage);
Edit:
Solution:
$remoteImage = "http://data.rcm-pelikan.cz/products2/KAV0152&5/b_0_779bc.jpg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remoteImage);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch) ;
header("Content-Type: $contentType");
echo $res;

Related

How to download an mp3 file from remote url?

I am redirecting my website to a url where only an mp3 file is streaming and I want that file to be downloaded to local computer.
how can i do that?
I have already searched google and stacksoverflow but the solutions didn't worked for me
I couldn't find solution for my specific problem.
Use CURL to download, then you can use file_get_contents to save file on server, or you can use some headers to force download the file.
$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
file_put_contents(dirname(__FILE__) . '/audio.mp3', $output);
}
Download to browser:
$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=audio.mp3");
echo $output;
die();
}
curl only works for small files.
For larger files you can download mp3, mp4, zip by doing the following:
file_put_contents(dirname(__FILE__) . '/recordings/audio.mp3', fopen('http://url-to-file.com/audio.mp3', 'r'))
There are two ways to do it.
First way is use function readfile - it will be faster, then all other methods.
function curl_get_file_size( $url ) {
$result = -1;
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_NOBODY, true );curl_setopt( $curl, CURLOPT_HEADER, true );curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
$data = curl_exec( $curl );
curl_close( $curl );
if( $data ) {
$content_length = "unknown";
$status = "unknown";
if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) $status = (int)$matches[1];
if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) $content_length = (int)$matches[1];
if( $status == 200 || ($status > 300 && $status <= 308) )
$result = $content_length;
}
return $result;
}
function run() {
global $url,$title;
header("Content-type: audio/mpeg");
header("Content-Disposition: attachment; filename=$title.mp3");
header("Content-length: " . curl_get_file_size($url));
header("accept-ranges: bytes");
readfile($url);
}
run();
Second way is when you download full file and then return it
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
header("Content-type: audio/mpeg");
header("Content-Disposition: attachment; filename=$title.mp3");
header("Content-length: " . strlen($output));
header("accept-ranges: bytes");
echo $output;
die();
}

how to get the size of a file corresponding to a url

$url = https://r3---sn-5jucgv5qc5oq-cagl.googlevideo.com/videoplayback?initcwndbps=2133750&mime=video%2Fmp4&ipbits=0&fexp=900720%2C907263%2C909721%2C930824%2C934954%2C9405651%2C9405972%2C9406849%2C9407103%2C9407756%2C9407880%2C9408102%2C942310%2C943610%2C948124%2C948703%2C951511%2C951703%2C952302%2C952612%2C952901%2C955301%2C957201%2C957507%2C958600%2C959701%2C961404%2C962727&ratebypass=yes&itag=18&requiressl=yes&sver=3&mm=31&id=o-AH3LjJikaNXn9Fxj1nCeZeGQxBRB2Env2Cd-Rwumwn2U&mt=1426837685&mv=m&ms=au&ip=106.51.132.147&dur=336.596&pl=24&upn=NV9m09pWiSg&source=youtube&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&expire=1426859426&key=yt5&signature=42A2FFA9C6EAFF6EBACC605EE5E06FA7B38805DB.9F608D789F6516F43E25A77B841B428975ADC7A4
How to get the size of file reffered by this URL. when I open this url the video file starts streaming. But I need the file size in order to force download the file to browser using the header('content-length') field
You can do this by using cURL :
I can see you are using a https URL So it will surly help you
<?php
$ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, 'http://upload.wikimedia.org/wikipedia/commons/a/af/Bonsai_IMG_6426.jpg');
curl_setopt($ch, CURLOPT_URL, 'https://r3---sn-5jucgv5qc5oq-cagl.googlevideo.com/videoplayback?initcwndbps=2133750&mime=video%2Fmp4&ipbits=0&fexp=900720%2C907263%2C909721%2C930824%2C934954%2C9405651%2C9405972%2C9406849%2C9407103%2C9407756%2C9407880%2C9408102%2C942310%2C943610%2C948124%2C948703%2C951511%2C951703%2C952302%2C952612%2C952901%2C955301%2C957201%2C957507%2C958600%2C959701%2C961404%2C962727&ratebypass=yes&itag=18&requiressl=yes&sver=3&mm=31&id=o-AH3LjJikaNXn9Fxj1nCeZeGQxBRB2Env2Cd-Rwumwn2U&mt=1426837685&mv=m&ms=au&ip=106.51.132.147&dur=336.596&pl=24&upn=NV9m09pWiSg&source=youtube&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&expire=1426859426&key=yt5&signature=42A2FFA9C6EAFF6EBACC605EE5E06FA7B38805DB.9F608D789F6516F43E25A77B841B428975ADC7A4');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);
?>
.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://weknowyourdreams.com/images/love/love-01.jpg');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);
?>
Try curl to get size of any file by curl_getinfo()
$ch = curl_init('http://www.example.com/yourfile'); //put your url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo $size;
Try to use filesize():
<?php
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>

Curl returning null page

I am trying to curl http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480Quality=Standard this location and returning null page each time. my code is
$image = 'http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480&Quality=Standard';
$ch = curl_init();
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_URL, $image);
$store = curl_exec ($ch);
echo $store;
curl_close ($ch);
Can anybody tell what i am missing?
try doing:
...
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $image);
// Get binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
// output to browser
header("Content-type: image/jpeg");
echo $image;
Edit: try urlencode()'ing the url as:
$image = 'http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution='.urlencode('640x480&Quality=Standard');
try to add
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
so code will be:-
$image = 'http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480&Quality=Standard';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_URL, $image);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$store = curl_exec ($ch);
curl_close ($ch);
header("Content-type: image/jpeg");
echo $store;

Retrieve a file from a URL and serve it with the same headers

Currently I'm working on building a SMF mod that enables SSL and one thing that keeps poping up is the issue of user embedded content (like images) and other sorts of things not being served from sites with SSL. I'm after a way to build a sort of proxy script that will run on my site and when passed a URL, will retrieve the file, maintain most of the headers (eg. mime type) and then serve the file again. This way it looks like the image or whatever is actually being served from a SSL enabled site when infact its not.
Thanks for all the comments! Just for those who want to do this in the future, heres the script I'm using:
<?php
$requestURL = filter_var($_REQUEST["url"], FILTER_SANITIZE_URL);
if(filter_var($requestURL, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
{
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
// Load the headers from the destination server.
$uri_info = new URIInfo($requestURL);
if($uri_info->info['filetime'] == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
header('HTTP/1.1 304 Not Modified');
}
else
{
//Serve a new copy of the file, its changed :O
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILETIME, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//Display the image in the browser
header('Content-type: ' . $info['content_type']);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $info['filetime']));
header('Cache-Control: no-cache'); //Stop CloudFlare!
echo $data;
}
}
else
{
//Just serve the file, they obviously don't have a copy of it!
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILETIME, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//Display the image in the browser
header('Content-type: ' . $info['content_type']);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $info['filetime']));
header('Cache-Control: no-cache'); //Stop CloudFlare!
echo $data;
}
}
exit();
?>
<?php
class URIInfo
{
public $info;
public $header;
private $url;
public function __construct($url)
{
$this->url = $url;
$this->setData();
}
public function setData()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_FILETIME, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$this->header = curl_exec($curl);
$this->info = curl_getinfo($curl);
curl_close($curl);
}
public function getFiletime()
{
return $this->info['filetime'];
}
}
?>

Simple CURL url exists won't work

I have 2 codes written with PHP
<?php
error_reporting(1);
function url_exists($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', curl_exec($ch) , $status);
return ($status[1] == 200);
}
echo "EX:".url_exists("http://www.google.com");
?>
and
<?php
error_reporting(1);
$ch = curl_init("www.yahoo.com");
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
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)
{
echo "Found#";
}
else
{
echo "Not found";
}
?>
None of this of above works? WHY??? Yesterday worked pretty good, why PHP cannot be explain some times?
I have curl extension on from php.ini ... i tried on 3 different servers ...
What i'm doing wrong? Thank you.
echo "EX:".url_exists("http://www.google.com");
will always echo EX: because url_exists returns a boolean. Try:
echo "EX:" . (url_exists("http://www.google.com") ? 'true' : 'false');
Maybe this works?
$ch = curl_init("http://www.example.com/favicon.ico");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);
Source https://stackoverflow.com/a/982045/956397

Categories