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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.