在php編程中,檔案上傳可以說是太常見的了,分享一個php 上傳檔案的簡單代碼,適合初學的朋友。
1、檔案上傳介面 up_file.html
<HTML> <HEAD> <TITLE>檔案上傳介面</TITLE> </HEAD> <BODY> <table> <tr align="center"> <td><form ENCTYPE="multipart/form-data" NAME="SubmitForm" ACTION="upload.php" METHOD="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"><input type="hidden" name="UploadAction" value="1"> </td></tr> <tr align="center"> <td><input NAME="UploadFile" TYPE="file" SIZE="30"></td> </tr> <tr align="center"> <td><input NAME="submit" VALUE="提交" TYPE="submit"></td> <td><input NAME="reset" VALUE="重設" TYPE="reset"></td> </tr> </form> </table> </center> </BODY> </HTML>
2、檔案上傳代碼 upload.php
<HTML> <HEAD> <TITLE>檔案上傳代碼</TITLE> </HEAD> <BODY> <? /** @檔案上傳代碼 儲存上傳的檔案 @site http://www.jbxue.com */ $TimeLimit=60; /*設定逾時限制時間 預設時間為30秒 設定為0時為不限時 */ set_time_limit($TimeLimit); If(($UploadFile !="none" )) { $UploadPath=AddSlashes(dirname($PATH_TRANSLATED))."\my_files\"; //上傳檔案存放路徑 $FileName=$UploadPath.$UploadFile_name; //上傳檔案名稱 if($UploadFile_size < 1024) //上傳檔案大小 { $FileSize=(string)$UploadFile_size." 位元組"; } elseif ($UploadFile_size <(1024 * 1024)) { $FileSize=number_format((double)($UploadFile_size/1024), 1) . " KB"; } else{ $FileSize=number_format((double)($UploadFile_size / (1024 * 1024)), 1) . " MB"; } if(!file_exists($FileName)) { if(copy($UploadFile,$FileName)) { echo "檔案 $UploadFile_name ($FileSize)上傳成功!"; } else { echo "檔案 $UploadFile_name上傳失敗!"; } unlink($UploadFile); } else { echo "檔案 $UploadFile_name已經存在!"; } } else { echo "你沒有選擇任何檔案上傳!"; } set_time_limit(30); //恢複預設逾時設定 ?> <BR><A HREF = "upload.php">返回</A> </BODY> </HTML>