我正在编写一个脚本来将图像上传到我的应用程序.以下安全步骤是否足以使应用程序从脚本端安全?
这是我的脚本:
$filename=$_FILES['my_files']['name'];
$filetype=$_FILES['my_files']['type'];
$filename = strtolower($filename);
$filetype = strtolower($filetype);
//check if contain php and kill it
$pos = strpos($filename,'php');
if(!($pos === false)) {
die('error');
}
//get the file ext
$file_ext = strrchr($filename, '.');
//check if its allowed or not
$whitelist = array(".jpg",".jpeg",".gif",".png");
if (!(in_array($file_ext, $whitelist))) {
die('not allowed extension,please upload images only');
}
//check upload type
$pos = strpos($filetype,'image');
if($pos === false) {
die('error 1');
}
$imageinfo = getimagesize($_FILES['my_files']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' …Run Code Online (Sandbox Code Playgroud) 我开发了一个PHP脚本来替换当前的脚本,这将有很多曝光于各个市场/国家.其他人之间的脚本提供照片上传功能.
经过大量关于这个问题的阅读,我按照下面描述的方法.我非常感谢您对其安全性的评论.
img10000.jpg将储存在photos/a/f/0/img10000.jpg同时img10001.jpg将储存在photos/0/9/3/img10001.jpg.这是出于其他原因(使用子域进行静态内容服务或使用CDN).该脚本将在Linux专用服务器上运行.
我正在为我的网站实现基于用户的图像上传工具.系统应允许任何用户仅上传JPEG和PNG文件.当然,我担心安全问题,所以我想知道许多比我更聪明的人对以下允许上传的检查感觉如何:
1)首先列出PHP中允许的文件扩展名,仅允许PNG,png,jpg,JPG和JPEG.通过以下功能检索用户文件的扩展名:
return end(explode(".", $filename));
Run Code Online (Sandbox Code Playgroud)
这应该有助于禁止用户上传恶意内容,例如.png.php.如果通过,请转到步骤2.
2)在TMP文件上运行php函数getimageize().通过类似的东西:
getimagesize($_FILES['userfile']['tmp_name']);
Run Code Online (Sandbox Code Playgroud)
如果这不返回false,请继续.
3)确保.htaccess文件放在uploads目录中,以便该目录中的任何文件都无法解析PHP文件:
php_admin_value engine Off
Run Code Online (Sandbox Code Playgroud)
4)将用户的文件重命名为预先确定的内容.IE
$filename = 'some_pre_determined_unique_value' . $the_file_extension;
Run Code Online (Sandbox Code Playgroud)
这也有助于防止SQL注入,因为文件名将是所使用的任何查询中唯一的用户确定的变量.
如果我执行上述操作,我仍然容易受到攻击吗?在接受文件之前,我希望有1)只允许jpgs和png,2)验证PHP说它是有效图像,3)禁用图像所在的目录执行.php文件和4)将用户文件重命名为某些内容独特.
谢谢,
使用PHP,我想检查上传的图像是jpg,gif,png还是svg格式.我发现exif_imagetype函数可以检查文件格式,但它不支持svg格式.有没有办法在PHP中检测svg格式?
嗨,我正在尝试使用PHP脚本上传图像.什么是非常奇怪的是我只在Internet Explorer中得到以下错误,其他地方的脚本工作正常:
Warning: move_uploaded_file(pictures/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/tntauto1/public_html/admin_add1.php on line 59
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpcJnHZE' to 'pictures/' in /home/tntauto1/public_html/admin_add1.php on line 59
Warning: copy() [function.copy]: The first argument to copy() function cannot be a directory in /home/tntauto1/public_html/admin_add1.php on line 60
Run Code Online (Sandbox Code Playgroud)
这是脚本:
if(is_uploaded_file($_FILES['image']['tmp_name'])){
if($_FILES['image']['type'] == 'image/jpeg'){
$original = 'original_'.$v_id.'.jpg';
$large = 'large_'.$v_id.'.jpg';
$small = 'small_'.$v_id.'.jpg';
}elseif($_FILES['image']['type'] == 'image/gif'){
$original = 'original_'.$v_id.'.gif';
$large = 'large_'.$v_id.'.gif';
$small = 'small_'.$v_id.'.gif';
}else{
$error = 'Error: The …Run Code Online (Sandbox Code Playgroud)