【问题标题】:Error while uploading gifs上传 GIF 时出错
【发布时间】:2014-02-26 07:13:54
【问题描述】:

我可以使用此代码上传任何图片,但是当我尝试上传 gif 时出现错误。 这是我尝试的方法,我得到的错误是Error 2: ERROR upload file。这是在第二个 IF 块上。这里有什么问题?

define('MAX_FILE_SIZE', 20000000000);
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
if (isset($_POST['upload'])) {

$caption = $_POST['caption'];
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$category = $_POST['gif_cat'];

$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());

// gif name with extension
$myFile = $randName . '.' . $ext;
// save gif path
$path = "../upload/gifs/" . $myFile;

if (in_array($fileType, $permitted) && $fileSize > 0
    && $fileSize <= MAX_FILE_SIZE) {

    $result = move_uploaded_file($tmpName, $path);

    if (!$result) {
        echo "Error uploading gif file";
        exit;
    } else {
        $db = new mysqli("localhost", "user", "pass", "table");

        if (mysqli_connect_errno()) {
            printf("Connect failed: %s<br/>", mysqli_connect_error());
        }
        mysqli_set_charset($db, "UTF8");

        $query = "INSERT INTO gifs (caption, name, size, type, file_path, gif_cat) VALUES (?,?,?,?,?,?)";
        $conn = $db->prepare($query);
        if ($conn == TRUE) {
            $conn->bind_param("ssisss",$caption, $myFile, $fileSize, $fileType, $path, $category);
            if (!$conn->execute()) {
                echo 'error insert';
            } else {
                echo "Gif {$_FILES['userfile']['name']} was successfully uploaded<br />
                <a href='index.php'>Add another gif</a><br />";
                exit;
            }
        } else {
            die("Error 1: ERROR preparing Statement");
        }
    }
} else {
    echo 'Error 2: ERROR upload file';
}
} else {
echo 'Error 3';
}

var_dump($_FILES)

array (size=1)
'userfile' => 
array (size=5)
  'name' => string 'azbRWYK_460sa.gif' (length=17)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0

更新:

这发生在 .gif 大于 1MB 的情况下。我能够上传

更新 2: 那奇怪。在 phpinfo() 结果是

max_file_uploads    20  20
post_max_size   8M  8M
upload_max_filesize 2M  2M

但在我的 php.ini 中我有

upload_max_filesize = 20M
post_max_size = 20M
max_file_uploads - I don't have this in php.ini?!

我怎么会有这种差异?在哪里可以找到第二个 php.ini?

更新 3:

好的,我在 apache 文件夹中找到了另一个 php.ini,在我更改值之后现在可以工作了。我从没想过第二个 php.ini 文件。

【问题讨论】:

  • 打印 $_FILES['userfile']['type'],它可能不是允许的
  • 这种情况发生在 .gif 大于 1MB 的情况下。我能够上传

标签: php mysql upload


【解决方案1】:

实际上,您的 if 语句的这一部分正在失败,您将把它带到 Error 2: ERROR upload file 块中。

  if (in_array($fileType, $permitted) && $fileSize > 0
        && $fileSize <= MAX_FILE_SIZE) {

对变量$fileType$fileSize 进行var_dump(),看看它们是否满足您的if 条件。

【讨论】:

  • 我不明白为什么这部分失败了,因为$filetype$permitted 没问题。我编辑了我的问题并添加了 var_dump 的结果。
  • 看到你的$fileType 只是一个空变量,这就是你的代码失败的原因。此外,您不需要var_dump $permitted 变量,因为我们已经通过代码知道它(里面是什么)。您实际上也需要var_dump $fileSize
  • var_dump($fileSize) return 0 int 0
  • 0 &gt; 0 条件仍然失败。最好 var_dump 你的 $_FILES 数组本身。
  • var_dump($_FILES) 更新了我的问题,但仍然不知道这是为什么
【解决方案2】:

检查您的 php.ini 文件和 phpinfo() 将返回什么以及它们是否相等。检查您是否有多个 php.ini 文件。

我之前也遇到过同样的问题,原来是这个原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-24
    • 2017-05-15
    • 1970-01-01
    • 2013-10-08
    • 2020-09-02
    • 2013-01-23
    • 2018-07-17
    • 2015-04-15
    相关资源
    最近更新 更多