Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder
    Join Date
    Apr 2008
    Location
    Manila, Philippines
    Posts
    262
    Thanks
    3
    Thanked 12 Times in 12 Posts

    Force Download Files on the Browser

    Here I am again seeking for guidance..

    Please help

    The problem is.. I am able to download the file on the server.. the issue is.. i only get 1kb of file during downloads??

    I get the proper name etc.. but the file is only 1kB?

    What I am doing wrong??

    test.com/members/download.php

    Code:
    <?php
    
    $file = $_GET["file"];
    
    // this is the directory in the root..
    $file = "../TourVideos/Members/Videos/" . $file; 
    
    echo "the resource file is " .$file ."<br>";
    
    echo basename($file);
    
    
    if (file_exists($file)) {
        echo "xxxx";
    
        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');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    
    else{
    
    echo "not found";
    
    }
    ?>
    HTML
    Code:
    <a href="download.php?file=movie.mp4">DOWNLOAD</a>
    can you shed some light on me?? I read everything.. but can't seem to fix this.. the code is working but its wrong..

    Thanks in Advance

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    For one thing you can't echo or otherwise produce any output in the download.php without corrupting the file being downloaded.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Regular Coder
    Join Date
    Apr 2008
    Location
    Manila, Philippines
    Posts
    262
    Thanks
    3
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by DrDOS View Post
    For one thing you can't echo or otherwise produce any output in the download.php without corrupting the file being downloaded.
    Boss.. I removed the echos..

    I mean comment them out..

    But still the same.. I only get 1KB of file..

    what could be wrong??

    Actually on the localhost... its working just fine.. I get the full file..

    but on the webserver.. it doesn't work.. I only get a 1kb file
    Last edited by jhaycutexp; 10-15-2013 at 07:59 PM. Reason: add details..

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    You might try removing the ob_clean and flush lines, or at least placing them after the readfile. You may be throwing the baby out with the bathwater.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #5
    Regular Coder
    Join Date
    Apr 2008
    Location
    Manila, Philippines
    Posts
    262
    Thanks
    3
    Thanked 12 Times in 12 Posts
    haays..

    what could be wrong..

    still not working.. i've followed everything..

    and tried different setup..

    it wont download the video..

    could it have issues with huge file sizes?? the one i am downloading is like 100MB..

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Post your current code. Also, add this to the top:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    And run the code again. If it generates errors, ensure you post those as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Also test file downloads with something like flashget - a download manager that lets you see the headers.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #8
    Regular Coder
    Join Date
    Apr 2008
    Location
    Manila, Philippines
    Posts
    262
    Thanks
    3
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by Fou-Lu View Post
    Post your current code. Also, add this to the top:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    And run the code again. If it generates errors, ensure you post those as well.
    dl.php
    Code:
    ini_set('display_errors', 1);
    error_reporting(E_ALL); 
    
    $file = $_GET["file"];
    
    // this is the directory in the root..
    $dir = "TourVideos/Members/Videos/";
    
    $file = "TourVideos/Members/Videos/" . $file; 
    
    
    
    if (file_exists($file)) {
    
    
        header('Content-Description: File Transfer');
        header('Content-Type: video/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        
        readfile($file);
    	
    	ob_clean();
        flush();
        exit;
    }
    
    else{
    
    echo "not found";
    
    }
    this is the html that calls the php
    Code:
    <a href="dl.php?file=Caty_and_Mia-480p_RH.mp4">DOWNLOAD</a>
    I added the code that you've given me..

    I don't know what they do.. but nothing happened .. still the same.. I was able to download a 1kb file.. the downloaded name is correct.. but the whole file size is not.. plus it wont open..

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Did you try using flashget to download the file?

    You really need to be able to see what is happening with the headers and the following content.

    Also save that 1kb. Then open it in notepad++ or wordpad (don't bother with windows notepad). Inside I bet you fill fine a php error notice.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #10
    Regular Coder
    Join Date
    Apr 2008
    Location
    Manila, Philippines
    Posts
    262
    Thanks
    3
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by tangoforce View Post
    Did you try using flashget to download the file?

    You really need to be able to see what is happening with the headers and the following content.

    Also save that 1kb. Then open it in notepad++ or wordpad (don't bother with windows notepad). Inside I bet you fill fine a php error notice.
    Here's the error on the download file..

    Code:
    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 517681153 bytes)
    The flashget.. is no use for me.. cause I have a mac.. its a windows program..
    i tried to look for mac version.. but i can't to seem to find one..

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    That error means that the file is too big for PHPs memory setting to handle. It can be increased in php.ini but if you're on a shared host you won't be able to change it.

    Still, at least you know why you were only getting 1kb eh? - Error message in the file

    As you are serving the file directly, would it not be better just to use the script to count the download and then issue a redirect header to the file itself?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • Users who have thanked tangoforce for this post:

    jhaycutexp (10-16-2013)

  • #12
    Regular Coder
    Join Date
    Apr 2008
    Location
    Manila, Philippines
    Posts
    262
    Thanks
    3
    Thanked 12 Times in 12 Posts
    so if it were just a small file.. like 1MB or 10MB..

    I will be able to download them.. the movie file we are serving is like 500MB

    and some of them are 1.2GB..

    I have tested this.. zugra download script.. and used them..

    its a free script i saw on the net.. and luckily it works like a gem..

    But what I am curious about is how they did them.. still studying the methods they used..

    I know php.. but not that intensive.. I can do basic stuff.. with this exp..

    I am going to take another step.. I will be working to develop my new language.. weew..

    Thanks for the help..

    I know what I did was working.. but just have different output..

  • #13
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    The error's not directly related to the file size (AFAIK readfile() doesn't actually load the file into memory.)

    The following is from the documentation -

    Note:

    readfile() will not present any memory issues, even when sending large files, on its own. If you encounter an out of memory error ensure that output buffering is off with ob_get_level().
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by jhaycutexp View Post
    so if it were just a small file.. like 1MB or 10MB..

    I will be able to download them..

    Using http://www.convert-me.com I input the bytes from your error message. You have 128MB memory to play with so yes, any smaller files of 1-10MB should be ok.

    Quote Originally Posted by jhaycutexp View Post
    the movie file we are serving is like 500MB

    and some of them are 1.2GB..
    What sort of server are you on? Is it shared hosting or a dedicated / VPS server? (If you don't know, it will almost certainly be shared - you'd know if you had a dedi or VPS).

    Quote Originally Posted by jhaycutexp View Post
    I have tested this.. zugra download script.. and used them..

    its a free script i saw on the net.. and luckily it works like a gem..
    It's possible that the script is using ini_set() to change the memory limit of your php. If you're on a shared host you must do this with caution - lots of caution because at some point you will overwhelm the server and your host will shut you down. If you're on a dedi or vps you'd be ok!
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    The error's not directly related to the file size (AFAIK readfile() doesn't actually load the file into memory.)

    The following is from the documentation -
    Yet the message shows a file of 493MB - the op states its a 500MB file.

    There is however (on that note you pointed out) a call to ob_clean.

    @jhaycutexp Why are you using ob_clean? -I don't see a call to ob_start anywhere.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •