要实现文件上传图片我们需要写出两个php文件,第一个php文件我们需要写出一个文件上传的页面,第二个php文件我们写出实现图片上传的功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>文件上传</h1> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传" /> </form> </body> </html>
<?php var_dump($_FILES["file"]); // 限制文件的类型 // 限制文件的大小 // 防止文件名重复 // 一.防止文件名重复 // 1.修改文件名 // 流水号,时间戳+随机数+用户名 // 2.建文件夹 // upload/20170317/lch/shangchuan/11.jpg // 3.保存文件 if($_FILES["file"]["error"]) { echo $_FILES["file"]["error"]; } else { //没有出错 //加限制条件 if(($_FILES["file"]["type"]=="image/png" || $_FILES["file"]["type"]=="image/jpeg") && $_FILES["file"]["size"]<1024000) { //防止文件名重复 $filename = "./img/".time().$_FILES["file"]["name"]; //转码 $filename = iconv("UTF-8","gb2312",$filename); if(file_exists($filename)) { echo "该文件已存在"; } else { //保存文件 move_uploaded_file($_FILES["file"]["tmp_name"],$filename); } } else { echo "文件类型不对"; } }
Related articles:
The mobile terminal implements the file upload function through HTML5
JavaScript Advanced (9) JS implements local file upload to Alibaba Cloud Server
Detailed introduction to file upload of HTML5 application
The above is the detailed content of File upload image in php. For more information, please follow other related articles on the PHP Chinese website!