繁体   English   中英

PHP文件上传导致Windows Apache服务器上的文件损坏

[英]PHP file upload results in broken files on Windows Apache server

我已经为PHP编写了文件上传脚本。 我正在Windows Apache服务器上进行测试,但最终将不得不在具有Apache的CentOS服务器上工作。 因为我仍在调试,所以尚未在实时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说:无法同步...访问被拒绝)。 “照片查看器”窗口显示:无法打开该图片,因为您无权访问文件位置。 在浏览器中查看文件会导致权限被拒绝。 链接到图片

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

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

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

我办公室的PHP版本为5.4.9,家庭PHP版本为5.3.3。 也许此信息可以帮助您?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM