Jump to content

File Downloads


Ninjakreborn

Recommended Posts

Is there a way (in PHP) to prompt to download file regardless of it's type?  I have taken someone's suggestions and put files in a folder with HTAccess protection. ALL I want

to do is just grab the files and prompt download (regardless of what the type of file is), is there an easy way to do this in PHP.

Link to comment
https://forums.phpfreaks.com/topic/218117-file-downloads/
Share on other sites

Here is what I have created so far. This does what I want, but screws up the file.

<?php
require_once('config.php');
$file_id = mysql_real_escape_string($_REQUEST['file_id']);
$sql = "SELECT * FROM order_files WHERE file_id = '" . $file_id . "'";
$query = mysql_query($sql);
header("Content-Type: application/force-download");
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
$file = '../' . $row['file_name'];
$filename = explode('/', $file);
$filename = $filename[2];
header( "Content-Disposition: attachment; filename=../" . $filename);
}
echo $file;
if (file_exists($file)) {
readfile($file);
}
?>

It does prompt download no matter what the filetype is.  The only bad thing is, it messes up the file.  So if it's an image file, it won't let me open that image file. 

If it was a PDF it corrupts it. I don't want to have to do a file detection, because I am not sure what all types of files it might be. It could be virtually any type of file.

Without having to analyze every possible extension.

Link to comment
https://forums.phpfreaks.com/topic/218117-file-downloads/#findComment-1131818
Share on other sites

Try something like this:

 

 

    $file = '/path/to/your/file.pdf';
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);

Link to comment
https://forums.phpfreaks.com/topic/218117-file-downloads/#findComment-1131819
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.