【问题标题】:Multiple image uploads cause 500 Internal Server Error多张图片上传导致 500 Internal Server Error
【发布时间】:2014-11-26 07:26:08
【问题描述】:

我想在我的网站上实现多个部分,访问者可以在其中上传要显示的图像。 我让它在服务器上的一个测试文件夹中完美地工作,它回显“文件是一个图像。[文件]成功上传”并且图像出现在服务器上。

但是,在网站中实际执行此脚本(我将脚本复制到不同的文件夹),我得到返回:500 Internal Server error。

你们中有人知道是什么导致了这个问题吗?

HTML 表单(在 /entries.php 中):

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <link href="../../style.css" rel="stylesheet" type="text/css">
    </head>
    <body bgcolor="#000000">    
        <form action="upload.php" method="post" enctype="multipart/form-data"><p class="text">
            Upload your entry here!:
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit"></p>
        </form>
    </body>
</html>

PHP 脚本(在 upload.php 中):

<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

【问题讨论】:

  • 检查你的网络服务器(可能是 apache2)的日志文件,它会告诉你 500 内部错误是什么
  • 感谢您的回复。可悲的是,我的网络服务器由 GoDaddy 托管。我可以看到谁在连接哪个页面的日志文件,但没有错误日志。

标签: php image upload


【解决方案1】:

php.ini 中的upload_max_filesize 可能需要增加。

echo ini_get ("upload_max_filesize");

这个命令会给你这个值,然后你可以进入你的 php.ini 并在不够时增加它。

FcgidMaxRequestLenhttpd.conf configuration 可能需要增加。

在这种情况下,您可以在 httpd.conf 中创建一个新的 apache 指令,类似于以下内容。

FcgidMaxRequestLen 字节

<IfModule mod_fcgid.c>
    MaxRequestLen 15728640
</IfModule>

您的托管公司不允许 PHP 访问您为上传指定的目录。

由于每个托管公司都有不同的配置,因此过程可能会有所不同。但这是 GoDaddy 和 Plesk 的流程。

Can I use open_basedir on my server running Parallels Plesk Panel?

如果您在该目录中有 .htaccess,请将其暂时移动并重试。

您需要将其从 PHP 脚本所在的目录以及文件上传的目录中移出。如果这显然允许上传,您就知道 .htaccess 中有一个规则/指令导致了问题。

【讨论】:

  • 感谢您的回复。我已经确保不超过文件大小限制,还有 fileUpload = On
  • 关于您的编辑:我不知道如何进入 httpd.conf 配置。我的托管公司使用“Plesk”来配置服务器,但似乎非常有限。另外,我在任何地方都看不到 .htaccess 。我也检查了权限,这应该不是问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
相关资源
最近更新 更多