php - 验证上传的文件类型

标签 php mysql

为什么下面无法识别正在上传的图像(.jpg)?

我收到上传的文件不是图像!!!

$finfo = finfo_open(FILEINFO_MIME_TYPE, 'C:\xampp\php\extras\magic\magic.mime');
if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {    

  // prepare the image for insertion
  $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

  // put the image in the db...
  // database connection
  mysql_connect($host, $user, $pass) OR DIE (mysql_error());

  // select the db
  mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());

  // our sql query
  $sql = "INSERT INTO uploaded_images
  (image, name)
  VALUES
  ('{$imgData}', '{$_FILES['userfile']['name']}');";

  // insert the image
  mysql_query($sql) or die("Error in Query: " . mysql_error());
  $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>';
}
else
  $msg="<p>Uploaded file is not an image.</p>";

最佳答案

如果您无权访问 GD 库,您可以简单地使用 getimagesize(),如果它不是图像,它将返回 false。 http://php.net/manual/en/function.getimagesize.php

关于php - 验证上传的文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13624975/

相关文章:

javascript - wordpress 将数据添加到表中并生成新行

php - 从短代码函数内部调用 WordPress get_template_part 首先呈现模板

来自foreach的php随机顺序

PHP Mysqli - 参数绑定(bind)和转义字符串?

php 更新 mysql 表每一列中的日期,以便它们自动增加一天

php - 将可选输入表单插入数据库

php - 如何减去类型不同的行的同一列?

php - 可恢复的 fatal error : Object of class PDOStatement could not be converted to string in - index. php 第 23 行

php - 带有 session 或数据库的多页申请表?

php - 用户个人资料更新错误