首頁  >  問答  >  主體

為什麼 PHP 看不到上傳的檔案?

我有一個 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粉356361722P粉356361722300 天前401

全部回覆(2)我來回復

  • P粉080643975

    P粉0806439752024-01-17 15:55:50

    您沒有提及正在上傳的文件類型。這是圖像上傳的範例。允許選擇多個影像。

    <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;}
          }
        }
      }

    回覆
    0
  • P粉321676640

    P粉3216766402024-01-17 00:50:31

    你忘記了 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>
    
    
    
    

    回覆
    0
  • 取消回覆