Jump to content

Force file download and autostart on page load


brdalert

Recommended Posts

Hi,

I have a lot of documents that are downloadable on my site. What i' wanting to do is have a jump menu link to individual pages, these pages would be the ones that would auto start the downloads. or a way to download straight from the jump menu would be nice. Any help would be appreciated.

<?php
ob_start();

$file = $_GET['file'];

if(file_exists($file)){
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));
ob_clean();
flush();
readfile($file);
}else {
echo "File does not exist";
}

ob_end_flush();
?>

As you can see the $file takes the file by method get.So whatever the name of the file coming in this would be taken,also the file can be anywhere but you must specify the right path in the original form from where you are sending( by get or post) the variable file.Also you must specify appropriate permissions on that file for its download from a particular directory.

This is my Page.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

ob_start();

 

Documents/Book of Daniel = $_GET['file'];

 

if(file_exists($file)){

 

 

 

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));

 

 

 

ob_clean();

 

 

 

flush();

 

 

 

readfile($file);

}else {

 

 

 

echo "File does not exist";

}

 

ob_end_flush();

?>

</body>

</html>

 

I am haveing a hard time understanding this can some one fix my script then re post it so it can see how i should look. I'm getting this error now. Parse error: parse error, unex.pected T_IF in /home/e/a/4/10671/10671/public_html/test doenload.php on line 14

See just copy and paste this script .It will surely work.The error is too obvious.You are not giving it the right string.$file or $document,use one.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

ob_start()

 

$file = $_GET['file'];

 

if(file_exists($file)){

 

 

 

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));

 

 

 

ob_clean();

 

 

 

flush();

 

 

 

readfile($file);

}else {

 

 

 

echo "File does not exist";

}

 

ob_end_flush();

?>

</body>

</html>

I don't understand where to put my file name within the code. this is my first attempt a php. so could you please put a fake file name in the script where it needs to go so i can see. or give my some more detailed instructions. thank you

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.