ecabrera Posted November 27, 2013 Share Posted November 27, 2013 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/ Share on other sites More sharing options...
jazzman1 Posted November 27, 2013 Share Posted November 27, 2013 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460387 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 jazzman1 It downloads it now but when its down downloading it still says invalid Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460388 Share on other sites More sharing options...
jazzman1 Posted November 27, 2013 Share Posted November 27, 2013 Can I see the output of: echo myfiles/$name Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460392 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 i get this myfiles/2.0 (5).zip Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460393 Share on other sites More sharing options...
jazzman1 Posted November 27, 2013 Share Posted November 27, 2013 Is there a real zip file with this name - 2.0 (5).zip? Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460395 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 yes and the same name to 2.0 (5).zip Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460396 Share on other sites More sharing options...
jazzman1 Posted November 27, 2013 Share Posted November 27, 2013 Try to replace the empty character with underscore(_) using str_replace() php function. Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460397 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 ok but i have other zips that don't have empty space like for example file.zip and its still says invalid Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460398 Share on other sites More sharing options...
jazzman1 Posted November 27, 2013 Share Posted November 27, 2013 (edited) 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 November 27, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460399 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 here you go file.zip Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460400 Share on other sites More sharing options...
jazzman1 Posted November 27, 2013 Share Posted November 27, 2013 (edited) 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 November 27, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460401 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 thanks for the help i will see if i can figure it out Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460402 Share on other sites More sharing options...
bashy Posted November 27, 2013 Share Posted November 27, 2013 Have you tried checking where it's looking or tried setting the full dir? Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460405 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 bashy thats what i tired doing and it downloads it but its saying invalid when it opens i feel the the readfile() maybe the problem i dont know Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460406 Share on other sites More sharing options...
bashy Posted November 27, 2013 Share Posted November 27, 2013 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."); Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460420 Share on other sites More sharing options...
ecabrera Posted November 27, 2013 Author Share Posted November 27, 2013 Most of that stuff is already being implement in the file but it when it opens its says invalid file Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460424 Share on other sites More sharing options...
bashy Posted November 27, 2013 Share Posted November 27, 2013 Well you best show all of the code being used for this...? So you have header('Content-Length: ' . filesize($name)); added? Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460426 Share on other sites More sharing options...
Solution ecabrera Posted November 27, 2013 Author Solution Share Posted November 27, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460429 Share on other sites More sharing options...
jazzman1 Posted November 28, 2013 Share Posted November 28, 2013 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/ Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460438 Share on other sites More sharing options...
mac_gyver Posted November 28, 2013 Share Posted November 28, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284337-cant-open-zip/#findComment-1460439 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.