943,832 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 656
  • PHP RSS
Aug 13th, 2009
0

PHP Not Detecting Local Files

Expand Post »
Hi, I am having a problem forcing a download from PHP. I am running 5.2 Stable. I have followed numerous tutorials and still can't figure it out. What I'm trying to do is force-download a file I made, because the browser likes opening it up inside, but it won't let me use relevant paths so I have to type in the full web address every time. Here's my working and not working code:

Working:
PHP Syntax (Toggle Plain Text)
  1. if ($checkbox) {
  2. $file = 'http://12.148.233.90/downloads/Tutorial1.wmv.zip';
  3. // Set headers
  4. header("Cache-Control: public");
  5. header("Content-Description: File Transfer");
  6. header("Content-Disposition: attachment; filename=Tutorial1.wmv.zip");
  7. header("Content-Type: application/zip");
  8. header("Content-Transfer-Encoding: binary");
  9. //header("Content-Length: " . filesize($file));
  10. ob_clean();
  11. flush();
  12. readfile("$file");
  13. exit;
  14. }
  15. else {
  16. ...
  17. }
Not working, but want to work:
PHP Syntax (Toggle Plain Text)
  1. if ($checkbox) {
  2. $file = 'downloads/Tutorial1.wmv.zip';
  3. // Set headers
  4. header("Cache-Control: public");
  5. header("Content-Description: File Transfer");
  6. header("Content-Disposition: attachment; filename=Tutorial1.wmv.zip");
  7. header("Content-Type: application/zip");
  8. header("Content-Transfer-Encoding: binary");
  9. header("Content-Length: " . filesize($file));
  10. ob_clean();
  11. flush();
  12. readfile("$file");
  13. exit;
  14. }
  15. else {
  16. ...
  17. }
With the not working code section, it says that it can't find the file. The reason I want that section working is because I want the file size to be correct when you download (so it doesn't say "Unknown Time Remaining") and to simplify the code a little bit. Thanks for your help.

If you need me to clarify anything, just ask. I could also map my HDD and upload my IIS settings too.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apease11 is offline Offline
19 posts
since Feb 2009
Aug 13th, 2009
0

Re: PHP Not Detecting Local Files

php Syntax (Toggle Plain Text)
  1. if ($checkbox) {
  2. $file = 'downloads/Tutorial1.wmv.zip';
  3. // Set headers
  4. header("Cache-Control: public ;");
  5. header("Content-Description: File Transfer");
  6. header("Content-Disposition: attachment; filename=Tutorial1.wmv.zip");
  7. header("Content-Type: application/zip");
  8. header("Content-Transfer-Encoding: binary");
  9. header("Content-Length: " . filesize($file) ."; ");
maybe, going to think
Last edited by almostbob; Aug 13th, 2009 at 11:43 pm.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Aug 14th, 2009
0

Re: PHP Not Detecting Local Files

me too had the same problem but I gave full path.

Try giving path from a variable preceded by $_SERVER['SERVER_NAME']; thus by making full path?

Hope this will work for you.

(thought your plan was to use same code in local and remote servers).
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
Aug 14th, 2009
0

Re: PHP Not Detecting Local Files

$_SERVER['SERVER_NAME'] returned a strange response; the server created a new zip file when I used that in the path, sent it over to me, and then deleted it (
php Syntax (Toggle Plain Text)
  1. $file = $_SERVER['SERVER_NAME'] . '/downloads/Tutorial1.wmv.zip';
). I know the file exists, and I echo'd the $_SERVER['SERVER_NAME'] and that came out correct. That's not a big problem because I can just type the full address like I did before and it works (the same thing that my first code snippet does), what I want is the file size to work most of all, so people aren't left not knowing how big the file is (and these are big files).

I'm getting a little confused on how $file = $_SERVER['SERVER_NAME'] . '/downloads/Tutorial1.wmv.zip'; is different from '12.148.233.90/downloads/Tutorial1.wmv.zip' when they return the same thing. Thanks for your help.

And the reason I'm using this code for zip files is one of two things, I will be force downloading other files than zip (like wmv and txt, most browsers read them) and my firefox somehow has the power to open up zip files within itself and it won't let me download them.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apease11 is offline Offline
19 posts
since Feb 2009
Aug 14th, 2009
0

Re: PHP Not Detecting Local Files

I think it must be something wrong with the config file, it works fine under my PHP 4 Linux server. The php.ini file is available at http://12.148.233.90/php.ini if you want to take a look. I'll be going through it myself, all 1338 lines, sigh. Thanks for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apease11 is offline Offline
19 posts
since Feb 2009
Aug 14th, 2009
0

Re: PHP Not Detecting Local Files

I got it to work, for now, I specified the file size manually along with reordering my info and adding stuff. I'll post the code if you want it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apease11 is offline Offline
19 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Conditional file upload issue
Next Thread in PHP Forum Timeline: php parse error - Please help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC