从表单

时间:2016-05-13 02:13:32

标签: php file-upload html-form-post


我在网站上上传图片时遇到了一些麻烦。我有一个上传页面,可让人们将条目上传到手册中。但是当我上传图片时,$ _FILES数组是空的。我有:

  • 检查上传是否已开启
  • 调整后的最大尺寸允许为20M
  • 已添加enctype="multipart/form-data"
  • 检查图像的大小(38KB)
  • 确保我使用了名称属性
  • 将临时文件夹的权限更改为777
  • 确保我使用了POST
  • 检查我是否有足够的可用空间(20GB可用)
  • 代码中可以看到的其他内容
  • 我调整了代码,并且在没有图片的情况下提交填充$ _FILES,并按预期填充空值。

我已在我的服务器here上传了phpinfo()的输出。

无论我在哪里,都没有任何帮助。请注意,我在本地计算机上运行此程序,Mac(10.11.4)与Apache 2.4.18和PHP 5.5.31。

我使用Why would $_FILES be empty when uploading files to PHP?的答案来检查这些事情。

以下是代码:

上传页面(upload.php):

<?php include "sql.inc"; ?>
<html>
    <head>
        <title>The Emerson Nature Center Official Handbook | Upload</title>
        <link rel="stylesheet" href="page.css" type="text/css">
        <iframe src="navigator.php"></iframe>
        <style>
            textarea#styled {
                width: 600px;
                height: 120px;
                border: 3px solid #cccccc;
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <h1>Upload an entry to the Handbook!</h1>
        <center>
            <form action="upload_page.php" method="POST" enctype="multipart/form-data">
                <h2>Title: <input type="text" name="title" placeholder="Title" style="font-size: 18pt;" required></h1>
                <input type="hidden" name="author" value="<?php if ($canadd) echo $db->querySingle("SELECT name FROM passwords WHERE id=".$_COOKIE['userid']); ?>">
                <h3 style="entry">Author: <?php if ($canadd) echo $db->querySingle("SELECT name FROM passwords WHERE id=".$_COOKIE['userid']); ?>
                <textarea id="styled" width="300" height="300" placeholder="text" name="entry" required></textarea>
                <br>
                <h3>Insert images here: </h3>
                Image 1 (required): <input type="file" name="image1" accept=".png, image/png" required><br>
                Image 2 (optional): <input type="file" name="image2" accept=".png, image/png"><br>
                <input type="submit" value="submit">
            </form>
            <br>
        </center>
    </body>
</html>

上传处理程序(upload_page.php):

<?php
include "sql.inc";
$target_dir = "images/";
$images = scandir($target_dir);
$last_id = ltrim(end($images), "id-");
$last_id = rtrim($last_id, ".png");
$firstid = intval($last_id) + 1;
$target_file = $target_dir . "id-" . $firstid . ".png";
$uploadOk = 1;
$typeAllowed = array("image/png");
echo 'Array:<br>';
var_dump($_FILES);
//$check = getimagesize($_FILES["image1"]["tmp_name"]);
//if ($check !== false) {
//  $uploadOk = 1;
//} else {
//  echo "The first file you uploaded is not a valid PNG image.";
//  $uploadOk = 0;
//}
if ($_FILES["image1"]["size"] > 500000) {
  echo "Sorry, your first file is too large. Your size was " .     $_FILES["image1"]["size"] . " bytes.";
  $uploadOk = 0;
}
if (exif_imagetype($_FILES['image1']['tmp_name']) != IMAGETYPE_PNG){
  echo 'You can only upload PNG images. (first file) Your type was "' . $_FILES['image1']['type'] . '".';
  $uploadOk = 0;
}
if ($uploadOk == 0) {
  echo "Sorry, your first file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["image1"]["tmp_name"], $target_file)) {
    $uploadOk = 1;
  } else {
     echo "Sorry, there was an error uploading your first file.";
  }
}
if (isset($_FILES['image2'])) {
  $target_dir = "images/";
$secondid = $firstid + 1;
$target_file = $target_dir . "id-" . $secondid . ".png";
$uploadOk = 1;
//$check = getimagesize($_FILES["image2"]["tmp_name"]);
//if ($check !== false) {
//} else {
//  echo "The second file you uploaded is not a valid PNG image.";
//  $uploadOk = 0;
//}
if ($_FILES["image2"]["size"] > 500000) {
  echo "Sorry, your second file is too large. Your size was " .     $_FILES["image2"]["size"] . " bytes.";
  $uploadOk = 0;
}
if (exif_imagetype($_FILED['image2']['tmp_name']) != IMAGETYPE_PNG){
  echo 'You can only upload PNG images. (second file) Your type was ' . $_FILES['image2']['type'];
  $uploadOk = 0;
}
if ($uploadOk == 0) {
  echo "Sorry, your second file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["image2"]["tmp_name"], $target_file)) {
    $uploadOk = 1;
  } else {
     echo "Sorry, there was an error uploading your second file.";
  }
}
}
if (isset($secondid)) $secondid = ",".$secondid;
else $secondid = "";
if ($uploadOk == 1) $db->exec('INSERT INTO handbook (id,title,author,entry,imageids) VALUES (NULL,"'.$_POST["title"].'","'.$_POST["author"].'","'.$_POST["entry"].'","'.$first    id.','.$secondid.'")');
else echo "Your entry was not added.";
header(""); //to send headers to stop redirect
flush(); // ---^
if (!headers_sent()) header("Location: view.php?id=".$db->querySingle("SELECT id FROM handbook WHERE title='".$_POST['title']."'"));
else echo 'Please press the back button on your browser.';
?>
<html>
  <head>
    <title>Uploading...</title>
  </head>
</html>

如果您需要额外的代码,日志或php.ini条目,请告诉我,我将获得必要的代码。

0 个答案:

没有答案