How to create a link to download a php file? [duplicate] - php

This question already has answers here:
How to download a php file without executing it?
(6 answers)
Closed 7 years ago.
I have a website, and I want to be able to put a link that enables the user to download a php file, I don't want it to run or for the user to navigate to the url, but be able to download the original file, is this possible?
Thanks

Thanks Biswajit, that link got me what I needed (I saw the original "duplicate" but I wasn't working for me)
<?php
// Fetch the file info.
$filePath = '/path/to/file/on/disk.jpg';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>

Related

PHP Force Download [duplicate]

This question already has answers here:
Handle file download from ajax post
(21 answers)
Closed last year.
I am new to php and ajax. Im trying to force download a file from a list of documents. The functions below gets triggered using an ajax call after a button click.
function downloadDocument($filename) {
$file_path = ".........../DocUploader/Uploads/" . $filename;
if (file_exists($file_path)) {
header("Content-Description: File Transfer");
header("Content-Type: application/json");
header("Content-Disposition: attachment; filename=\'" . $filename);
readfile($file_path);
} else{
echo "Document does not exist";
}};
Instead of downloading the file, I assume I am getting the file content as a response. I would really appreciate any advise on what I should do.
First remove any echo's or output to the browser before sending any header, ie. remove the echo "File exists";
The file name should be encapsulated in quotations, ie. Content-Disposition: attachment; filename="filename.ext"
<?php
header("Content-Description: File Transfer");
header("Content-Type: application/json");
header('Content-Disposition: attachment; filename="'. $filename .'"');
readfile($file_path);
Keep in mind to set correct Content-type based on the real kind of the downloaded document, see topic What are all the possible values for HTTP "Content-Type" header?
You can dynamically detect the MIME type using PHP function mime_content_type()
And reference for PHP header.

PHP how to open the .doc and .docx files? [duplicate]

This question already has answers here:
How can I view/open a word document in my browser using with PHP or HTML
(6 answers)
Closed 7 years ago.
doc files
$filename = 'ahfdghasfdh.doc';
header('Content-type: application/msword');
header('Content-Disposition: inline; filename="testqq"');
#readfile($filename);
I tried this code its not working. How to open the .doc files on browser using php.
Try below code.
<?php
$filename = 'ahfdghasfdh.doc';
header('Content-disposition: inline');
header('Content-type: application/msword'); // not sure if this is the correct MIME type
readfile($filename);
exit;
?>
You can open as like that:
MSWORD FILE
And other solution #vinod already shared
And if you just want to open doc file on browser no application involved than I recommend to use GOOGLE DOCS.

How to start download of file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forcing to download a file using PHP
So i am selling something, and I want the file download to start when users go to the thanks page.
The location of the file is stored in $install, now when user visitsthanks.php, how do i start the file download automatically?
Thanks.
The majority of what you want to do is actually going to be done in Javascript.
You PHP code will serve up the thanks page, and after it is loaded, you will want to direct a hidden iFrame in your page to a page which serves up the file as a download, using the following headers:
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfile.txt");
readfile($pathToFile);
<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// Download here
?>
The alternative to a javascript redirect is using a meta refresh tag in your HTML. It works even when javascript is disabled.

How to add a txt file and create a ZIP in php [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
Open file, write to file, save file as a zip and stream to user for download
I have a txt file in uploads folder where i need to add this file and create a ZIP on fly and prompt the user to download the Zip file which should contain the text file .How do i do this and moreover what are the headers needed for prompting the user for download .can any one Help me out. I have used the same constucts from PHP Manual.Iam Using the same function to zip the file "http://davidwalsh.name/create-zip-php" and iam writing this code and iam getting prompted for zip download but iam getting the caution message when i extarct the file
$zip_file = "my-archive.zip";
$file_path="../../../downloads/";
$files_to_zip = array($file_path."test_testing.txt");
$result = create_zip($files_to_zip,'my-archive.zip');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zip_file);
header("Pragma: no-cache");
header("Expires: 0");
readfile($zip_file);
The Caution error message is "one or more files in this archive uses ".."(parent folder) as part of its folder information "
In the create_zip() function,
change
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
to
foreach($valid_files as $file) {
$zip->addFile($file,pathinfo($file,PATHINFO_BASENAME));
}
This will work as long as you don't have files with duplicate names but different directories in your array of files

forcing a file to download [duplicate]

This question already has answers here:
php - How to force download of a file?
(7 answers)
Closed 7 years ago.
I have a php page with information, and links to files, such as pdf files. The file types can be anything, as they can be uploaded by a user.
I would like to know how to force a download for any type of file, without forcing a download of links to other pages linked from the site. I know it's possible to do this with headers, but I don't want to break the rest of my site.
All the links to other pages are done via Javascript, and the actual link is to #, so maybe this would be OK?
Should I just set
header('Content-Disposition: attachment;)
for the entire page?
You need to send these two header fields for the particular resources:
Content-Type: application/octet-stream
Content-Disposition: attachment
The Content-Disposition can additionally have a filename parameter.
You can do this either by using a PHP script that sends the files:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment');
readfile($fileToSend);
exit;
And the filenames are passed to that script via URL. Or you use some web server features such as mod_rewrite to force the type:
RewriteEngine on
RewriteRule ^download/ - [L,T=application/octet-stream]
Slightly different style and ready to go :)
$file = 'folder/' . $name;
if (! file) {
die('file not found'); //Or do something
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}

Categories