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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

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.