Through PHP, files can be uploaded to the server
We will demonstrate it through code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>文件上传</title> </head> <body> <form method="post" action="file.php" enctype="multipart/form-data" name="uploadfile"> <label>文件名</label> <input type="file" id="upfile" name="upfile"></br></br></br> <input type="submit" value="提交"> </form> </body> </html>
As shown in the above code, the form is submitted to file.php. The file.php code is as follows
<?php header("Content-type: text/html; charset=utf-8");//设置编码 //print_r($_FILES["upfile"]); if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ $upfile=$_FILES["upfile"]; //获取数组里面的值 $name=$upfile["name"];//上传文件的文件名 $type=$upfile["type"];//上传文件的类型 $size=$upfile["size"];//上传文件的大小 $tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径 //判断是否为图片 switch ($type){ case 'image/pjpeg':$okType=true; break; case 'image/jpeg':$okType=true; break; case 'image/gif':$okType=true; break; case 'image/png':$okType=true; break; } if($okType){ /** * 0:文件上传成功<br/> * 1:超过了文件大小,在php.ini文件中设置<br/> * 2:超过了文件的大小MAX_FILE_SIZE选项指定的值<br/> * 3:文件只有部分被上传<br/> * 4:没有文件被上传<br/> * 5:上传文件大小为0 */ $error=$upfile["error"];//上传后系统返回的值 echo "================<br/>"; echo "上传文件名称是:".$name."<br/>"; echo "上传文件类型是:".$type."<br/>"; echo "上传文件大小是:".$size."<br/>"; echo "上传后系统返回的值是:".$error."<br/>"; echo "上传文件的临时存放路径是:".$tmp_name."<br/>"; echo "开始移动上传文件<br/>"; //把上传的临时文件移动到up目录下面 //move_uploaded_file($tmp_name,'up/'.$name); $destination="up/".$name; echo "================<br/>"; echo "上传信息:<br/>"; if($error==0){ echo "文件上传成功啦!"; //echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; }elseif ($error==1){ echo "超过了文件大小,在php.ini文件中设置"; }elseif ($error==2){ echo "超过了文件的大小MAX_FILE_SIZE选项指定的值"; }elseif ($error==3){ echo "文件只有部分被上传"; }elseif ($error==4){ echo "没有文件被上传"; }else{ echo "上传文件大小为0"; } }else{ echo "请上传jpg,gif,png等格式的图片!"; } } ?>
Note: For the above two pieces of code, please create two files yourself, copy the code into it, and put it on the local server for debugging
When we select a picture, it will Output the information, such as path, suffix, etc.
Next Section