Welcome Guest, Not a member yet? Register   Sign In
download helper does not work on zip file?
#1

[eluser]zhulato[/eluser]
hi all,

i got a problem here, i want viewer of my page can download some file on my site, so i'm using download helper force download function, it work (thanks to CI) but the problem is, zip and film file is always corrupt went someone download it.

is there any problem with my code?
Code:
$this->load->helper('download');
$this->load->helper('file');
      
$download_url = "http://www.mysite.com/myfile.zip";
$exploed_url = explode("/", $download_url);
$file_pos = count($exploed_url) - 1;

$name = $exploed_url[$file_pos];
$data = file_get_contents($download_url);

force_download($name, $data);

fyi: some of the file is download directly it form other site, so i'm using ablsolute url

thanks.
#2

[eluser]zhulato[/eluser]
i've solve it

i insert ob_end_clean() in download helper

Code:
// Clean data in cache if exists
@ob_end_clean();
        
// Generate the server headers
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))

is it a bug in download helper? or just me?

is there another way to do this without change the code?

thanks to Wilker for this enlightment Big Grin
#3

[eluser]Kemik[/eluser]
I use force_download() with CI Base and it pushes the download fine. Maybe it's a browser issue?

I've noticed that it won't load views with the force_download() function though.
#4

[eluser]cutoffme[/eluser]
Hi.

In my situation, force_download() always make a broken ZIP file.

$this->load->helper('download');
ob_end_clean();
$path_url = 'http://*****.com/download/';
force_download( $file, file_get_contents( $path_des . $file ) );

Original file has 52,470 bytes and downloaded file has 37,834 bytes. Zip archiver says that this archive might be broken or cut off the later part of this file.

But in another case, this method works for a text file. So, I think this method is not binary-safe...
#5

[eluser]zhulato[/eluser]
[quote author="cutoffme" date="1211904211"]Hi.

In my situation, force_download() always make a broken ZIP file.

$this->load->helper('download');
ob_end_clean();
$path_url = 'http://*****.com/download/';
force_download( $file, file_get_contents( $path_des . $file ) );

Original file has 52,470 bytes and downloaded file has 37,834 bytes. Zip archiver says that this archive might be broken or cut off the later part of this file.

But in another case, this method works for a text file. So, I think this method is not binary-safe...[/quote]

ok, you have same problem with me. i think this is a bug for force_download function.

ob_end_clean(); need to be place inside the code.

for my case, i don't use download helper but i'm make a new function under my controler

here are the code
Code:
function force_download($filename = '', $data = false, $enable_partial = false, $speedlimit = 0)
    {
        if ($filename == '')
        {
            return FALSE;
        }
        
        if($data === false && !file_exists($filename))
            return FALSE;

        // Try to determine if the filename includes a file extension.
        // We need it in order to set the MIME type
        if (FALSE === strpos($filename, '.'))
        {
            return FALSE;
        }
    
        // Grab the file extension
        $x = explode('.', $filename);
        $extension = end($x);

        // Load the mime types
        @include(APPPATH.'config/mimes'.EXT);
    
        // Set a default mime if we can't find it
        if ( ! isset($mimes[$extension]))
        {
            if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
                $UserBrowser = "Opera";
            elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
                $UserBrowser = "IE";
            else
                $UserBrowser = '';
            
            $mime = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';
        }
        else
        {
            $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
        }
        
        //$size = $data === false ? filesize($filename) : strlen($data);
        $size = strlen($data);
        if($data === false)
        {
            $info = pathinfo($filename);
            $name = $info['basename'];
        }
        else
        {
            $name = $filename;
        }
        
        // Clean data in cache if exists
        @ob_end_clean();
        
        // Check for partial download
        if(isset($_SERVER['HTTP_RANGE']) && $enable_partial)
        {
            list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
            list($fbyte, $lbyte) = explode("-", $range);
            
            if(!$lbyte)
                $lbyte = $size - 1;
            
            $new_length = $lbyte - $fbyte;
            
            header("HTTP/1.1 206 Partial Content", true);
            header("Content-Length: $new_length", true);
            header("Content-Range: bytes $fbyte-$lbyte/$size", true);
        }
        else
        {
            header("Content-Length: " . $size);
        }
        
        // Common headers
        header('Content-Type: ' . $mime, true);
        header('Content-Disposition: attachment; filename="' . $name . '"', true);
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT", true);
        header('Accept-Ranges: bytes', true);
        header("Cache-control: private", true);
        header('Pragma: private', true);
        
        // Open file
        if($data === false) {
            $file = fopen($filename, 'r');
            
            if(!$file)
                return FALSE;
        }
        
        // Cut data for partial download
        if(isset($_SERVER['HTTP_RANGE']) && $enable_partial)
            if($data === false)
                fseek($file, $range);
            else
                $data = substr($data, $range);
        
        // Disable script time limit
        @set_time_limit(0);
        
        // Check for speed limit or file optimize
        if($speedlimit > 0 || $data === false)
        {
            if($data === false)
            {
                $chunksize = $speedlimit > 0 ? $speedlimit * 1024 : 512 * 1024;
            
                while(!feof($file) and (connection_status() == 0))
                {
                    $buffer = fread($file, $chunksize);
                    echo $buffer;
                    flush();
                    
                    if($speedlimit > 0)
                        sleep(1);
                }
                
                fclose($file);
            }
            else
            {
                $index = 0;
                $speedlimit *= 1024; //convert to kb
                
                while($index < $size and (connection_status() == 0))
                {
                    $left = $size - $index;
                    $buffersize = min($left, $speedlimit);
                    
                    $buffer = substr($data, $index, $buffersize);
                    $index += $buffersize;
                    
                    echo $buffer;
                    flush();
                    sleep(1);
                }
            }
        }
        else
        {
            echo $data;
        }
    }
i'm using Wilker function for my code
#6

[eluser]cutoffme[/eluser]
Dear zhulato,
Brilliant! It works very fine. In my case, the above codes are in my controller and it calls force_download() internally.

Thank you for your quick reply.
Regards.
#7

[eluser]JOKERz[/eluser]
@zhulato:

when i use absolute path, the download work, but it return 0 kb.
when i use relative path (full site url) it doesnt work. same as CI download helper.

i don't know why.
#8

[eluser]zhulato[/eluser]
[quote author="JOKERz" date="1212190172"]@zhulato:

when i use absolute path, the download work, but it return 0 kb.
when i use relative path (full site url) it doesnt work. same as CI download helper.

i don't know why.[/quote]

i think download helper just work with abosolute path. i don't realy sure about it but for folder and file i'm usualy using abosolute path.
#9

[eluser]JOKERz[/eluser]
yupz.
buts still i doesn't work.
any suggestion?
#10

[eluser]zhulato[/eluser]
[quote author="JOKERz" date="1212411330"]yupz.
buts still i doesn't work.
any suggestion?[/quote]

what code do you use? from download helper or the code i post above. if you use download helper try use code i post above.




Theme © iAndrew 2016 - Forum software by © MyBB