Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Auto save of file - php header
Old 03-19-2012, 10:13 AM Auto save of file - php header
numbenator's Avatar
Webmaster Talker

Posts: 534
Location: London
Trades: 0
Hi There
Im trying to enable clicking a href link to download a file automatically for some reason ( probably something silly ) my file im downloading is always 0kb in size even though i know the image / pdf / zip exist.

My code in my download_file.php is as below

Code:
$filen = isset($_GET['filen']) ? addslashes($_GET['filen']) : '';
$ext=substr($filen, -3); 
$fileDownload="download.".$ext;
header('Content-disposition: attachment; filename='.$fileDownload);
switch ($ext) {
case "pdf":
header('Content-Type: application/pdf');
break;
case "zip":
header('Content-Type: application/zip'); // ZIP file
break;
case "mpeg":
case "mpg":
header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file
break;
}
readfile($filen);
?>
My link to the above is

Code:
<a href="download_file.php?filen=<?= $asset[0] ?>">
and the url is normaly something like /uploads/filename.pdf

Has anyone got any idea what i am doing wrong here. I have tried taking out the switch and literally just putting in everything as basic like below

Code:
<?php
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=test.pdf");
readfile('/wp-content/uploads/2012/02/On_a_Knife_Edge_-_Schools_Pack1.pdf');
?>

But it always uploads 0 bytes

Any help would be greatly received as im going cross eyed on this now.

Cheers

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 03-19-2012, 11:19 AM Re: Auto save of file - php header
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
Trades: 0
Since your URL is linking to a PDF file, I would think your script is not even being used or executed at all.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-19-2012, 11:24 AM Re: Auto save of file - php header
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
If you are certain that the file exists, then perhaps the url is wrong and simply doesn't point to where you think it does. As an example, on shared hosts absolute paths usually looks something like

/home/username/public_html/some/directory/file.ext

See what result you get from file_exists().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-19-2012, 11:35 AM Re: Auto save of file - php header
numbenator's Avatar
Webmaster Talker

Posts: 534
Location: London
Trades: 0
Hey lizciz,
good idea thanks. will try that now.
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 03-19-2012, 11:43 AM Re: Auto save of file - php header
numbenator's Avatar
Webmaster Talker

Posts: 534
Location: London
Trades: 0
OK lizciz
Good test.

I have evaluated that the file isnt effectively being found.
However, when i output the url it's looking for and paste that into a new browser window the file does in fact exist.

Any ideas to forward me another step.???
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 03-19-2012, 12:13 PM Re: Auto save of file - php header
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Well, when using an absolute path (starts with /) you start from the root. The root from the browsers perspective isn't the same as from the servers perspective, where php operates. Hence, as I wrote before, absolute paths will often start with "/home/username/public_html" or similar in php scripts, which should not be included in i.e. anchor link tags.

So to clarify, a link to
/wp-content/uploads/2012/02/On_a_Knife_Edge_-_Schools_Pack1.pdf

should perhaps be
/home/username/public_html/wp-content/uploads/2012/02/On_a_Knife_Edge_-_Schools_Pack1.pdf

when passing it to php functions.

To find your absolute path, you can i.e. echo the response from getcwd().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 03-19-2012 at 12:15 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-19-2012, 01:01 PM Re: Auto save of file - php header
numbenator's Avatar
Webmaster Talker

Posts: 534
Location: London
Trades: 0
Thank lizciz
Much appreciated
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 03-19-2012, 01:12 PM Re: Auto save of file - php header
numbenator's Avatar
Webmaster Talker

Posts: 534
Location: London
Trades: 0
Just to add you absolutely right in what you said.
My referencing by absolute url just wasn't working on the server and by using getcwd ive established how to reference the file.

Thanks soo much. much appreciated.
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Reply     « Reply to Auto save of file - php header
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27293 seconds with 11 queries