为什么PHP看不到上传的文件?

yb3bgrhw  于 2022-10-30  发布在  PHP
关注(0)|答案(3)|浏览(165)

我有一个index.phpupload.php。在index.php中有一个form action="upload.php",其中有input type="file" id="file" name="file"标记。PHP代码是:

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

由于某种原因,它显示echo,但print_r()不起作用。
我试着重写代码。在index.php中为input标签添加了其他标识符,但仍然不起作用。我做错了什么?

0ve6wy6x

0ve6wy6x1#

您忘记了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>
</body>
</html>
pcww981p

pcww981p2#

您没有提到上传的文件类型。下面是一个上传图像的示例。允许选择多个图像。

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

iugsix8n3#

假设你已经可以上传文件了,比如使用如下形式:

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

然后,如果你想在接收端“upload.php”显示图像,你可以用途:
如果图像为JPG/JPEG类型:

<?php
$string1= base64_encode(file_get_contents($_FILES["file"]["tmp_name"]));

echo '<img src="data:image/jpeg;base64, ' .  $string1  . '"/>';

?>

如果图像为PNG类型:

<?php
$string1= base64_encode(file_get_contents($_FILES["file"]["tmp_name"]));

echo '<img src="data:image/png;base64, ' .  $string1  . '"/>';

?>

注意:实际上我们通常把上传的文件保存在服务器中(以备以后显示),或者把二进制数据存储到db表中的BLOB字段中,但上面的代码是为了演示如何做才能立即显示上传的图片。

相关问题