readfile() script not working after changing server - php

I had a code that worked, here:
if (isset($_GET['file']) && isset($_GET['name']))
{
$file = base64_decode($_REQUEST['file']);
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
// Contains file size in bytes
$contentLength = (int)$matches[1];
}
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".$_REQUEST['name']);
header('Content-Length: ' . $contentLength);
header('Content-Transfer-Encoding: binary');
#readfile($file);
}
And than I changed my server to a differenf host providor, and the code stoped working. When I download from chrome it just keeps loading and loading,
but when I download from a download manager like IDM than it gets the correct name and size, but when I press start it makes an error that says something about not supporting resuming or something...
Again, the code worked when I used a different providor.

It might be errors which are outputted before readfile() use ob_clean();
flush(); to remove any output before readfile

Related

PHP - Download a file from a URL

In my project, I need to download a file, and I need to use PHP.
All the results from Google were eventually not very helpful.
After combining code from 2 results, I ended up attempting:
function downloadFile($url, $filename) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
header('Content-Description: File Transfer');
header('Content-Type: audio/*');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($data));
readfile($data);
}
The result is an audio file, weighs 10KB (should be 3.58MB).
This code isn't the same as the code in the question that was suggested as a duplicate - here there's a section of curl functions and headers, while the question's answer only has a bunch of headers.
When opened the file with VLC - the following error appears:
Also, I tried using:
file_put_contents($filename, file_get_contents($url));
This one results in downloading the file to the path in which the PHP file is located at - which is not what I want - I need the file to be downloaded to the Downloads folder.
So now I'm basically lost.
What's the appropriate way to download files?
Thanks!
I successfully did it, thanks to Google and a lot of experiments.
The final code:
function downloadFile($url, $filename) {
$cURL = curl_init($url);
curl_setopt_array($cURL, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => fopen("Downloads/$filename", "w+"),
CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"]
]);
$data = curl_exec($cURL);
curl_close($cURL);
header("Content-Disposition: attachment; filename=\"$filename\"");
echo $data;
}

PHP - Download file from external server, filename has quotes " ' " : 'filename.zip'

I made a script where depending on the request the server will return a specific file for download from an external source, I use an external source because I have unlimited bandwidth on the other server:
$ch = curl_init();
$url="http://www.example.com/downloads/$fileName";
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // make it a HEAD request
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mimeType");
header("Content-length: $size");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition:attachment;filename='$fileName'");
readfile($url);
$filename comes from the request on the front end. It is a simple post from a form.
Everything works great, but some users, not all, have reported that they cant open the file because the file name has quotes: 'filename.zip', instead of filename.zip
I hit a wall, I have no idea even where to start looking, apparently this happens with some mac users. Any ideas?
The HTTP standard would have you putting double quotes around the filename parameter in the Content-Disposition header. That might look like this in your code:
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Type: ' . $mimeType);
header('Content-Type:application/octet-stream');
header('Content-length: ' . $size);
header('Content-Transfer-Encoding: binary');
header('Content-Disposition:attachment;filename="' . $fileName . '"');
readfile($url);
Note here that I have changed all your PHP string definitions to use single quotes to present all string definitions consistently.

Pass URL to PHP via $_GET

I've face weird problem below url when I copy and paste it into download manager, it works fine. But I am going to stream it in direct link via PHP then get an 400 error. However when I use the link into different upload center site (uploadboy.com) to test if the link works fine or not, it was working fine on the uploader.
I coded a script that I attach to affix. And try many different way to download it but still face error 400!
What action do I have taken to fix the problem?
I test to see if the script works urlencode and urldecode function on whole parts of url and on query string of url. in these 4 different ways no success to download. However I attached my script in one of the way I tested.
I notice you again. the link working fine and it's not limited to IP Address. However Download Manager that I test to download the url have same IP address with php webserver.
Sample of URL:
http://r5---sn-p5qlsnsk.googlevideo.com/videoplayback?ipbits=0&mm=31&fexp=9407724,9408142,9408592,9408705,9408710,9408982,9412517,9412776,9413317,9413355,945137,948124,952612,952637,952640,952642&id=o-ACIY_VMiJqYl9lskpYea7F_6tP6DNv6WlJattCarpNST&signature=0DF7579F401F8BB481A4511F96ECD95C52FB618B.29AA2A450464D709E3FC3974759459A1EB7066E9&nh=IgpwcjAzLmlhZDA3KgkxMjcuMC4wLjE&key=yt5&mt=1432113172&ip=198.50.235.216&upn=aR3Slhiml7g&expire=1432134981&sparams=dur,id,ip,ipbits,itag,mime,mm,ms,mv,nh,pl,source,upn,expire&mime=video/3gpp&sver=3&pl=23&source=youtube&mv=u&dur=87.817&itag=17&ms=au&signature= '&title='Youtube-API-V3-Drag--Drop-PlayList--Download-videos--Search-Keyword'
Sample of Code to stream:
$url = $_POST['url'];
$referrer = $_POST['referrer'];
if(strstr($url,'googlevideo.com')){
$url_parts = parse_url($url);
$query = $url_parts['query'];
$query_parts = parse_str($query,$queries);
foreach ($queries AS $key=>$value){
$post_url .= $key.'='.urlencode($value).'&';
}
$post_url = rtrim($post_url, '&');
$url2 = 'http://r1---sn-u2oxu-f5f6.googlevideo.com/videoplayback?'.$post_url;
die($url.'<br><br>'.$url2);
$filename = preg_replace('/[^a-zA-Z0-9-_\.]/','', $title).'.'.getExtension($mime);
// $head = array_change_key_case(get_headers($url, TRUE));
// $filesize = $head['content-length'];
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
// header('Content-Length: '.$filesize); // provide file size
header('Connection: close');
// echo getUrl($url,$_SERVER['HTTP_USER_AGENT'],$referrer);
$file = fopen(str_replace('&','&',$url), 'rb');
while (($content = fread($file, 20480)) !== false) { // Read in 2048-byte chunks
echo $content; // or output it somehow else.
flush(); // force output so far
}
fclose($file);
exit();
}
Update1:
I already test below curl function:
function getUrl($url,$agent='',$referrer='',$proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
if(!empty($agent)){
curl_setopt ($ch, CURLOPT_USERAGENT,$agent);
}
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
if(!empty($referrer)){
curl_setopt ($ch, CURLOPT_REFERER, $referrer);
}
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
The 400 error comes from a typo in your url: remove the space character in this part: &signature= '&title='.
Your curl function is ok, but it doesn't work with your url. I don't know why not, but it works with an url like this: https://youtu.be/LxBk5f6mr-A.
Perhaps you can use the tool mentioned in this post:
grabbing youtube video URL from curl or get_video_info

cURL - Downloading a file (wrong size)

I want to get a video file from another server, and open it on my website in flowplayer.
I did this :
$ch = curl_init('http://example.com/file.flv');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
header("Content-Type: video/flv");
header("Content-Disposition: attachment; filename=video.flv;" );
header("Content-Length: ".$size);
readfile($result);
But there is a problem with the size of file, it always shows wrong size of file (too small)
I have to use a cURL, because only my server IP can get a video from video server.
Thanks for help
readfile() is used to reads a file and write it to the output, use echo instead of, otherwise, use strlen to get content length, try the code below:
$ch = curl_init('http://example.com/file.flv');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$size = strlen($result);
header("Content-Type: video/flv");
header("Content-Disposition: attachment; filename=video.flv;" );
header("Content-Length: ".$size);
echo $result;

How to download this PDF file using php

I want to download a pdf file from this link. From the page, select weekly summary, I want download the first pdf.
PDF Weekly summary - Last week: Sorted by insider
I'm using the following code try to download the pdf,
<?php
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked'); //changed to chunked
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('Content-Length: ' . filesize($file)); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
?>
It seems I can not download using PHP. I can download by myself confirm the popup window. Any suggestions?
I tried curl also, still can not download. The file size is zero.
<?php
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
$path = 'c:\download\output.pdf';
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
Finally I could download the PDF file with PHP curl and CURLOPT_REFERER setting. Below is the code.
<?php
$url = 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA';
$path = "/pdf/output.pdf";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTReportsAccessController?menukey=15.03.00&locale=en_CA');
$data = curl_exec($ch);
curl_close($ch);
$result = file_put_contents($path, $data);
if (!$result) {
echo "error";
} else {
echo "success";
}
?>
Instead of getting your PHP server to act as a proxy why not just:
<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header("Location: $file");
?>
Version 2
<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf"');
readfile($file);
?>
Version 3
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA');
Add this line to your original code?
Also, as mentioned here:
Download a image from SSL using curl?
for https we should add the following:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Categories