这个是发送页面send.php
<form method="POST" name='sadf' action="upload_img.php" enctype="multipart/form-data">
<p align="center"><input type="file" name="upload_img"/><input type="submit" value="submit"/></p>
</form>
<?phpprint_r($_FILES);if($_FILES['upload_img']['name']){
require_once("plugin/image.class.php");
$image=new image(); $image->check_file($_FILES['upload_img']);
$img_url=$image->upload_file($_FILES['upload_img']);
echo "<input id='dd' type='hidden' value='$img_url'/><p align='center'><img src='images/user/$img_url' width='350px' /></p>";
}
?>这个是图片上传类image.class.php
<?php
/**
* 上传类
*/
class image
{
/**
* 验证图片
*
* @param $file 获取的文件对象($_FILES)
*/
public static function check_file($file)
{
$allow_file_ext_list = array(1=>'image/jpeg' , 2=>'image/gif' , 3=>'image/pjpeg');
$allow_file_max_size = 1024*1024;
//验证上传图片类型
if (array_search($file['type'] , $allow_file_ext_list) == false)
{
exit;
}
if ($allow_file_max_size < $file['size'])
{
show_alert('上传图片大小超过1M,系统只允许上传1M的图片');
}
}
/**
* 上传文件
*
* @param $file 获取的文件对象($_FILES)
* @param $filePath 可选参数,即是否存在分级目录下
*/
public static function upload_file($file , $file_path = '')
{
$destination_path = ($file_path == '') ? ("images/user/") : ($file_path);
if(!file_exists($destination_path))
{
mkdir($destination_path , 0777);
} $file_name = get_datetime('YmdHis') . '_' . get_cookie('sysuserid') . substr( basename($file['name']) , strpos(basename($file['name']),'.') ); if (move_uploaded_file($file['tmp_name'], $destination_path . $file_name)) {
return $file_name;
}
return '';
}
//删除图片
public static function del_file($filepath){
if(unlink($filepath)){
return true;
}else{
return false;
}
}
}function get_datetime($string_format='Y-m-d H:i:s'){
global $timestamp;
return gmdate($string_format,time()+3600*8);
}
function get_cookie($name){ global $_COOKIE,$cookieprename;
if (isset($_COOKIE[$cookieprename.$name])) {
return urldecode($_COOKIE[$cookieprename.$name]);
}
return false;
}?>麻烦大家耐心看下帮我分析下
<form method="POST" name='sadf' action="upload_img.php" enctype="multipart/form-data">
<p align="center"><input type="file" name="upload_img"/><input type="submit" value="submit"/></p>
</form>
<?phpprint_r($_FILES);if($_FILES['upload_img']['name']){
require_once("plugin/image.class.php");
$image=new image(); $image->check_file($_FILES['upload_img']);
$img_url=$image->upload_file($_FILES['upload_img']);
echo "<input id='dd' type='hidden' value='$img_url'/><p align='center'><img src='images/user/$img_url' width='350px' /></p>";
}
?>这个是图片上传类image.class.php
<?php
/**
* 上传类
*/
class image
{
/**
* 验证图片
*
* @param $file 获取的文件对象($_FILES)
*/
public static function check_file($file)
{
$allow_file_ext_list = array(1=>'image/jpeg' , 2=>'image/gif' , 3=>'image/pjpeg');
$allow_file_max_size = 1024*1024;
//验证上传图片类型
if (array_search($file['type'] , $allow_file_ext_list) == false)
{
exit;
}
if ($allow_file_max_size < $file['size'])
{
show_alert('上传图片大小超过1M,系统只允许上传1M的图片');
}
}
/**
* 上传文件
*
* @param $file 获取的文件对象($_FILES)
* @param $filePath 可选参数,即是否存在分级目录下
*/
public static function upload_file($file , $file_path = '')
{
$destination_path = ($file_path == '') ? ("images/user/") : ($file_path);
if(!file_exists($destination_path))
{
mkdir($destination_path , 0777);
} $file_name = get_datetime('YmdHis') . '_' . get_cookie('sysuserid') . substr( basename($file['name']) , strpos(basename($file['name']),'.') ); if (move_uploaded_file($file['tmp_name'], $destination_path . $file_name)) {
return $file_name;
}
return '';
}
//删除图片
public static function del_file($filepath){
if(unlink($filepath)){
return true;
}else{
return false;
}
}
}function get_datetime($string_format='Y-m-d H:i:s'){
global $timestamp;
return gmdate($string_format,time()+3600*8);
}
function get_cookie($name){ global $_COOKIE,$cookieprename;
if (isset($_COOKIE[$cookieprename.$name])) {
return urldecode($_COOKIE[$cookieprename.$name]);
}
return false;
}?>麻烦大家耐心看下帮我分析下
$image->check_file($_FILES['upload_img']); 这段判断上传的是不是图片 只能上传gif和jpg/jpeg图片
我怀疑他可以上传别的类型文件
他直接把后缀改了,你获取到的$_FILES['upload_img']['type']就改变了!如果你确实需要获取文件的类型的话
参考http://user.qzone.qq.com/9399428/blog/1290052850
转载的文章!再退一部分!你精确判断到文件类型之后一样不能排除病毒的植入,当你下载到病毒文件,执行的时候会出现内存溢出,然后执行病毒部分!
http://www.php.net/manual/en/features.file-upload.post-method.php
PHP端检查能给个类子么
建议还是通过判断实际上传文件的扩展名来过滤吧?至少,如果是PHP程序,被改成了.JPG,就算传上来,也无法执行,除非要有另一个程序来进行改名。
而另一个程序的上传,则又会有其他方面的限制了。