只允许.jpg和.png文件 [英] Only allow .jpg and .png files only

查看:322
本文介绍了只允许.jpg和.png文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何仅允许上传某些文件类型到php?

Possible Duplicate:
How can I only allow certain filetypes on upload in php?

如何使下面的脚本仅允许.jpg图像或.png图像? 它将基于会话用户名匹配将映像目录上载到mysql表.是否可以限制文件类型?因为我只需要.jpg或.png

How can I make the script below only allow .jpg images or .png images? It uploads an image directory to a mysql table based on a session username match. Is it possible to restrict the file types? Because I only need .jpg or .png

 if ($_POST['submit']) {
    //get file attributes

    $Name = $_FILES['myfile']['name'];
    $tmp_name = $_FILES['myfile']['tmp_name'];
    error_reporting(E_ERROR | E_PARSE);

    if ($Name) {            
    $location = "avatars/$Name";    
    move_uploaded_file($tmp_name, $location ) or die('Failed to upload picture'); 
    $query = mysql_query("UPDATE fgusers3 SET imagelocation='$location' WHERE name='$name'") or die("Your profile image has been uploaded!");

   }}



echo "Upload your image below:
  <form action='profilepic.php' method='POST' enctype='multipart/form-data'>
   Profile Picture: <input type='file' name='myfile'> <input type='submit' name='submit' value='upload'>
   </form>";

推荐答案

您可以尝试使用pathinfo& exif_imagetype

You can try using pathinfo & exif_imagetype

if(pathinfo($Name,PATHINFO_EXTENSION) != "jpg" || exif_imagetype($Name) != IMAGETYPE_JPEG)
{
    // throw error 
}

查看更多信息以检测假图像

这篇关于只允许.jpg和.png文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆