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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    PHP download script confusion

    Hi,

    I would like to ask about something that makes my downloaded files from my website becomes corrupted.

    I'll just post the screenshots.

    My codes in here

    dl_functions.php
    PHP Code:
    <?php

    function set_size($size)
    {
        
    $kb 1024;         
        
    $mb 1024 $kb;   
        
    $gb 1024 $mb;   
        
    $tb 1024 $gb;   
        if (
    $size $kb) return $size.' B';
        elseif (
    $size $mb) return round($size/$kb,2).' KB';
        elseif (
    $size $gb) return round($size/$mb,2).' MB';
        elseif (
    $size $tb) return round($size/$gb,2).' GB';
        else return 
    round($size/$tb,2).' TB';
    }

    function 
    get_download_files_list()
    {
     global 
    $download_dirs$SLINE;
        
    $files_list ""
        
    $i 0;
        
    $from_path dirname(__FILE__)."/".$download_dirs;
        
        if (
    is_dir($from_path)) {
            
    chdir($from_path);
            
    $handle opendir('.');
            while ((
    $file readdir($handle)) !== false)
            {
                if ((
    $file != ".") && ($file != "..") && is_file($file)) {
                    
                    
    $files_list .= '<tr><td width="75%"><a href="viewfiles.php?'.$SLINE.'&file='.htmlentities(urlencode(basename($file))).'">'.basename($file).'</a></td><td>'.set_size(filesize($file)).'</td><td><input type="checkbox" name="deleteMe[]" value="'.basename($file).'"></tr>';
                }
            }
            
    closedir($handle); 
        }
     return 
    $files_list;
    }

    function 
    do_download($file)
    {
     global 
    $download_dirs;
        
    $file_name dirname(__FILE__)."/".$download_dirs.$file;
        
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
    header("Pragma: public");
        
    header("Content-Type: application/save");
        
    header("Content-Length: ".filesize($file_name));
        
    header("Content-Disposition: attachment; filename=\"$file\"");
        
    header("Content-Transfer-Encoding: binary");
        
    readfile($file_name);
        exit;
    }
    ?>
    viewfiles.php
    PHP Code:
    <?php 
    include("config.php");
    include(
    "dl_functions.php");
    include 
    'include/session.php';
    include 
    'templates/include/header.php';
    include 
    'include/menubar.php';


    $from_path dirname(__FILE__)."/".$download_dirs;
        
    if (
    is_dir($from_path)) {
        
    chdir($from_path);
        
    $handle opendir('.');
        while ((
    $file readdir($handle)) !== false)
        {
            if ((
    $file != ".") && ($file != "..") && is_file($file)) {
                if(isset(
    $_POST['deleteMe']) && count($_POST['deleteMe']) > 0){
                    foreach(
    $_POST['deleteMe'] as $file){
                        if(
    file_exists($from_path.$file)){
                            
    unlink($from_path.$file);
                        }
                    }
                }

            }
            
        }
        
    //unset($_POST['deleteMe']);
    }
        

    if (isset(
    $_GET["file"])) do_download($_GET["file"]);

    ?>

    <div id="content">
    <h1>VIEW FILES</h1>
    <a href="admin.php">Back</a>
    <?php if($session->isAdmin()){
        echo 
    '|&nbsp;<a href="upload.php">Upload Files</a>';
    }
    if(
    $session->isInstructor()){
        echo 
    '|&nbsp;<a href="upload.php">Upload Files</a>';
    }
    ?>
    <br />
    <form action="viewfiles.php" method="post">
    <h3><center>Click the file to download.</center></h3>
    <table class="main_table" align="center" style="width: auto;">
      <tr>
        <td colspan="1"><b>Download files list</b></td>
        <td colspan="1"><b>Size</b></td>
        <td colspan="1"><b>Select</b></td>
      </tr>
      <?php echo get_download_files_list(); ?>
    </table>
    <center><button type="submit" name="submit" value="submit" onclick="return confirm('Note: This will delete selected files.')">Delete selected</button>
    <button type="reset" name="reset">Reset</button></center>
    </form>

    <br />
    </div>
    <?php include 'templates/include/footer.php'?>
    </html>
    and in my headers.php which i think i have a mistaken header on this.
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml" lang="en">
      <
    head>
        <
    title>CvSUBELMS 2012</title>
        <
    link rel="stylesheet" type="text/css" href="style.css" />
        <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <
    meta name="generator" content="handmade" />
        <
    script type="text/javascript" src="templates/tiny_mce/tiny_mce.js"></script>
        <script>
        $(function(){
            $( "#publicationDate" ).datepicker({ dateFormat: "yy-mm-dd" });
        });
        </script>
      </head>
      <body>
        <div id="container">
        <div id="header">
        </div> 
    Sorry for my english and for being a newbie @ PHP.

    Help me please, Thank you guys!
    Attached Thumbnails Attached Thumbnails PHP download script confusion-lklyukyu.png   PHP download script confusion-sdttre.jpg   PHP download script confusion-frhjy.png   PHP download script confusion-dtrtwe.jpg  

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    You need to read the topic in my signature about cannot modify headers - headers already sent.

    PHP Code:
    <?php
    function do_download($file)
    {
     global 
    $download_dirs;
        
    $file_name dirname(__FILE__)."/".$download_dirs.$file;
        
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
    header("Pragma: public");
        
    header("Content-Type: application/save");
        
    header("Content-Length: ".filesize($file_name));
        
    header("Content-Disposition: attachment; filename=\"$file\"");
        
    header("Content-Transfer-Encoding: binary");
        
    readfile($file_name);
        exit;
    }
    ?>
    viewfiles.php
    PHP Code:
    <?php 
    include 'templates/include/header.php';
    include 
    'include/menubar.php';

    if (isset(
    $_GET["file"])) do_download($_GET["file"]);

    ?>

    You're starting off outputting a header and sidebar. Then if the user is requesting a file you're then trying to output it and headers AFTER html - you can't do that. This will lead to a filestream that has html AND file data. You're basically mixing your html page and the file itself. You can't do that. It must be one OR the other.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Have you got this working yet?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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