標籤:
1.目錄結構
2.index.php
<html><head> <meta charset="utf-8"> <title>上傳檔案</title></head><body><form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">檔案名稱</label> <input type="file" name="file" id="file"><br> <input type="submit" value="提交"></form></body></html>
3.upload.php
<?php//上傳檔案代碼//核心代碼:/*if($_FILES[‘file‘][‘error‘]>0){ echo ‘錯誤:‘.$_FILES[‘file‘][‘error‘].‘<br>‘;}else{ echo ‘上傳檔案名稱:‘.$_FILES[‘file‘][‘name‘].‘<br>‘; echo ‘檔案名稱類型:‘.$_FILES[‘file‘][‘type‘].‘<br>‘; echo ‘檔案大小:‘.($_FILES[‘file‘][‘size‘]/1024).‘kb<br>‘; echo ‘檔案臨時存放地址:‘.$_FILES[‘file‘][‘tem_name‘];}*///上傳檔案所需要的代碼,但是在上傳之前需要對檔案進行判斷限制,大小,類型等//允許上海竄的圖片尾碼$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]); //截取檔案尾碼$extension = end($temp); //檔案尾碼$type = $_FILES["file"]["type"];$size = $_FILES["file"]["size"];$error = $_FILES["file"]["error"];$name = $_FILES["file"]["name"];$tmp_name = $_FILES["file"]["tmp_name"];if (($type == "image/gif") || ($type == "image/jpeg") || ($type == "image/jpg") || ($type == "image/pjpeg") || ($type == "image/x-png") || ($type == "image/png") && $size < 2048 && in_array($extension, $allowedExts)) { if ($error > 0) { echo "錯誤:" . $error . ‘<br>‘; } else { echo "檔案名稱:" . $name . "<br>"; echo "檔案類型:" . $type . "<br>"; echo "檔案大小:" . ($size / 1024) . "kb<br>"; echo "臨時存放位置:" . $tmp_name; //判斷存放檔案的目錄中是否存在該檔案,如果沒有,需要建立它,file的目錄許可權為777 if (file_exists("file/" . $name)) { echo $name . "檔案已經存在。"; } else { move_uploaded_file($tmp_name, ‘file/‘. $name); echo "檔案儲存體在:" . "file/". $name; } }} else { echo "非法檔案";}
[moka同學代碼]PHP初級知識:上傳檔案源碼