HTML 表单不会将一些 POST 变量发送到 PHP 脚本

HTML Form just won't send some POST variables to a PHP script

所以我遇到了一个问题,阻止我上传文件作为对我的新上传服务的测试。

每次我正确填写每个表单并提交时,有一堆变量没有为上传程序设置。

HTML 表格(从我的网站截取,我留下了一些 bootstrap 元素以防万一):

<form action="upload.php" method="post">
            <span style="color: green;">Select the file you want to upload:</span>
            <input type="file" class="form-control" name="fileToUpload" id="fileToUpload" />
            <br>TIP: Taking a long time for the page to load after uploading? That's your browser uploading the files. It will take time if you have slow internet or you are uploading a big file.
            <br><br>
            <b><span style="color: red;">DISCLAIMER: I, Mark Jones, am not held responsible for the uploading of porn, pirated music, images and films. In the event that any pirated, offensive or sexual files are uploaded they will be deleted by staff or removed by storage clean-ups. Please keep all these types of files that are not permitted away from this website. Only upload things that are not sold elsewhere and keep porn on your own PC. Once you upload a file against this disclaimer after accepting it (You can't upload files without accepting it), The uploaded file becomes your responsibility. It will be deleted and if it is not deleted then you'll have to witness a punishment that hasn't been put into effect by me, but by others such as my hosting or the DMCA. I also would like all the members/users of this site not to upload Viruses, Tojans, Malware and Spyware. If you do, It will be deleted or other punishments will occur. Also by accepting this disclaimer you must use your real name in the box below.</span></b><br><br>
            <!--<input type="submit" value="Upload Image" name="submit" />-->
            <input type="radio" name="disclaimer" value="1">Accept</input>
            <input type="radio" name="disclaimer" value="0" checked>Decline</input><br><br>
            Insert your full real name below:<br>
            <input type="text" class="form-control" name="realname" value="Full name"></input><br>
            Do you want your own personal folder for your file?<br>
            <input type="radio" name="pf" value="1">Yes</input>
            <input type="radio" name="pf" value="0" checked>No</input><br>
            <input type="text" class="form-control" name="pfn" value="Personal Folder Name"></input><br><br>
            Please tag your file:<br>
            <input type="radio" name="tag" value="other" checked>Other</input>
            <input type="radio" name="tag" value="funny">Funny</input><br>
            <input type="radio" name="tag" value="theoretic">Theoretic</input>
            <input type="radio" name="tag" value="sad">Sad</input><br>
            <input type="radio" name="tag" value="important">Important</input>
            <input type="radio" name="tag" value="informative">Informative</input><br>
            <input type="radio" name="tag" value="family">Family</input>
            <input type="radio" name="tag" value="friendly">Friendly</input><br>
            <input type="radio" name="tag" value="dangerous">Dangerous</input>
            <input type="radio" name="tag" value="horrible">Horrible</input><br>
            <input type="radio" name="tag" value="scary">Scary</input>
            <input type="radio" name="tag" value="musical">Musical</input><br>
            <input type="radio" name="tag" value="creative">Creative</input>
            <input type="radio" name="tag" value="ugly">Ugly</input><br>
      </div>
      <div class="modal-footer">
      <b>Uploader breaks if ", ', / or \ is used anywhere.</b>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel request</button>
        <input type="submit" id="uploadsubmit" class="btn btn-primary" value="Upload" name="submit"></input>
        </form>

接下来是我的 PHP 处理程序 (upload.php):

<center>
<?php

if(isset($_POST["disclaimer"]) && isset($_POST["realname"])) {

if($_POST["disclaimer"] != "1") {
    die("You didn't accept the disclaimer. Go back a page and read the disclaimer.");
}

if($_POST["realname"] == "Full name") {
    die("You didn't enter your real name. Go back and try again.");
} 

if($_POST["realname"] == "") {
    die("You didn't enter your real name. Go back and try again.");
}

} else {

    echo "Uh Oh! Something is going wrong! Attempting to repair.<br>";
    $_POST["realname"] == "real-name-post-error";

}


$personalfolder = $_POST["pf"];

//Base file names
$base_file_name = basename($_FILES["fileToUpload"]["name"]);
$base_file_name_path = $_FILES["fileToUpload"]["name"];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

//Main script
echo "File(s) uploaded. Reading file list...<br>";
if($personalfolder == "1") {
$target_dir_ext = time()."_".basename($_FILES["fileToUpload"]["name"])."_".$ip.$_POST["realname"]."/".$_POST["pfn"]; // Prevent file already existing
} else {
$target_dir_ext = time()."_".basename($_FILES["fileToUpload"]["name"])."_".$ip.$_POST["realname"]; // Prevent file already existing
}
$structure = "uploads/".$_POST["tag"]."/".$target_dir_ext;
mkdir($structure, 0777, true);
$target_dir = "uploads/".$_POST["tag"]."/".$target_dir_ext."/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
echo "File(s) detected: <br>".$target_file."<br>";
echo "Checking file(s)...<br>";

$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.<br>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 99999999) {
    echo "Sorry, your file is too large.<br>";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" 
&& $imageFileType != "png" 
&& $imageFileType != "jpeg"
&& $imageFileType != "GIF"
&& $imageFileType != "JPG"
&& $imageFileType != "JPEG"
&& $imageFileType != "PNG"
&& $imageFileType != "mp3"
&& $imageFileType != "MP3"
&& $imageFileType != "WAV"
&& $imageFileType != "wav"
&& $imageFileType != "mp4"
&& $imageFileType != "MP4"
&& $imageFileType != "swf"
&& $imageFileType != "SWF"
&& $imageFileType != "ogg"
&& $imageFileType != "OGG"
&& $imageFileType != "js"
&& $imageFileType != "JS"
&& $imageFileType != "txt"
&& $imageFileType != "TXT"
&& $imageFileType != "exe"
&& $imageFileType != "EXE"
&& $imageFileType != "zip"
&& $imageFileType != "ZIP"
&& $imageFileType != "rar"
&& $imageFileType != "RAR"
&& $imageFileType != "msi"
&& $imageFileType != "MSI"
&& $imageFileType != "ttf"
&& $imageFileType != "TTF"
&& $imageFileType != "svg"
&& $imageFileType != "SVG"
&& $imageFileType != "eot"
&& $imageFileType != "EOT"
&& $imageFileType != "woff"
&& $imageFileType != "WOFF"
&& $imageFileType != "woff2"
&& $imageFileType != "WOFF2"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG, GIF, OGG, MP3, MP4, WAV and SWF files are allowed.<br>";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded due to the reasons above.<br>";
// if everything is ok, try to upload file
} else {
    echo "File(s) OK. Attempting to move uploaded file(s) to the uploads directory...";
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.<br>";
        $ul = "http://marksrtz.site50.net/uploader/".$target_file;
        echo "File URL: <a href='".$ul."'>".$ul."</a><br>";
        if($imageFileType == "jpg" 
        || $imageFileType == "png" 
        || $imageFileType == "PNG" 
        || $imageFileType == "gif" 
        || $imageFileType == "GIF" 
        || $imageFileType == "jpeg" 
        || $imageFileType == "JPEG" 
        || $imageFileType == "JPG") {
            echo 'Preview:<br><iframe width="1440px" height="900px" src="'.$ul.'"></iframe> ';
        } else {
            echo 'Preview:<br><iframe width="600px" height="400px" src="'.$ul.'"></iframe> ';
        }
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
echo "<br>PHP Script was executed and ended correctly.";
?> 
</center>

可能是什么问题? PHP 脚本的输出是:

File(s) uploaded. Reading file list... File(s) detected: uploads/funny/1422113393__188.29.64.63Mark Jones/ Checking file(s)... Sorry, file already exists. Sorry, only JPG, JPEG, PNG, GIF, OGG, MP3, MP4, WAV and SWF files are allowed. Sorry, your file was not uploaded due to the reasons above.

PHP Script was executed and ended correctly.

它之前没有检测到的是 $_POST["disclaimer"]$_POST["realname"] 但后来才开始工作

现在它根本不发送我要上传的文件。

但是,如果我使用 enctype,它会发送文件,但不会发送 POST 变量。有没有一种方法可以同时发送 POST 变量和文件而无需创建另一个表单?

首先 - 不要把 POST 变量当作它们的本来面目! 总是检查它们

而不是

$个人文件夹 = $_POST["pf"];

例如使用

$personalfolder = trim(strip_tags(stripslashes($_POST["pf"])));

我将继续进行回答。

000webhost 最有可能有一个默认的 2M 上传最大值。

您可以使用 .htaccess 覆盖它并将其值更改为

php_value memory_limit 30M
php_value post_max_size 100M
php_value upload_max_filesize 30M

例如,放置在服务器的根目录中。由于您无法修改他们的 php.ini 文件。

根据 Google 上为您所做的一些调查结果。

http://www.000webhost.com/forum/customer-assistance/202-file-upload-limit-2mb-set-php-ini.html

如其所述我们不允许在服务器上上传大文件(文件大小超过 5 MB) 在我的组件(图片库)的后端有一个声明,上传文件限制为 2Mb - 由 php.ini 设置 我使用 Joomla。有人知道如何将它增加到 5Mb 吗? 它是在服务器上还是在我的文件系统中的某个地方?

本主机上传文件限制为2Mb,可以到phpinfo()查看。

另外,仅供讨论:

表单上传时需要有效的enctype;你没有。

将您的表格更改为:

<form action="upload.php" method="post" enctype="multipart/form-data">