PHP ZipArchive中的PDF引发错误,服务器权限 [英] PDF in PHP ZipArchive throwing errors, server permissions

查看:108
本文介绍了PHP ZipArchive中的PDF引发错误,服务器权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从服务器收集许多文件,并使用PHP生成一个ziparchive.一切顺利,直到我下载了zip存档并尝试打开该文件.读者引发异常不支持文件类型的纯文本文档(文本/纯文本)".该文件已正确命名(c102.pdf).我认为文件的mime类型有些混乱,但是我不太确定.

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版本5.4.37

php version 5.4.37

我应该注意,我在本地使用ubuntu 14.04,而我的阅读器是默认的文档查看器,但是,它在上传之前确实能正确读取文件.

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.

跟进-这可能是权限问题吗? 下载"目录设置为777,而在其中创建的新zip文件为644.就好像空白"文件被放在zip归档文件中一样(zip的总大小为1.6k,并且至少应为几百).

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).

另一个后续行动-以下先前的文章讨论了chmod问题 PHP:生成的zip包含chmod 644

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.....

与客户支持中心交谈后-这些都不起作用的原因是因为我使用共享的托管帐户. chmod()函数受到限制,否则应该可以正常工作.

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.

我的传奇继续... 我现在在EC2实例上有一个LAMP堆栈,就在我以为自己是老板的时候-我仍然处于相同的绑定中.我的程序在正确的目录中生成了一个zip文件-带有所有适当的文件名-但这些文件为空.我仍然认为这是所有权/权限问题.所以,一些后续问题...

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...

Web服务器上的文件夹归"Ubuntu"所有,而新创建的文件归"www-data"所有.那么,谁是"www-data"?是Apache还是PHP? PHP也是Apache吗?为什么在文件创建后无法chmod()使它们不是644? zip的文件状态为644是我无法写入它们的原因吗?我可以强制目录中的新创建文件具有不同的所有权或特定的权限集吗?

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();
    }

推荐答案

尝试更改此内容...

Try changing this...

$zip->addFile("/home/marekfalkowski/public_html/index/uploads/".$files, $filename);

你这个白痴!!!

$zip->addFile("/home/marekfalkowski/public_html/index/uploads/".$file, $filename);

这真是浪费时间.现在可以使用了.

What a waste of time this has been. It works now.

这篇关于PHP ZipArchive中的PDF引发错误,服务器权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆