问题
I am collecting a number of files from the server and generating a ziparchive with PHP. All goes super well until I download the zip archive and try to open the file. The reader throws an exception "File type plain text document (text/plain) is not supported". The file is named correctly (c102.pdf). I presume the mime type of the file is getting messed up some how, but I'm not quite sure.
php version 5.4.37
I should note that I'm using ubuntu 14.04 locally and my reader is the default document viewer, however, it does read the file correctly before upload.
follow up - Could this be a permissions issue? the "downloads" directory is set to 777, while the new zip files created within it are 644. Its as if "blank" files are being placed in the zip archive (the total size of the zip is 1.6k and should be at least a few hundred).
Another follow up - the following previous posts discuss the chmod issue PHP: generated zip has chmod 644 and how to set permission 777 to a zip file? . The chmod() method is not working for me however - chmod() fails to execute on the server (I've amended the code below). We will see what technical support has to say.
Answer in this case.....
After speaking to Customer Support - the reason none of this is working is because I'm on a shared hosting account. The chmod() function is restricted and should otherwise work.
My saga continues... I now have a LAMP stack on an EC2 instance and just when I thought I was boss - I remain in the same bind. My program generates a zip file in the correct directory - with all the appropriate file names - but the files are empty. I am still thinking this is an ownership / permissions issue. SO, some follow up questions...
The folders on the web server are owned by "Ubuntu", yet the newly created files are owned by "www-data". So, who is "www-data"? is it Apache or PHP? is PHP also Apache? why cant I chmod() the files after their created so they aren't 644? Is the files status of the zip as 644 the reason I can't write to them? Can I force newly created files in a directory to have different ownership or a particular set of permissions?
if (isset($_GET['getzip'])) {
$issuedsetid = $_GET['getzip'];
$time = mktime();
$sql = "SELECT * FROM issuedsets WHERE id=$issuedsetid";
if ($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$packid = $row['packid'];
$sheets = $row['sheets'];
$sheets = explode(",",$sheets);
}
}
$sql = "SELECT * FROM pname WHERE id=$packid";
if ($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$packname = $row['name'];
}
}
// create the zip file
$path = "/home/marekfalkowski/public_html/index/downloads/".$packname."_" . $time . ".zip";
$zip = new ZipArchive();
// Open a new zip file
$zip->open($path, ZipArchive::CREATE);
if (chmod($path,0777)){
echo "chmod succesful";
} else {
echo "not succesful";
}
foreach($sheets as $sheet){
// the saved file name as it exists on the server...
$file = $sheet.".pdf";
// the new name of the file...
$sql = "SELECT * FROM files WHERE id=$sheet";
if ($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$filename = $row['number']."-".$row['name'].".pdf";
}
}
// add the files
if (file_exists("/home/marekfalkowski/public_html/index/uploads/".$file)) {
$zip->addFile("/home/marekfalkowski/public_html/index/uploads/".$files, $filename);
} else {
echo "not there";
}
}
$zip->close();
}
回答1:
Try changing this...
$zip->addFile("/home/marekfalkowski/public_html/index/uploads/".$files, $filename);
To this you idiot!!!!
$zip->addFile("/home/marekfalkowski/public_html/index/uploads/".$file, $filename);
What a waste of time this has been. It works now.
来源:https://stackoverflow.com/questions/30496403/pdf-in-php-ziparchive-throwing-errors-server-permissions