我想限制用户只从服务器端上传我想要的图像类型文件。我的代码中缺少一些东西可以帮助我。提前致谢 我做了客户端限制 这可以在某种程度上限制用户,但如果用户在浏览按钮单击期间选择所有文件,则可以上传任何内容。
<?php
//image script
if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$image = fread($fp, filesize($tmpName));
$image = addslashes($image);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";
$conn=mysqli_connect($servername, $username, $password, $dbname);
$query = "INSERT INTO upload (name, size, type, image,passport_no,dateofissue,dateofexpiry,placeofissue ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$image','".$passno."','".$doi."','".$doe."','".$poi."')";
mysqli_query($conn,$query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
?>
答案 0 :(得分:1)
为什么不使用accept
HTML属性?
<form action="form-handler.php" method="post" enctype="multipart/form-data">
<div>
<input id="myfile" name="myfile" type="file" accept="image/*">
</div>
</form>
&#13;
答案 1 :(得分:0)
if ((($_FILES['userfile']['type'] == 'image/gif') || ($_FILES['userfile']['type'] == 'image/jpeg') || ($_FILES['userfile']['type'] == 'image/png'))
{
image file code
}
else
{
error message code
}
在php中尝试使用此代码进行图像验证。
答案 2 :(得分:0)
您可以查看已上传的文件类型
喜欢
$type = explode('/', $_FILES['file']['type']);
if($type[0] == "image") {
//do something here
}