Lost Password?

Go Back   CodeCall Programming Forum > Web Development Forum > PHP Forum

PHP Forum Use this forum to discuss all aspects of PHP Development. PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-2008, 05:04 AM
jaguar jaguar is offline
Newbie
 
Join Date: May 2008
Posts: 2
Rep Power: 0
jaguar is on a distinguished road
Exclamation File download is corrupted

Hey all,

I have a site that sells mp3's. The mp3 files are grouped; so the download is a zip file of about 70-100 MB. The zip files are in .htaccess protected directory of my webserver. The code i use to send the file to the client browser is the following:

PHP Code:

function vmReadFileChunked($filename,$retbytes=true) {
    
$chunksize 1*(1024*1024); // how many bytes per chunk
    
$buffer '';
    
$cnt =0;
    
// $handle = fopen($filename, 'rb');
    
$handle fopen($filename'rb');
    if (
$handle === false) {
        return 
false;
    }
    while (!
feof($handle)) {
        
$buffer fread($handle$chunksize);
        echo 
$buffer;
        
sleep(1);
        
ob_flush();
        
flush();
        if (
$retbytes) {
            
$cnt += strlen($buffer);
        }
    }
    
$status fclose($handle);
    if (
$retbytes && $status) {
        return 
$cnt// return num. bytes delivered like readfile() does.
    
}
    return 
$status;

Now, everything works fine with all my zip files, except one. There is one zip file that always gets corrupted when you download it. I have recreated the zip file many times, with many different compressors. I have made sure that the file is fine when you upload it through ftp. Yet the file continues to become corrupt on download. Oddly enough, if place outside of the .htaccess protected directory, the download is fine. This does not make sense as all the other zip files get downloaded just fine.

so, the possible sources of the problem are:
* the file is in an .htacces protected directory -- yet all other files download fine
* the zip file is corrupted on ftp upload -- yet, if placed in another location within the server, the download is fine
* the zip file is created with a lousy compressor -- windows, mac and linux compressors have been tried
* the code to send the file is buggy -- yet all other files download fine

Any insight on the true source of the problem and how it might be solved is greatly appreciated.

Cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-02-2008, 10:30 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,035
Last Blog:
NaNoWriMo Day 23 The...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default Re: File download is corrupted

Which browser are you using when the zip file is corrupted? Also, have you verified that it is not corrupt on the server?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-02-2008, 10:34 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,857
Last Blog:
Performance or Maintai...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: File download is corrupted

How large is the corrupted file compared to the others? Are you actually sending the correct size? What do you headers look like on the download script? They should look similar to this:

PHP Code:
          header("Pragma:  no-cache");
          
header("Expires: 0");
          
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
          
header("Cache-Control: public");
          
header("Content-Description: File Transfer");
          
header("Content-type: $mtype");
          
header("Content-Transfer-Encoding: binary"); 
          
header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
            
            
          if (
ini_get('allow_url_fopen')) {
        if (
extension_loaded('curl')) {
                
header("Content-Length: " remotefsize($file));  
        }
     }
      
            
header("Accept-Ranges: bytes"); 
Next, why did you write your own function to "chunk" the data? You can use something as simple as:

PHP Code:
@readfile("$filename"); 
If all you ever intend on sending the user is zip/compressed files you can simply forward their browser to the file URL. IE:

PHP Code:
header("Location: " .$file); 
There is no reason to force a download for these type of files as the browser will do that anyway.

Here is some things you can do:
1) Download the file. Once complete open it with a text-editor and see if any text data is getting added to the beginning or ending of the file that shouldn't be there.

2) Does the file download when you link directly? If not, then it isn't your PHP script. You've corrupted the file somehow.

3) Try to use the php readfile function above and see if it is just your chunk function.

4) Make sure your headers are correct.

I hope this helps. You can download the community project "ionFiles" here on CodeCall. If you look at the download.php file you can see how our PHP file download script works.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-04-2008, 05:54 AM
jaguar jaguar is offline
Newbie
 
Join Date: May 2008
Posts: 2
Rep Power: 0
jaguar is on a distinguished road
Default Re: File download is corrupted

WingedPanther: Yes, I have tried downloading with with Firefox for ubuntu, windows and mac. Same thing happens with IE7 and safari. And yes i have verified that the file is not corrupt on the server because if i move it outside the protected directory, the download is fine.

Jordan: thanks for the advice. because i'm selling downloads, i dont want anyone having a direct URL to them. that is why i'm using this script (which is part of virtuemart, btw) and a protected directory. all the files are of similar size.

I just checked if there is an error message appended to the downloaded file, there is none. the file sizes are slightly different though, the downloaded file is smaller.

Here is the code where i construct my headers (again, from virtuemart). it is very similar to the example you posted.

PHP Code:
$datei DOWNLOADROOT $file_name;
// dump anything in the buffer
@ob_end_clean();

header('Content-Type: ' $mime_type);
header('Expires: ' gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Length: ' filesize($datei) );

if (
$UserBrowser == 'IE') {
    
header('Content-Disposition: attachment; filename="' $file_name '"');
    
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    
header('Pragma: public');
} else {
    
header('Content-Disposition: attachment; filename="' $file_name '"');
    
header('Pragma: no-cache');
}
/*** Now send the file!! ***/
vmReadFileChunked$datei ); 
i guess that the strangest thing is that all the code works for every other zip file i have. why should this particular one become corrupt?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
corrupt, download, zip



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows XP Tricks & Tips!!!!..new ones. pranky Tutorials 9 08-23-2008 03:22 PM
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 300 08-13-2008 08:36 PM
Download a file Lop PHP Forum 14 05-16-2008 12:07 PM
C#:Tutorial - Download Data Xav CSharp Tutorials 9 04-23-2008 03:18 PM
Easy way to download a file Saint Managed C++ 5 09-11-2006 02:13 PM


All times are GMT -5. The time now is 03:58 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads