Home > php教程 > php手册 > PHP 图片上传实现代码 带详细注释

PHP 图片上传实现代码 带详细注释

WBOY
Release: 2016-06-06 20:33:38
Original
1403 people have browsed it

PHP 图片上传实现代码 带详细注释,喜欢的朋友可以学习下。

代码如下:
//用户上传图片处理文件
if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "
"; //出错返回
}else{
/* //这是上传图片的信息,去掉前后的注释就可以看到效果.
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; */
if (file_exists("userupload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],"userupload/" . $_FILES["file"]["name"]);
}

$date=date('Ymdhis'); //得到当前时间,如;20070705163148
$fileName=$_FILES['file']['name']; //得到上传文件的名字
$name=explode('.',$fileName); //将文件名以'.'分割得到后缀名,得到一个数组
$newPath=$date.'.'.$name[1]; //得到一个新的文件为'20070705163148.jpg',即新的路径
$oldPath=$_FILES['file']['tmp_name']; //临时文件夹,即以前的路径
rename("userupload/".$fileName,"userupload/".$newPath);

//这里可以写你的SQL语句,图片的地址是 "userupload/".$newPath

?>

}
}else{
echo "Invalid file"; //图片类型错误或者过大
}
?>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template