Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 12:36 PM   PM User | #1
markbaron15
New Coder

 
Join Date: May 2012
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
markbaron15 is an unknown quantity at this point
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
Click image for larger version

Name:	lklyukyu.PNG
Views:	24
Size:	22.1 KB
ID:	11594   Click image for larger version

Name:	sdttre.jpg
Views:	17
Size:	42.8 KB
ID:	11595   Click image for larger version

Name:	frhjy.PNG
Views:	18
Size:	31.6 KB
ID:	11596   Click image for larger version

Name:	dtrtwe.jpg
Views:	18
Size:	51.5 KB
ID:	11597  
markbaron15 is offline   Reply With Quote
Old 10-02-2012, 02:18 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 10-05-2012, 09:34 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Have you got this working yet?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:12 PM.


Advertisement
Log in to turn off these ads.