为什么 PHP 看不到上传的文件?
P粉356361722
P粉356361722 2024-01-16 17:07:02
0
2
416

我有一个 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>



热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板