Download file to website using php [duplicate] - php

This question already has answers here:
Download File to server from URL
(12 answers)
Closed 8 years ago.
I have a file with URL !
$url = "http://www.example.com/aa.txt";
and I want to download this file and save it to path on my website
this is my website ( online )
$path = "server/username/";
i want the $url file saved to $path ,,
i try this
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
when I test the code , it make the file download to my computer not to my website

you can use file_put_contents();
see manual here:http://php.net/manual/en/function.file-put-contents.php
a different implementation here:http://www.finalwebsites.com/forums/topic/php-file-download

Related

Viewing a file when clicked not downloaded? [duplicate]

This question already has answers here:
What is correct content-type for excel files? [duplicate]
(3 answers)
Closed 3 years ago.
When I do a Google search I find a TON of replies about the code needed to download a file when clicked and that works just fine. What I am looking for is when I click on the link it will just open up the file in the browser instead of prompting to download the file. Here is the working code to Download a file:
$file = $invoice_dir . '/test.xlsx';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
How do I just make it open up instead using my local software? It is an XLSX Office type file not plain text. I tried include() and it outputs jibberish.
You just needed to open the file in a href like this
<a href"http://www.yourdomain.com/index.php?file=file.pdf">View File</a>
and in case you want to open it in a new browser tab then
<a href"http://www.yourdomain.com/index.php?file=file.pdf" target="_blank">show file</a>
Add this code
header("Content-type: application/vnd-ms-excel");

Force / Ask for video download on iPhone using PHP

I'm going crazy with this. Been looking here and at Google for a solution. The code I copied works fine under Android, but it does nothing when I try to run it on an iPhone 6. I read that it used to work on iPhone 5 at least, but seems that there is no way to force video download on iPhone 6 and 7.
Is there any way to force or ask for a file to be downloaded on an iPhone using PHP? There's something wrong with this code or is just a pain in the ass provided by Apple?
<?php
ob_start();
if(!empty($_GET['file'])) {
$fileName = basename($_GET['file']);
$fileName = getcwd() . '/uploads/videos/' . $fileName;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($fileName);
exit;
}

downloading pdf files using php [duplicate]

This question already has answers here:
PHP output file on disk to browser
(6 answers)
PHP: How to make browser to download file on click
(2 answers)
Closed 5 years ago.
I have PHP files stored on my server, and their names in the mysql database, I want to download those files. What code should I write for the same? I am using PHP as coding language. Please help.
<?php
$file = 'send_me.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
Obviously, set $file to the file name.
Read more about the use of readfile here.
Download?
Literally just make a link the stored file.
file_put_contents("PDFName.pdf", fopen("http://someurl/PDFName.pdf", 'r'));
You really should show what you have done so far/researched online before asking a question!
This will download the file PDFName.pdf from the url http://someurl/PDFName.pdf and put it into the same directory as the script is in.

Incorrect Headers?

What I'm trying to do:
Im trying to push a jpg file to download witout user seeing the URL. In this case the file is located at: http://www.example.com/upload/asdasdsadpokdaspdso/36.jpg.
My current code:
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$download->name.'.jpg"');
readfile($weburl."/upload/".$hiddenpassage."/".$download->link);
My vars / db values:
$weburl = "http://wwww.example.com";
$hiddenpassage = "asdasdsadpokdaspdso";
$download->link = 36.jpg //not a var, just drom db.
$download->name = The First Test Product //not a var, just from db.
The problem:
When I get the download I open it and I get the following error:
The file “The First Test Product (28).jpg” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognize.
Renaming .jpg to .txt:
http://pastebin.com/K9NGL5RP
Most of that is the content of the page I downloaded it from.
I think you need to specify the whole header (untested). Specially the Content-Length.:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$download->name.'.jpg');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($weburl."/upload/".$hiddenpassage."/".$download->link));
readfile($weburl."/upload/".$hiddenpassage."/".$download->link);
Hope this helps.

Size not showing while downloading files from my site [duplicate]

This question already has answers here:
Size not showing while downloading files from my website
(3 answers)
Closed 8 years ago.
Please see the screenshot below. When someone is downloading files from my site it, while downloading size is not showing. Thus, users cannot know how much is remaining, what is the size. Can someone help me or tell any idea to fix this? Is this a problem with the server? Or I have to put any code or something?
You should properly send the headers from your server
Better look at this :- http://php.net/manual/en/function.readfile.php
Example
<?php
$file = 'yourfile.apk';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

Categories