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 20
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Force download .mov

    I have this file that works like a charm, but I have a problem.
    In the database the download field is being inputted with the path after the root for each category, for example:

    mysite.com/videos/horror/031609/horrormovie.mov
    mysite.com/videos/action/031809/actionmovie.mov

    so this is what is appending to it:

    download.php?file=videos/horror/031809/actionmovie.mov

    How can I append after .com/ "videos/horror/031609/" or "videos/action/031809/" and then the user be able to download the video.
    Firefox let you download the video, but IE, and Safari prompt you to buy quicktime professional version which I don't want my users to buy any software.

    Let me know if you have any questions, in advance thanks for your help.
    Code:
    <?php
    $filename = $_GET['file'];
    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    if( $filename == "" ) 
    {
      echo "<html><title></title><body>ERROR: download file NOT SPECIFIED. USE download.php?file=filepath</body></html>";
      exit;
    } elseif ( ! file_exists( $filename ) ) 
    {
      echo "<html><title></title><body>ERROR: File not found. USE download.php?file=filepath</body></html>";
      exit;
    };
    switch( $file_extension )
    {
      case "mov": $ctype="video/quicktime"; break;
      case "mp4": $ctype="video/mp4"; break;
      default: $ctype="application/force-download";
    }
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    exit();
    ?>

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Hmm I can't understand the problem you are talking about, it seems fine to me. Any more information you can provide?

  • #3
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    what about adding this line:

    PHP Code:
    header("Content-Type: application/octet-stream"); 

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi kokjj87and masterofollies,

    Thanks for replying.

    Kokjj87,

    The octet-stream is just another header that I am not looking for, but thanks.

    masterofollies,

    My information is storage in the database in this format:
    so this is what is appending to it:

    download.php?file=videos/horror/031809/actionmovie.mov

    and then it does not work
    Code:
    mysql> desc myMovies;
    --------------------------------------------------------------------------------+
    | ID | myDate	             |	Title  | Category  | myPath 				        | 
    ----+---------------------+--------+-----------+-------------------------------+
    | 1 | 2009-03-18 00:00:00 | Jason  | Horror    | horror/031609/jasonmovie.mov	|
    | 2 | 2009-03-16 00:00:00 | Rambo | Action    | action/031809/rambomovie.mov	|
    | 3 | 2009-03-04 00:00:00 | Taken  | Action    | action/031809/takenmovie.mov	|
    -----------+-------------+-------------------+---------------------------------+
    3 rows in set (0.00 sec)
    The script does works fine if would it be with out the path but this is not possible for me:
    Code:
    mysql> desc myMovies;
    --------------------------------------------------------------------+
    |ID  | myDate			  |	Title  | Category  | myPath 		| 
    ----+---------------------+--------+-----------+-------------------+
    | 1 | 2001-12-03 00:00:00 | Jason  | Horror    | jasonmovie.mov	        |
    | 2 | 2001-08-24 00:00:00 | Rambo | Action    | rambomovie.mov	|
    | 3 | 2001-08-01 00:00:00 | Taken  | Action    | takenmovie.mov	|
    -----------+-------------+---------------------+--------------------+
    3 rows in set (0.00 sec)
    If any body know how to deal with this problem any help would be greatly appreciated.

  • #5
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    After

    PHP Code:
    $filename $_GET['file']; 
    add

    PHP Code:
    // Create array [0] => videos [1] => horror [2] => 031809 [3] => actionmovie.mov
    $filename explode('/'$filename);
    // Return just the file name part of the array
    $filename $filename[3]; 

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hi Steelaz,

    Thanks a lot, and I see you understand where I am stuck. How can the array be created?
    Another problem I might face later is that, how about if I have to skip more than three directories?

  • #7
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    The line about creating array is just a comment explaining what "explode('/', $filename);" actually does.

    You should use end() function to get the last element in array. So beginning of your script should look like this:

    PHP Code:
    <?php
    $filename 
    $_GET['file'];
    $filename explode('/'$filename);
    $filename end($filename);

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ERROR: File not found. USE download.php?file=filepath
    and still getting this appended to the url
    mysite.com/videos/download.php?file=horror/031809/actionmovie.mov

    Code:
    <?php
    $filename = $_GET['file'];
    // Create array [0] => videos [1] => horror [2] => 031809 [3] => actionmovie.mov 
    $filename = explode('/', $filename); 
    // Return just the file name part of the array 
    $filename = end($filename);
    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    if( $filename == "" ) 
    {
      echo "<html><title></title><body>ERROR: download file NOT SPECIFIED. USE download.php?file=filepath</body></html>";
      exit;
    } elseif ( ! file_exists( $filename ) ) 
    {
      echo "<html><title></title><body>ERROR: File not found. USE download.php?file=filepath</body></html>";
      exit;
    };
    switch( $file_extension )
    {
      case "mov": $ctype="video/quicktime"; break;
      case "mp4": $ctype="video/mp4"; break;
      default: $ctype="application/force-download";
    }
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    exit();
    ?>

  • #9
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Ok, let's try another thing, remove my modifications and replace this line

    PHP Code:
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); 
    with

    PHP Code:
    header("Content-Disposition: attachment; filename=\"".$filename."\";" ); 
    basename() function returns just a file name, so if your videos are in sub-directories they won't be fetched.

  • #10
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I changed but still appending

    Code:
    <?php
    $filename = $_GET['file'];
    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    if( $filename == "" ) 
    {
      echo "<html><title></title><body>ERROR: download file NOT SPECIFIED. USE download.php?file=filepath</body></html>";
      exit;
    } elseif ( ! file_exists( $filename ) ) 
    {
      echo "<html><title></title><body>ERROR: File not found. USE download.php?file=filepath</body></html>";
      exit;
    };
    switch( $file_extension )
    {
      case "mov": $ctype="video/quicktime"; break;
      case "mp4": $ctype="video/mp4"; break;
      default: $ctype="application/force-download";
    }
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".$filename."\";" ); 
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    exit();
    ?>

  • #11
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    What do you mean by "still appending"? What exactly happens when you access download URL, do you get any errors?

  • #12
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes I get this error:
    ERROR: File not found. USE download.php?file=filepath

    In the address bar it shows like this and broken link:
    mysite.com/videos/download.php?file=horror/031809/actionmovie.mov

    It should be:

    mysite.com/videos/horror/031809/actionmovie.mov

    with download.php

    be the middle man.

  • #13
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    That's strange, I just tried the same code on my page and it worked without a problem. Obviously it can't find "actionmovie.mov" file. Are you 100% sure file is there? Can you download it when opening mysite.com/videos/horror/031809/actionmovie.mov in the browser?

  • #14
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you think my php version (4.4.9) or probably the system administrator disable force download?

  • #15
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    I don't think so, this error - "ERROR: File not found. USE download.php?file=filepath" means that "( ! file_exists( $filename ) )" returns TRUE, which in turn means that file with that path was not found.


  •  
    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
    •