PHP ?? ???
??? ??? ????? ????? ?? php.ini?? ?? ???? ????? ?? ????? ?? ??? ??? ?? ???. ?? ??? ??? ??? ?? ? ?? ??? ??? ??? ? ????.
file_upload: ?? on?? ??? ?? ???? ????? ?????. ??? ?? ??
upload_tmp_dir: ?? ???? ?? ?? ???????. ??? ????? ????? ?? ??? ?? ??? ?? ????? ?????. ??? ?? ??? ??? ?? ??? ???? ???. ??? ??? ??? ?? ????
upload_max_filesize: ???? ???? ? ?? ?? ?? ??(MB)???. ??? ???? 2MB? ???? ?? ?? ??
★ ?? ?? ???? ???? PHP ?? ??? ???? ??? ??? ?? ??? ???? ???? ????.
?? ??? ??
PHP? ? ? ????, ??? PHP ?? ???? 6??? ??????.
?? ?? ? ?? 6??? ??? PHP ?? ???? ????? ??? ? ????.
1. ?? ??? ??? ??
????? ??? ?? ??? ?? ??? ??:
?? ?? | ?? |
0 | ????. ?? ?? ??? ??? ??? ? ????. |
1 | ??? ?? ??? ??????. PHP??? upload_max_filesize = 2M? ???? ????. ini, ????? ???? 2M???. ???? ?? ??? ?? ?? ?? |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ??? ?? ??? ??????. ????? ???? ??? ?? ????? ??? ?? ??? ?????. |
3 | ?? ??? ???????. ???? |
4 | 文件沒有被上傳 |
6 | 找不到臨時文件夾,可能目錄不存在或沒權(quán)限 |
7 | 文件寫入失敗,可能磁盤滿了或沒有權(quán)限 |
??★: ?? ??? 5? ????
2. ??? ?? ??? ??? ????? ?? ?? ???
? ????? ??? ?????. ????? ??? php.ini? ??? ?? ??? ? ???
??? ????? ???? ??? ??? ?? ??? ?? ?????.
?:
Sina Weibo ?? QQ Zone? ?? ??? ??? 2M? ?????. ?? ??? ? 2M ?? ??? ?????.
??? ?? ???? ? ? ?? ???? ?????.
??? ??? ?? ??? ?? ?????? ????? ??? ?? ??? ???? ? ?????.
3. ??? ??? MIME ??? ????? ?????.
MIME(Multi Purpose Internet Mail Extensions)? ??? ??? ?? ??? ?????. ?? ???? ?? ??? ???????? ??? ???? ?????. ???? ?? ??? ???? ????? ???? ??? ??????? ???? ?? ??? ???. ?? ?? ????? ?? ?? ??? ?? ??? ?? ?? ??? ???? ? ?????.
???? MIME ??? ??? ? ? ?? ????? ???? PHP ?? in_array()? ?????.
? ?? ????? ??? ???,
? ?? ????? ?? ?????.
? ??? ???? ?? ???? MIME ??? ?? ?? ?? ??? ?????.
4. ??? ??
??? ????? ??????? ?? ??? ???? ????.
?? ??? ??? ???? ???? ?? ???? ???? ?? ??? ???? ?? ?????.
date(), mt_rand() ?? Unique()? ???? ??? ?? ??? ??? ? ????.
5. ?? ??? ??? ?????.
??? ????? ??????? ?? ??? ???? ????.
date(), mt_rand() ?? Unique()? ???? ??? ?? ??? ??? ? ????. ?? ???? ???? ???? ???? ?? ??? ???? ?? ????? ??????. ?? ??? ????.
?? ???? ?????. ??? ?? ? ?? ?? ??? ???? ??? ????? ???? ????.
???? ?? ????? ???? ???, ?? ???? ?? ???????. ???? ?? ???? ??? ?? ???? ???? ?? ?? ??? ???? ???.
is_uploaded_file()? ??? ??? ???? ???? ???? ?? ????($_FILES? ??? ?? ??)? ?????.
6. ?? ??? ??? ????? ?????.
?? ??? ?? ?? ????? ?? ????? ???? ???. ???? ???? ??? ????.
?? ???? ????? ???? ?? ???? ??? ? ??? ????.
??? move_uploaded_file()? ?????.
???? ??? ??? ??? ???? ??? ???? ?????.
? ?? ????? ?????.
? ?? ????? ??? ???? ?????.
? ?? ????? ??? ??? ??? ??? ??????.
??? ?????? ?????? ??? ???? ???. ??? ????
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文網(wǎng)</title> </head> <body> <form action="file.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上傳"> </form> </body> </html>
??:
1. ?? ??? ???? ???? ??? post??? ???. get? ?? ?? ???? ??? ? ????
2. enctype? multipart/form-dat??? ???
3. type=file? ??? ?? ???? ?? ??? ????? ????. .
? ???? ??? ?? ??? file.php? ?????.
file.php? PHP ??? ?? ???? ??? ?????.
???? ?? ??? ?????. ?? ??? .jpg?? ???? ?????? ?????.
PHP? ?? ???? ?? ??? ??? ?? $_FILES? ??????. ???? ??? ?? ?? ???? ? ??? ??? ?????.
PHP ???? $_FILES? ???? ? ??? ??? ?????.
<?php //var_dump()或print_r() //打印變量的相關(guān)信息,將變量的信息詳細(xì)的展示出來 var_dump($_FILES); ?>
???? ?? ??:
array(1) {
["file"]=>
array(5) {
["name"]=>
string(7) "psu.jpg"
["type"]=> ;
???(10) "image/jpeg"
["tmp_name"]=>
???(22) "C:WindowsphpE2F1.tmp"
["error"]=>
int(0)
["size"]=>
int(488929)
}
}
??? ??? ?? ??? ??? ????.
?? ?? ????? ??? ? ??????(??=1)
'??' =>
??(??=5)
//?? ??
'??' => ??? 'psu .jpg' (??=7)
//??? MIME ??
'type' => string 'image/jpeg' (??=10)
// ?? ??, ???? ??? ??? ?????
'tmp_name' => string 'E:wamptmpphpC32A.tmp' (??=23)
// ?? ??, ??? ??? ?? ?? ?? ??? ?????
' error' => ?? 0?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??>
????? ?? ?? ?????.
.
? ?? ??? ?? ??? ???? ????
<?php
header("Content-type:text/html;charset=utf-8");
if($_FILES['file']['error'] > 0){
switch ($_FILES['file']['error']) { //錯誤碼不為0,即文件上傳過程中出現(xiàn)了錯誤
case '1':
echo '文件過大';
break;
case '2':
echo '文件超出指定大小';
break;
case '3':
echo '只有部分文件被上傳';
break;
case '4':
echo '文件沒有被上傳';
break;
case '6':
echo '找不到指定文件夾';
break;
case '7':
echo '文件寫入失敗';
break;
default:
echo "上傳出錯<br/>";
}
}else{
echo "上傳成功";//錯誤碼為0,即上傳成功,可以進(jìn)行后續(xù)處理,處理流程見下文
}
?>
? ??? ??? ?? ?? ??? ?? ??? ?? ?? ??? ???? ??? ?? ????? ??? ? ????.
?? ??????? ??? ???? ??, ?? ?? ???? ?? ???? ?? ?? ??? ????? ?? ?????? ??? ??? ???. ??? ???? ?? ??? ???? ????. ??? ?? ??? ????
??????? ?? ????? ??? ? ????.
<?php header("Content-type:text/html;charset=utf-8"); if($_FILES['file']['error'] > 0){ //有錯誤可停止執(zhí)行 }else { //當(dāng)前上傳文件無誤,運(yùn)行本段代碼 //判斷文件是否超出了指定的大小 //單位為byte $MAX_FILE_SIZE = 100000 if ($_FILES['file']['size'] > $MAX_FILE_SIZE) { //判斷,如果上傳的文件,大小超出了我們給的限制范圍,退上傳并產(chǎn)生錯誤提示 exit("文件超出指定大小"); } } ?>
??? ???? ?? ??? $MAX_FILE_SIZE? ?????. ? ??? ?? ??? ???? ??? $_FILES['file']['size'] ??? ???? ??????. . ?? ????? ? 100K ??? ?? ??? ?????.
? ?? ??? ??? MIME ??? ???? ???? ????.??? ?? ??? ??? ???? ???? ??? ?? ??? ????? ???? ?? ??? ???, ??? ? ?? ??? ???? ??? ??? ??????? ???? ?? ??? ??? ????. ???? ??? ???. ??? ???? ??? MIME ??? ??? ??? ???? ???? ???? ??? ?? ??? ????? ?????. ?? ?? ????? ?? ???? ?? ??? ???? ???? ???? ??? ???? ???? ?? ??? ???? ?? ??? ????? ?? ??? GIF ?? jpg? ??? ??? ????? ???. ?? ???? ?????. /* ??? ??? MIME ??? ??? ?? ??? ????? ?? ? ?? ??? ??? ??? ?? ??? ???? ????. ????? ?? ??? ?? ?? ?? ??? ?????. ?? ??? ???? ???? ??? ???? ?? ?? ?? ??? ?? ??? ?? ??? ?????. ??. ????? ?? ??? ?? ?? ?? ??? ?????. ?? ??? ???? ???? ??? ????? ?? ??? ?? ?? ??? ???? ?????. //??? ?? ?? ?? ?? ??? ??? ??????? ???? ????. is_uploaded_file() ??? ?? ??? ???? ???? ???? ?? ?????. <?php 6??, ??? ??? ??? ?????. move_uploaded_file() ??? ???? ??? ??? ??? ???? ??? ?????. Linux ????? ?? ????? ?? ??? ?? ??? ??? ???? ??? ???? ??? ??? ??? ?????. /*
?:
?? ??????? ???? .jpg ?? .gif? ???? ?????? ?????. , $allowSuffix = array ('jpg','gif');
*/
//???? ??? ?? ?? ??
$myImg =explore('.', $_FILES['file' ; $myImg? ????, ?? ???? ??? ??? ????.
*/
$myImgSuffix = array_pop($myImg);
/*
???? ?? ??? ???? ??? ??? ??? ?????
in_array() ??? ???? ???? ??? ?? ??? ????? ??
?? ??? ??? ?? ?? ?? ?? ?? ?? ??? ?? ???? ???? ?? ???? ?????
*/
if(!in_array($myImgSuffix, $allowSuffix)){???????????????????????????????????????????????????????????? ?? ??? ??? ??? ?? ??? ??? ?? ??? ??? ? ????. ???? ?? ??? ??? ???? ??? ??? ? ?? ?? ?? ???? ?? ????.
MIME ??? ???? ???. ?? ???? ?? ???
? ?? ???? ??? ??? ? ??? ???? ??? ?? ??? ???? ??? ?? ???? ?? ????.
*/
//??? ??? ???? ??? MIME ?????.
$allowMime = array(
"image/jpg",
"image /jpeg",
"???/pjpeg",
"???/gif"
);
if(!in_array($_FILES['file']['type '], $allowMime)) {?????????????????????????????? // ???? ??? MIME ??? ?? ?? ?? ??? ??
????????????????????????????????????????>
$path = "upload/images/";
/*
?? ??? ???? ??? ?? ??? ????, ? ?? ?? ?? ?? + 0~9 ??? ??? ??? ???? ?? ??? ???? ???? ??? ?? ?? ??? ?????.
*/
$name = ?? ('Y').date('m').date("d").date('H').date('i').date('s').rand(0,9).'. '.$myImgSuffix;
//is_uploaded_file()? ???? ???? ???? ?????. ?? ??? ?
? ?????. (is_uploaded_file($ _FILEs['file']['tmp_name'])){
}
?>
???? ??? ??? ??? ????? move_uploaded_file()? ?????. ? ?? ????? ???? ????, ? ?? ????? ?? ??? ??? ??? ?????. " ??? ??";?????????????????????????????????????????????????????????????????????????????????????????????????????~ >? ? ? ? ? ? ? ? ? ? ????????? echo '???? ??? ??' ??
??? ??? ?? 1 ??
?? ?? ???
PHP?? ?? ??? ????? ??? ?????. ??? ??? ?? ???? ?? ?? ??? ??? ????? ?? ??? ????. ?? ?? ???? ??? ?????, ??? ?? ? ???? ???? ???? ???? ???. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP中文網(wǎng)</title>
</head>
<body>
<html>
<body>
<form action="morefile.php" method="post" enctype="multipart/form-data">
<input type="file" name="file[]">
<input type="file" name="file[]">
<input type="submit" value="上傳">
</form>
</body>
</html>
</body>
</html>
??? ??? ??? ???? ???, ????? ? ?? ??? ??? ?????. ? ???? ?? ???? ??? ? ????.
2. input type="file" name="file[]" 2? ?? ??
?? ??? ??? ??????(??=1)
'??' =>
??(??=5)
'??' => >???????????????????????????????????????????> ??(?? =2)
0 => string 'image/jpeg' (??=10)
1 => string 'image/jpeg' (??=10)
//?? ??
'tmp_name ' => = > ??? 'E:wamptmpphpF6F5.tmp' (??=23)
//?? ?? ???
'error' =>
??(??=2)
0 = > int 0
1 => int 0
//?? ??
'size' => ??(??=2)
0 => 1 => int 151651
? ??? ??? ???? ?? ?? ??? ? ???, ? ??? ???? ??? ?????. ??? ? ???? ?? ??? ???? ????? for() ??? ???? ???.
? ??? ???? ??? $_FILES? ?????. ?? ??? ??? ??? ??? ??? ??? ????? ??? ??? ???? ???.for ($i=0; $i < count($_FILE['file']['name']); $i++) {
/*
is_uploaded_file ?? () ??? ??? ??????
??? ??? ?????
*/
if(is_uploaded_file($_FILEs['file']['tmp_name'][$i]) && $_FILEs[ 'file']['error'][$i] == 0){??
??if(move_uploaded_file($_FILEs['file']['tmp_name'][$i],'upload/' .$_FILE[ 'file']['name'][$i])){
//move_uploaded_file() ??? ???? ??? ??? ??? ???? ??? ?? ??? ?????
echo "??? ??";
??????????????????????????????????????????????????????????????????????????????> }
}
??? ?? ??? ???? ???? ??????. ???? ????? ?? ??? ??? ?? ??? ????.
?? 1??? ???? ???
???? 1 html ???
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文網(wǎng)</title> </head> <body> <form action="file-upload.php" enctype="multipart/form-data" method="post" name="uploadfile"> 上傳文件:<input type="file" name="upfile" /><br> <input type="submit" value="上傳" /></form> </form> </body> </html>???? 2
php ???? ??
<?php header("Content-type:text/html;charset=utf-8"); //print_r($_FILES["upfile"]); if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ $upfile=$_FILES["upfile"]; //獲取數(shù)組里面的值 $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文件中設(shè)置<br/> * 2:超過了文件的大小MAX_FILE_SIZE選項指定的值<br/> * 3:文件只有部分被上傳<br/> * 4:沒有文件被上傳<br/> * 5:上傳文件大小為0 */ $error=$upfile["error"];//上傳后系統(tǒng)返回的值 echo "上傳文件名稱是:".$name."<br/>"; echo "上傳文件類型是:".$type."<br/>"; echo "上傳文件大小是:".$size."<br/>"; echo "上傳后系統(tǒng)返回的值是:".$error."<br/>"; echo "上傳文件的臨時存放路徑是:".$tmp_name."<br/>"; echo "開始移動上傳文件<br/>"; //把上傳的臨時文件移動到指定目錄下面 move_uploaded_file($tmp_name,'D:\upload/images/'.$name); $destination="D:\upload/images/".$name; echo "上傳信息:<br/>"; if($error==0){ echo "文件上傳成功啦!"; }elseif ($error==1){ echo "超過了文件大小,在php.ini文件中設(shè)置"; }elseif ($error==2){ echo "超過了文件的大小MAX_FILE_SIZE選項指定的值"; }elseif ($error==3){ echo "文件只有部分被上傳"; }elseif ($error==4){ echo "沒有文件被上傳"; }else{ echo "上傳文件大小為0"; } }else{ echo "請上傳jpg,gif,png等格式的圖片!"; } } ?>?? ??? ????? ???? ?? ??? ??????? 2
? ??? 4?? ?? ??? ???? ????. ??? ??? u_file[]??, ?? ? ???? ?? ??? $_FILES[u_file]? ???? ??? ??? ?????. ??? ??? ?? ??? ??????.
???? 1 html ???
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文網(wǎng)</title> </head> <body> <html> <body> <p>請選擇要上傳的文件</p> <form action="morefile.php" method="post" enctype="multipart/form-data"> <table border="1" bgcolor="f0f0f0"> <tr> <td>上傳文件</td> <td><input type="file" name="u_file[]"></td> </tr> <tr> <td>上傳文件</td> <td><input type="file" name="u_file[]"></td> </tr> <tr> <td>上傳文件</td> <td><input type="file" name="u_file[]"></td> </tr> <tr> <td>上傳文件</td> <td><input type="file" name="u_file[]"></td> </tr> <tr> <td colspan="2"><input type="submit" value="上傳"></td> </tr> </table> </form> </body> </html> </body> </html>???? 2 php ???? ??<?php header("Content-type:text/html;charset=utf-8"); if(!empty($_FILES[u_file][name])){ //判斷遍歷$_FILES是否為空 $file_name=$_FILES[u_file][name]; //將上傳文件名另存為數(shù)組 $file_tmp_name=$_FILES[u_file][tmp_name]; //將上傳的臨時文件名另存為數(shù)組 for($i=0;$i<count($file_name);$i++){ //循環(huán)上傳文件 if($file_name[$i]!=""){ //判斷上傳文件名是否為空 move_uploaded_file($file_tmp_name[$i],$i.$file_name[$i]); echo "文件" .$file_name[$i] ."上傳成功。更名為"."$file_name[$i]"."<br>"; } } } ?>????? ???? ??????? 3
? ?? 1MB ??? ?? ???? ???? ??? ??????.
<from action="" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上傳"> </from> <?php if(!empty($_FILES[file][name])){ //判斷是否有文件上傳 $fileinfo=$_FILES[file]; //將文件信息賦給變量$fileinfo if($fileinfo['size']<1000000 && $fileinfo['size']>0){ //判斷文件大小 echo "上傳成功"; }else{ echo "上傳文件太大或未知"; } } ?>????? ?????.||<from action="" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上傳"> </from> <?php if(!empty($_FILES[file][name])){ //判斷是否有文件上傳 $fileinfo=$_FILES[file]; //將文件信息賦給變量$fileinfo if($fileinfo['size']<1000000 && $fileinfo['size']>0){ //判斷文件大小 echo "上傳成功"; }else{ echo "上傳文件太大或未知"; } } ?>
- ?? ??
- ???? ????
![]()
????PHP ?? ?? ?? ????
176404?? ???? ????.![]()
????PHP ?? ?? ??? ????
42722?? ???? ????.![]()
???? ?? ????? ?? ?? ????
33708?? ???? ????.![]()
???? ?? PHP ?? ??? ???? VIP ??? ???
34465?? ???? ????.![]()
??Geek Academy jquery ?? ?? ?? ??? ????
9531?? ???? ????.![]()
??Shangxuetang ????? ?? ?? ?? ?? ??? ????
34431?? ???? ????.![]()
????Little Turtle? Python ??? ?? ?? ?? ?? ??? ????
179372?? ???? ????.![]()
????PHP ?? ??? ?? ????
19189?? ???? ????.![]()
????PHP ?? ???? 1: ??? ?? PHP ???
462496?? ???? ????.![]()
?????? ??? ?? ?? ???? ?? ?? PHP ????
27841?? ???? ????.![]()
????jQuery/Ajax/PHP ?? ??/H5 ??? ??? ?? ?? ?? ???? ??
4904?? ???? ????.![]()
???????? JavaScript? ?? ? ?? ????
34412?? ???? ????.?? ????? ????? ? ????. ?? ???? ???? ????. ???? ? ??? ?? ?? ??????~? ??? ??? ???? ???? ????.
- PHP? ??? ???? ??? ?? ??? ???? ?????.
- ? ??? ?? ??? ?? ?? ??
- ??? ?? ???? ???? ??? Mini ?? MVC ?????? ??? ?? Tianlongbabu ??
- PHP ?? ?? ????: ?? PHP ?? [???? ??]
- ??? ?? ? ??? ???
- ??? ???? ?? ??
- ?? ?? Node.JS ?? ??
- ??? ?? ? ???? ????? ??: HTML5/CSS3/ES6/NPM/Vue/...[??]
- ???? PHP MVC ????? ??(?? ?? 40? ?/??? ??/???? ????? ??? ?)