There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Force file download


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
13-Jun-2008, 11:06 PM #16
with this? <?php
force_download("file.jpg");
?>

or an html tag?
Eriksrocks's Avatar
Computer Specs
Senior Member with 1,859 posts.
 
Join Date: Aug 2005
Location: Minnesota
Experience: Advanced
14-Jun-2008, 12:58 AM #17
Nope, just the script that you posted in your last post, if that's what you're using. You would put that in a file called downloadmovie.php or something and then you would link to it wherever you want users to download the MOV file.

So it might look like:

HTML Code:
<a href="downloadmovie.php">Download my movie here!</a>
Then when they click on that it would force download the file that you specified earlier.

Does that make sense?

EDIT: Also, don't know what you're doing with this line:
PHP Code:
header("Content-Type: $ctype"); 
You never decalred the $ctype variable anywhere in the script, and that's sort of the main part in making it force the download.
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
14-Jun-2008, 01:04 AM #18
Using this code now.
PHP Code:
<?PHP
// Define the path to file
$file 'test.mp4';

if(!
file)
{
    
// File doesn't exist, output error
    
die('file not found');
}
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);
}
?>
senior.danjnelson.com
Working sort of as you may see.
Can I choose the name the file will be downloaded as?
I can use the other code, I just am going to need more instruction.
Thanks!!

Last edited by dannyn : 14-Jun-2008 09:35 PM.
TheRobatron's Avatar
Computer Specs
Senior Member with 417 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
15-Jun-2008, 01:32 PM #19
I think to choose the name it is downloaded as, you'll have to change the name of the file on the webserver. The code seems to work, so is that your problem sorted?
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
15-Jun-2008, 10:36 PM #20
have you tried to downlaod it?
Using mozilla it names the location then the file, and it fails to replay after its downloaded.
I will try your script, but where do i define the file path?
Eriksrocks's Avatar
Computer Specs
Senior Member with 1,859 posts.
 
Join Date: Aug 2005
Location: Minnesota
Experience: Advanced
16-Jun-2008, 02:57 PM #21
Why are you setting the content type as a ZIP?
PHP Code:
header("Content-Type: application/zip"); 
That makes the browser think it's a ZIP file, which it's not.
I tried to download it and play it in QuickTime, but it wouldn't play. That's not a problem with the script, however, it's a problem with the original file or the way you uploaded it to the server.
TheRobatron's Avatar
Computer Specs
Senior Member with 417 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
16-Jun-2008, 03:15 PM #22
I assumed that when I downloaded it, the file would play (which it doesn't now ). With my code, you change the $dir variable to specify the folder, and then specify the filename when you call the function. To download a file called test.txt in the current folder, you would use the following code:
PHP Code:
<?php
function force_download($file)
{
    
$dir      "./";
    if ((isset(
$file))&&(file_exists($dir.$file))) {
       
header("Content-type: application/force-download");
       
header('Content-Disposition: inline; filename="' $dir.$file '"');
       
header("Content-Transfer-Encoding: Binary");
       
header("Content-length: ".filesize($dir.$file));
       
header('Content-Type: application/octet-stream');
       
header('Content-Disposition: attachment; filename="' $file '"');
       
readfile("$dir$file");
    } else {
       echo 
"No file selected";
    } 
//end if

}//end function

force_download("test.txt");
?>
The $dir variable and the $file variable are joined together anyway, so it doesn't matter if you want to specify the whole file path when you call the function.

Note: As before, you will have to put the code in a separate file.
__________________
There's no place like 127.0.0.1


Last edited by TheRobatron : 16-Jun-2008 03:21 PM.
Eriksrocks's Avatar
Computer Specs
Senior Member with 1,859 posts.
 
Join Date: Aug 2005
Location: Minnesota
Experience: Advanced
16-Jun-2008, 04:03 PM #23
I would recommend trying the script with a simple JPG file or even a TXT file to make sure that it's working right and that the problem isn't the MP4.
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
17-Jun-2008, 12:45 AM #24
Thanks for all your help.
You guys are amazing.
Changed to header('Content-Type: '.$mime);
It worked.
Thanks.
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
17-Jun-2008, 01:15 AM #25
This is not necessary, but how would I let the download manager know the size of the file?
Thanks
Eriksrocks's Avatar
Computer Specs
Senior Member with 1,859 posts.
 
Join Date: Aug 2005
Location: Minnesota
Experience: Advanced
17-Jun-2008, 01:31 PM #26
Quote:
Originally Posted by dannyn View Post
This is not necessary, but how would I let the download manager know the size of the file?
Thanks
Add this where the rest of the headers are:
PHP Code:
header("Content-length: ".filesize($dir.$file)); 
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
17-Jun-2008, 01:54 PM #27
PHP Code:
<?PHP
// Define the path to file
$file 'test4.mp4';

if(!
file)
{
    
// File doesn't exist, output error
    
die('file not found');
}
else
{
    
// Set headers
    
header("Cache-Control: public");
    
header("Content-Description: File Transfer");
    
header("Content-Disposition: attachment; filename=$file");
    
header('Content-Type: '.$mime);
    
header("Content-Transfer-Encoding: binary");
   
    
// Read the file from disk
    
readfile($file);
}
?>
There is no $dir, but i will still try it.
Do i still want the encoding to be binary? I am thinking yes, beacuse as the code stands, it works.
Thanks
TheRobatron's Avatar
Computer Specs
Senior Member with 417 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
17-Jun-2008, 01:57 PM #28
Without the $dir variable, it would just be
PHP Code:
header("Content-length: ".filesize($file)); 
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
17-Jun-2008, 02:10 PM #29
Ok. Thanks. I will try that when I get home.
Thanks for all of your help!
dannyn's Avatar
Senior Member with 923 posts.
 
Join Date: Nov 2007
Experience: Intermediate
17-Jun-2008, 09:34 PM #30
Alright,Got it going due to all your help.
Thanks!
I am going to post the final code one more time in case someone else would like to use it.
PHP Code:
<?PHP
// Define the path to file
$file 'test4.mp4';

if(!
file)
{
    
// File doesn't exist, output error
    
die('file not found');
}
else
{
    
// Set headers
    
header("Cache-Control: public");
    
header("Content-Description: File Transfer");
    
header("Content-Disposition: attachment; filename=$file");
    
header('Content-Type: '.$mime);
    
header("Content-Transfer-Encoding: binary");
    
header("Content-length: ".filesize($file));  
   
    
// Read the file from disk
    
readfile($file);
}
?>
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:01 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.