php文件上传可作为两个单独的文件,但不能在单个帖子中

时间:2018-11-28 07:55:41

标签: php post upload mamp

我希望我知道如何解释这个问题。我在这里使用w3schools PHP文件上传模板:https://www.w3schools.com/php/php_file_upload.asp

完全复制为两个文件时,它可以工作。当我尝试将逻辑合并到发布到其自身的文件中时,它会异常失败。首先,这是我的代码:

<?php

require_once('../../private/initialize.php');

if(is_post_request()) {

  $submission = [];
  $submission["id"]         = $_POST['id'] ?? '';
  $submission["email"]    = $_POST['email'] ?? '';
  $submission["theme_id"]   = $_POST['theme_id'] ?? '';

  // Crazy validation for images
  $target_dir     = "uploads/";
  $target_file    = $target_dir . basename($_FILES["image"]["name"]);
  $uploadOk       = 1;
  $imageFileType  = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

  var_dump($_FILES);
  echo "<br><br>";

  if($_FILES["image"]["error"] > 0) {
    echo "Error: " . $_FILES["image"]["error"] . "<br />";
  } else {
        echo "Upload: " . $_FILES["image"]["name"] . "<br />";
        echo "Type: " . $_FILES["image"]["type"] . "<br />";
        echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
        echo "Stored in: " . $_FILES["image"]["tmp_name"];
  }

  echo "<br><br>";

  if(is_writable($_FILES["image"]["name"])) {
    echo "Writeable!";
  }
  else {
    echo "Not writeable!";
  }

  echo "<br>";

  // Check if image file is an actual image or a fake image
  $check = getimagesize($_FILES["image"]["name"]);

  echo "<br>";

  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  }
  else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// This will set the form values to new. 
else {
  $submission = [];
  $submission["id"]         = '';
  $submission["email"]    = '';
  $submission["theme_id"]   = '';
  $submission["image_link"] = '';
}

?>

<?php $page_title = 'Submission Form'; ?>
<?php include(SHARED_PATH . '/admin_top.php'); ?>

<div id="content">
    <button onclick="location.href='index.php'">Back to List</button>

  <div class="theme">
    <h1>Submit an Entry</h1>

    <!-- Prepare sql data for submission form -->
    <?php echo display_errors($errors); ?>
    <?php $challenge_list = get_theme_challenges(); ?>
    <?php $theme_id = get_current_theme_id(); ?>

    <form action="<?php echo url_for('/submissions/new.php'); ?>" method="post" enctype="multipart/form-data">

      <dl>
        <dt>Email Address:</dt>
        <dd><input type="email" name="email" value="<?php echo $submission['email']; ?>" /></dd>
      </dl>

      <dl>
         <dt>Challenge:</dt>
        <dd>
          <?php foreach($challenge_list as $challenge) {?>
            <input type="radio" name="challenge" value=""><?php echo($challenge);?><br>
          <?php } ?>
        </dd>
       </dl>

      <input type="hidden" name="theme_id" value="<?php echo $theme_id; ?>"/>

      <dt>Image:</dt>
      <dd><input type="file" name="image" id="image"></dd>
      <br><br>
      <div id="submit">
        <input type="submit" value="Submit Entry" name="submit" />
      </div>

    </form>
  </div>
</div>

这是我奇怪的结果:


array(1) { ["image"]=> array(5) { ["name"]=> string(10) "Thanos.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(45) "C:\Users\theaq\AppData\Local\Temp\php5EBF.tmp" ["error"]=> int(0) ["size"]=> int(35900) } } 

Upload: Thanos.png
Type: image/png
Size: 35.05859375 Kb
Stored in: C:\Users\theaq\AppData\Local\Temp\php5EBF.tmp

Not writeable!

Warning: getimagesize(Thanos.png): failed to open stream: No such file or directory in C:\MAMP\htdocs\sketchy\public\submissions\new.php on line 42

File is not an image.

不确定如何使自己更漂亮,但这是我一直在尝试收集的所有信息。

1 个答案:

答案 0 :(得分:0)

哦,我的天哪。

很明显,在我发布问题后几乎立即,我开始尝试注释掉与众不同的部分,发现这一行打破了一切:

$submission["theme_id"]   = $_POST['theme_id'] ?? '';

因为我在POST变量中没有该字段。

我很as愧。