felayman——PHP中图片上传到服务器_PHP教程
//该文件负责获取上传的图片的扩展名和随机生成文件名
header("content-type:text/html;charset=utf-8");
/**
* 获取文件扩展名
*Enter description here ...
* @param unknown_type $filename
*/
function getFileName($filename){
//strrchr— 查找指定字符在字符串中的最后一次出现
return substr(strrchr($filename,'.'), 1);
}
/**
* 随机生成n位字符串
* Enterdescription here ...
* @param unknown_type $num
*/
function rand_str($num){
$str = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKOLP1234567890";
$str_len = strlen($str)-1;
//echo$str_len;
$s='';
for ($i = 0; $i
$s.=$str[rand(0,$str_len)];
}
echo $s;
}
?>
2.index.html
3.deal_upload.php
header("content-type:text/html;charset=utf-8");
//引入获取上传文件的名称已经生成文件名的函数lib
require 'upload_file.php';
//设置文件要保存的目录
$upload_dir = "files/";
if(!file_exists($upload_dir)){
mkdir($upload_dir);
}
//设置运行上传的文件类型
$type =array('jpg','png','gif','jpeg');
//in_array — 检查数组中是否存在某个值
if(!in_array(strtolower(getFileName($_FILES['file']['name'])), $type)){
//implode,将数组联合成一个字符串
$text = implode(',', $type);
echo "";
}else{
//获取文件名称
$filename = explode('.', $_FILES['file']['name']);
$filename[0] =rand_str(10);
$name =implode('.', $filename);
http://blog.csdn.net/u012332735/article/details/$uploadfile=$upload_dir.$name;
//is_uploaded_file — 判断文件是否是通过 HTTP POST上传的
if(is_uploaded_file($_FILES['file']['tmp_name'])){
//move_uploaded_file— 将上传的文件移动到新位置
if(move_uploaded_file($_FILES['file']['tmp_name'],http://blog.csdn.net/u012332735/article/details/$uploadfile)){
$file_path = getcwd().'\\'.http://blog.csdn.net/u012332735/article/details/$uploadfile; echo "
echo "
";
echo "继续上传";
}else{
echo "上传失败";
}
}
}
?>上一篇: 损失函数总结
下一篇: php生成xml实例方法