PHP实现简单文件上传系统
生活随笔
收集整理的這篇文章主要介紹了
PHP实现简单文件上传系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄結構如下,其中包含兩個代碼文件和一個uploads文件夾(用于存放上傳的文件)
index.php
該代碼實現html頁面,包括需要填寫學號和姓名,上傳文件大小不得超過20M
<form action="fileSystem.php" method="post" enctype="multipart/form-data"><h3 style="color: red">文件大小不要超過20M</h3><hr>請輸入學號:<input type="text" name="num"><br>請輸入姓名:<input type="text" name="username"><br><input type="hidden" name="MAX_FILE_SIZE" value="25000000" /><input type="file" name="myPicture[]" size= "25" maxlength="100"><br><br><input type="submit" value="提交"> </form>fileSystem.php
該代碼處理文件上傳邏輯
<?phpheader("Content-type=text/html;charset=utf-8");if (empty($_POST)) {exit("提交的表單數據超過post_max_size的配置");}// 轉存post提交的各個變量$num = $_POST['num'];$username = $_POST['username'];$arr = $_FILES['myPicture'];$file =array();for ($i=0; $i < count($arr['name']); $i++) { $file[$i]['name'] = $arr['name'][$i];$file[$i]['type'] = $arr['type'][$i];$file[$i]['tmp_name'] = $arr['tmp_name'][$i];$file[$i]['error'] = $arr['error'][$i];$file[$i]['size'] = $arr['size'][$i];}for ($i=0; $i < count($file); $i++) { switch ($file[$i]['error']) {default:echo "Failed upload";case 0: $fileName = $file[$i]['name'];$fileTemp = $file[$i]['tmp_name'];// 文件名稱合成:uploads目錄下,學號+姓名+文件后綴// 其中文件后綴使用了php字符串處理的幾個方法,主要是通過判斷"."的位置獲取后綴名$destination = "uploads/" . $num . $username . substr($file[$i]['name'], strpos($file[$i]['name'], ".")) ;move_uploaded_file($fileTemp, $destination);echo "Successful upload";break;case 1:echo "php.ini upload_max_filesize is to small";break;case 2:echo "upload file is to large";break;case 3:echo "only part is ok";break;case 4:echo "no charge file";break;} } ?>需要注意的是,php文件上傳有諸多控制,需要修改相關參數
源代碼
<input type="hidden" name="MAX_FILE_SIZE" value="25000000" />php相關配置文件 php.ini文件
max_execution_time = 30,每個腳本運行的最長時間max_input_time = 60,每個腳本可以消耗的時間
memory_limit = 128M,腳本運行最大消耗的內存,-1為無限制
post_max_size = 8M,表單提交最大數據
Apache或者Nginx相關配置文件
Apahce目錄下的httpd.conf文件添加LimitRequestBody 31457280 即30M=30*1024*1024
Nginx目錄下的nginx.conf文件添加client_max_body_size 30M;
總結
以上是生活随笔為你收集整理的PHP实现简单文件上传系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 现在股市里人人都在赚钱!!到底谁在亏钱?
- 下一篇: DataGridComboBoxColu