1

我为 PHP 编写了一个文件上传脚本。我在 Windows Apache 服务器上进行测试,但它最终必须在带有 Apache 的 CentOS 服务器上工作。因为我还在调试,所以没有在live linux机器上测试过。谷歌了它,但找不到一个好的解决方案。

发生什么了?当我上传 .png 或 .jp(e)g 文件时,一切顺利。该脚本将我的文件移动到正确的目录,但随后输出一个不可读的文件。检查下面的图像。我上传的部分脚本:

if ($posting_photo === true) {
    $uploaded_file = $_FILES['review_photo'];
    $uploaded_file['ext'] = explode('.', $uploaded_file['name']);

    //Only continue if a file was uploaded with a name and an extension
    if ((!empty($uploaded_file['name'])) && (is_array($uploaded_file['ext']))) {
        $uploaded_file['ext'] = secure(strtolower(end($uploaded_file['ext'])));
        $upload_session = secure($_COOKIE[COOKIE_NAME_POST_REVIEW_SESSION]);
        $upload_dir = realpath(getcwd() . DIR_REVIEW_IMAGES) . DIRECTORY_SEPARATOR . $upload_session . DIRECTORY_SEPARATOR;
        $upload_ext = array('jpg', 'jpeg', 'png');

        //Only continue if a file was uploaded in a right way
        if (($uploaded_file['error'] == 0) && ($uploaded_file['size'] > 0) && ($uploaded_file['size'] <= MAX_FILESIZE_REVIEW_PHOTO_BYTES) && (in_array($uploaded_file['ext'], $upload_ext))) {

            //Check if upload dir already exists. If so, there will be probably files in it. So we check for that too. If not, we can start with the first file.
            if (is_dir($upload_dir)) {
                //
                //
                //Part where a new file name gets generated. Not very interesting and it works well, so I left it out on Stack Overflow
                //
                //


            } else {
                mkdir($upload_dir, 0777, true);
                $upload_name = $upload_session . '-1.' . $uploaded_file['ext'];

            }


            //Check if new upload name was generated. If not, something is wrong and we will not continue
            if (!empty($upload_name)) {
                chmod($upload_dir, 0777);

                if (move_uploaded_file($uploaded_file['tmp_name'], $upload_dir . $upload_name)) {
                    $files = array_diff(@scandir($upload_dir), array('.', '..'));

                    //Change slashes on Windows machines for showing the image later
                    $upload_dir = str_replace('\\', '/', $upload_dir);


                }

            }

        }

    }

}

此处未初始化的所有变量均较早初始化。cookie 是之前设置和检查的,用于唯一的目录和文件名。检查此图像以获取输出文件。左边的 x 来自 Dropbox(Dropbox 说:无法同步……访问被拒绝)。照片查看器窗口显示:无法打开此图片,因为您无权访问文件位置。在浏览器中查看文件会导致权限被拒绝。 链接到图片

谁熟悉这个问题和/或知道解决方案?希望你能在这里帮助我!

4

2 回答 2

2

这个也有过一次。这是 PHP 5.3.x 中的一个错误。从 5.3.3 升级到 5.6 后,问题就消失了!

于 2015-05-26T10:09:56.600 回答
0

好的,这是有关问题的更新... 上述问题是由我办公室的本地服务器引起的。在我家的本地服务器上,移动后图像很好。所以这不是我的脚本,而是服务器。我猜PHP处理不同的东西。

我办公室的 PHP 版本是 5.4.9 版,家里的 PHP 是 5.3.3 版。也许这些信息可以提供帮助?

于 2013-03-20T09:07:53.870 回答