Jump to content

cant open zip


Go to solution Solved by ecabrera,

Recommended Posts

why does it not download it says its invalid i thought this would work

        if($type == "application/x-zip-compressed"){

            header("Content-Type: application/x-zip-compressed");
            header('Content-Disposition: attachment; filename="'.$name.'"');
            header("Content-Transfer-Encoding: binary");
            header('Pragma: no-cache');
            header('Expires: 0');
            readfile("myfiles/$name");

        }
Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/
Share on other sites

Try,

if($type == "application/x-zip-compressed"){

            header("Content-Type: application/x-zip-compressed");
            header('Content-Disposition: attachment; filename='.  basename("myfiles/$name"));
            header("Content-Transfer-Encoding: binary");
            header('Pragma: no-cache');
            header('Expires: 0');
            header('Content-Length: ' . filesize("myfiles/$name"));
            readfile("myfiles/$name");

        }
Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460387
Share on other sites

Attach this file.zip here I wanna test it on my local server. 

 

Don't attach anything I tried with another zip file and the result was like yours. I will try later again. There is something wrong.

Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460399
Share on other sites

When I moved the zip file in the same directory where the script is - it works.

 

The script is:

<?php

$name = "test.zip";

header("Content-Type: application/x-zip-compressed");
header('Content-Disposition: attachment; filename='.  basename("$name"));
header("Content-Transfer-Encoding: base64");
header('Pragma: no-cache');
header('Expires: 0');
header('Content-Length: ' . filesize("$name"));
readfile("$name");

I will try later on to see where the problem is b/s don't have the time to get into it

Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460401
Share on other sites

Looking on PHP manual, try adding

header('Content-Length: ' . filesize($name));

Or looking at PHP readfile, there's some added functions by users, found something you might want to change and take a look at

$filename = 'dummy.zip';
            $filename = realpath($filename);

            $file_extension = strtolower(substr(strrchr($filename,"."),1));

            switch ($file_extension) {
                case "pdf": $ctype="application/pdf"; break;
                case "exe": $ctype="application/octet-stream"; break;
                case "zip": $ctype="application/zip"; break;
                case "doc": $ctype="application/msword"; break;
                case "xls": $ctype="application/vnd.ms-excel"; break;
                case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
                case "gif": $ctype="image/gif"; break;
                case "png": $ctype="image/png"; break;
                case "jpe": case "jpeg":
                case "jpg": $ctype="image/jpg"; break;
                default: $ctype="application/force-download";
            }

            if (!file_exists($filename)) {
                die("NO FILE HERE");
            }

            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
            header("Content-Type: $ctype");
            header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ".@filesize($filename));
            set_time_limit(0);
            @readfile("$filename") or die("File not found.");
Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460420
Share on other sites

  • Solution

ok i solved it i added 

           header("Content-Type: application/x-zip-compressed");
           header('Content-Disposition: attachment; filename='.  basename("myfiles/$name"));
            header("Content-Transfer-Encoding: binary");
            header('Pragma: no-cache');
            header('Expires: 0');
            header('Content-Length: ' . filesize("myfiles/$name"));
//added this below
ob_clean();
flush();
            readfile("myfiles/$name");
exit;
Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460429
Share on other sites

ob_clean() and flush() functions have nothing to do unless the output buffering being enabled in the php configuration script or you called ob_start() somewhere on your own script manualy starting this buffer. So, my advise is to avoid using it here.

 

Try again without any buffering.

 

Here it is:

<?php

// get the current working directory
//echo __DIR__; exit;

// define the name of the zip file
$fileName = 'file.zip';

// set the path to zip directory and file name
$path = __DIR__ . '/downloads/zip/'.$fileName;

header("Content-Type: application/x-zip-compressed");
header('Content-Disposition: attachment; filename=' . basename($path));
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
header('Content-Length: ' . filesize($path));
readfile($path);

Here, my working directory on my local machine is named - /var/www/html/public_html/pdo/

The path to the zip directory is - /var/www/html/public_html/pdo/downloads/zip/

Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460438
Share on other sites

your php code in the download file is either intentionally (i.e. your previous thread for this subject) or accidentally outputting characters to the browser that become part of the downloaded file, corrupting it.

 

if you open the downloaded file that is failing in a programming editor, you will be able to see either what is being added to the start of the file. it's either some specific content, a php error message, or byte order mark characters.

Link to comment
https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460439
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.