为了账号安全,请及时绑定邮箱和手机立即绑定

表单上传文件无法显示

表单上传文件无法显示

PHP
繁星点点滴滴 2023-10-01 17:19:25
表单上传文件无法显示,这是我的表单上传代码<?php$imageinfo = getimagesize($_FILES['img']['tmp_name']);if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {echo "<center><br>Sorry, we only accept GIF and JPEG images</br><br>param name: img<br>u can upload with CSRF";exit;}$uploaddir = 'ex/';$uploadfile = $uploaddir . basename($_FILES['img']['name']);if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {echo "File is valid, and was successfully uploaded.\n";} else {echo "File uploading failed.\n";}?><form action="" method="post" enctype="multipart/form-data"><input type="file" size="20" name="img" /><input type="submit" name="upload" value="Upload" /></form>结果是Sorry, we only accept GIF and JPEG imagesparam name: imgu can upload with CSRF我想显示表单上传文件
查看完整描述

1 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

这将起作用,您应该指定正确的路径


<?php

   if($_SERVER["REQUEST_METHOD"] == "POST"){

      $imageinfo = getimagesize($_FILES['img']['tmp_name']);

      if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {

         echo "<center><br>Sorry, we only accept GIF and JPEG images</b><br>param name: img<br>u can upload 

         with CSRF";

         exit;

      }

      else{

         $uploaddir = '../path-to-move/';

         $uploadfile = $uploaddir . basename($_FILES['img']['name']);

         if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {

            echo "File is valid, and was successfully uploaded.\n";

         } else {

            echo "File uploading failed.\n";

         }

      }

}?>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">

<input type="file" size="20" name="img" />

<input type="submit" name="upload" value="Upload" />


查看完整回答
反对 回复 2023-10-01
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信