Warum kann PHP die hochgeladene Datei nicht sehen?
P粉356361722
P粉356361722 2024-01-16 17:07:02
0
2
445

Ich habe ein index.phpupload.php。在 index. 中是一个 form action="upload.php" ,其中包含 input type="file" id="file" name="file"-Tag. Der PHP-Code lautet:

<?php
$file = $_FILES['file'];
print_r($file);
echo "test";
?>

Aus irgendeinem Grund zeigt es, dass echoprint_r() nicht funktioniert.

Ich habe versucht, den Code neu zu schreiben. Das Hinzufügen anderer Bezeichner in index.php 内为 input-Tags funktioniert aber immer noch nicht. Was habe ich falsch gemacht?

P粉356361722
P粉356361722

Antworte allen(2)
P粉080643975

您没有提及正在上传的文件类型。这是图像上传的示例。允许选择多个图像。

<form action="upload.php" method="post" enctype="multipart/form-data">
Get a Photo from your device <br>
<input type="file" name="image1[]" multiple accept="image/png, image/jpeg, image/gif, image/webp" /><br>
<button type="submit">Upload Photo</button>
</form>

以及上传PHP:

foreach ($_FILES["image1"]["error"] as $key => $error) {
    if ($error == 0 ) {
        $tmp_name = $_FILES["image1"]["tmp_name"][$key];
        $name = basename($_FILES["image1"]["name"][$key]);
        $result = move_uploaded_file($tmp_name, "$uploads_dir/$name");
        if($result == false){echo "not moved\n";}
    }
    if($error > 0){echo "error $error\n";}
}

要确定上传的图像类型:

switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/webp':
    $image = @imagecreatefromwebp($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['image1']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image1']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'ebp':
        $image = @imagecreatefromwebp($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
P粉321676640

你忘记了 enctype="multipart/form-data"

尝试这个版本

<form action="upload.php" method="post" enctype="multipart/form-data">
  <p><input type="file" name="file">
  <p><button type="submit">Submit</button>
</form>



Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage