為什麼 PHP 看不到上傳的檔案?
P粉356361722
P粉356361722 2024-01-16 17:07:02
0
2
411

我有一個 index.phpupload.php。在index. 中是一個form action="upload.php" ,包含input type="file" id="file" name="file" 標籤。 PHP程式碼是:

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

由於某種原因,它顯示 echoprint_r() 不起作用。

我嘗試重寫程式碼。在 index.php 內為 input 標記添加其他標識符,但仍然不起作用。我做錯了什麼?

P粉356361722
P粉356361722

全部回覆(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>



熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板