php 圖片上傳php
基礎原理: html
經過使用php的全局數組 $_FILES,你能夠從客戶計算機向遠程服務器上傳文件。數組
第一個參數是表單的 input name,第二個下標能夠是 "name"、"type"、"size"、 "tmp_name" 或 "error"。 以下所示:服務器
$_FILES["file"]["name"] - 上傳文件的名稱post
$_FILES["file"]["type"] - 上傳文件的類型spa
$_FILES["file"]["size"] - 上傳文件的大小,以字節計code
$_FILES["file"]["tmp_name"] - 存儲在服務器的文件的臨時副本的名稱orm
$_FILES["file"]["error"] - 由文件上傳致使的錯誤代碼htm
功能代碼以下:blog
input_file.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>input_file</title> </head> <body> <form action="./upload.php" method='post' enctype="multipart/form-data"> <input type="file" name="file"> <p><input type="submit"></p> </form> </body> </html>
upload.php
<?php header("Content-type: text/html; charset=utf-8"); // 接收到的參數 $file = $_FILES['file']; //傳輸的數據 $name = $file['name']; //文件名 $type = strtolower(substr($name, strrpos($name, '.')+1)); //獲得文件類型,而且都轉成小寫 $allow_type = array('jpg', 'jpeg', 'gif', 'png'); //容許上傳的文件類型 if( !in_array($type, $allow_type) ) { // 不是容許的文件類型 return; } //判斷是不是經過HTTP POST上傳的 if(!is_uploaded_file($file['tmp_name'])){ //若是不是經過HTTP POST上傳的 return ; } $upload_path = "./img/"; //上傳文件的存放路徑 //開始移動文件到相應的文件夾 if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){ echo "Successfully!"; }else{ echo "Failed!"; } ?>