【问题标题】:How can I upload files larger than 10MB?如何上传大于 10MB 的文件?
【发布时间】:2019-12-17 00:37:56
【问题描述】:

此脚本有效,但仅适用于大约 10 MB 或更小的文件。还有什么,我得到一个文件错误:

error = 上传软件时出错

我想上传更大的文件,比如 50MB,甚至是几 GB 的文件。

<form action="../../programming/softupload.php" enctype="multipart/form-data" method="POST" class="uploader">
    <p class="close">x</p>
    <h3>Upload software</h3>
    <input type="text" class="f1" name="name" placeholder="Software name" required>
    <input class="ch1" type="file" id="file" name="filer" required>
    <label for="file" class="f2">Upload software</label>
    <input class="ch2" type="file" id="file2" name="filer2" required>
    <label for="file2" class="f3">Upload image</label>
    <select name="bitss" class="bitss" required>
        <option value="0" selected="1">Version</option>
        <option value="32">32 [Bit]</option>
        <option value="64">64 [Bit]</option>
    </select>
    <input class="ch3" type="submit" name="submit" value="Upload">
</form>
<?php
    require_once 'dbh.php';
    if (isset($_POST['submit'])) {
        $getFileName = mysqli_real_escape_string($mysqli, $_POST['name']);
        $getFileNameRel = mysqli_real_escape_string($mysqli, $_POST['bitss']);
        $file = $_FILES['filer'];
        $fileName = $_FILES['filer']['name'];
        $fileTmpName = $_FILES['filer']['tmp_name'];
        $fileSize = $_FILES['filer']['size'];
        $fileError = $_FILES['filer']['error'];
        $fileType = $_FILES['filer']['type'];

        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));
        $uploadDate = date("M d Y");
        $allowed = array("exe", "zip", "msi", "rar");
            if (in_array($fileActualExt, $fileExt)) {
                if ($fileError === 0) {
                    if ($fileSize < 2000000000000) {
                        $fileNewName = uniqid('', true).".".$fileActualExt;
                        $fileDestination = "software/".$fileNewName;
                        $file1 = $_FILES['filer2'];
                        $fileName1 = $_FILES['filer2']['name'];
                        $fileTmpName1 = $_FILES['filer2']['tmp_name'];
                        $fileSize1 = $_FILES['filer2']['size'];
                        $fileError1 = $_FILES['filer2']['error'];
                        $fileType1 = $_FILES['filer2']['type'];

                        $fileExt1 = explode('.', $fileName1);
                        $fileActualExt1 = strtolower(end($fileExt1));
                        $allowed1 = array("jpeg", "jpg", "png");

                        if (in_array($fileActualExt1, $fileExt1)) {
                            if ($fileError1 === 0) {
                                if ($fileSize1 < 2000000000) {
                                    $fileNewName1 = uniqid('', true).
                                    ".".$fileActualExt1;
                                    $fileDestination1 = "software/img/".$fileNewName1;
                                    $stmt = mysqli_stmt_init($mysqli);
                                    $sql = "INSERT INTO softwares (name, img, file, bit, uploadDate) VALUES ('$getFileName', '$fileNewName1','$fileNewName', '$getFileNameRel', '$uploadDate')";
                                    if (!mysqli_stmt_prepare($stmt, $sql)) {
                                        header("Location: ../includes/the_areas/nanosoft?error=Software could not uploaded");
                                        exit();
                                    }else{
                                        mysqli_stmt_bind_param($stmt, "sssss", $getFileName, $fileNewName1, $fileNewName, $getFileNameRel, $uploadDate);
                                        mysqli_stmt_execute($stmt);
                                        move_uploaded_file($fileTmpName1, $fileDestination1);
                                        move_uploaded_file($fileTmpName, $fileDestination);
                                        header("Location: ../includes/the_areas/nanosoft?success=Software uploaded successfully!");
                                        exit();
                                    }
                                } else {
                                    echo "Your image is too big";
                                }
                            } else {
                                echo "There was an error uploading image";
                            }
                        } else {
                            echo "You cannot upload image of this type";
                        }
                    } else {
                        echo "Your movie is too big";
                    }
                } else {
                    echo "There was an error uploading software";
                }
            } else {
                echo "You cannot upload this type of software";
            }
    }else{
        header("Location: ../includes/the_areas/nanosoft?error");
        exit();
    }

我认为我的脚本有问题,但我无法弄清楚。

【问题讨论】:

  • 您应该检查 max_execution_timepost_max_sizeupload_max_filesize 设置 - 必要时覆盖。还有 - set_time_limit(0);
  • 您没有正确使用准备好的语句。你需要参数化你的变量。
  • @RamRaider 我认为它应该有所帮助
  • @Dharman 如果你不介意,你能告诉我怎么做吗?
  • 当然,请仔细阅读。您的 SQL 中有 PHP 变量,而不是占位符 ? stackoverflow.com/questions/7537377/…

标签: php html mysqli


【解决方案1】:

创建文件.htaccess添加如下代码

php_value upload_max_filesize 72M
php_value post_max_size 72M

它将增加您的上传限制(如果服务器允许通过.htaccess 更改 PHP 配置)。或者使用 PHP 来实现

ini_set('post_max_size', '72M');
ini_set('upload_max_filesize', '72M');

72M 是您允许的最大上传大小

【讨论】:

  • 问题是一些图像显示,但一些图像没有显示。我应该如何处理任何建议。
  • 很难建议,我只能说当我看到代码或图像故障时 - 但如果解决方案解决了您的问题,请接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
相关资源
最近更新 更多