在台大 ptt BBS 上面看到有人推文上傳功能可以交給 pear HTTP_Upload 來跟 PHP 搭配使用,自己去看了一下官方線上文件,教學還蠻容易的,大概看過範例就可以瞭解了,上傳之後的檔案,在搭配之前我寫的 [PHP] header下載檔案 搭配資料庫
* Can handle from one file to multiple files. * Safe file copying from tmp dir. * Easy detecting mechanism of valid upload, missing upload or error. * Gives extensive information about the uploaded file. * Rename uploaded files in different ways: as it is, safe or unique * Validate allowed file extensions * Multiple languages error messages support (es, en, de, fr, it, nl, pt_BR) 1. 首先下載官方檔案:點我 2. 撰寫 PHP 程式:
$root_path = './'; include($root_path . 'config.php'); /* 把 pear HTTP_Upload 檔案 include 進來 */ require_once($root_path . 'includes/pear/Upload.php'); $upload = new HTTP_Upload("en"); /* form 的 file 欄位名稱 */ $file = $upload->getFiles("upload_file_01"); if ($file->isValid()) { /* 設定上傳檔案之後,以亂數取檔名 */ $file->setName("uniq"); /* 檔案資訊 */ echo ""; print_r($file->getProp()); echo "";/* 印出所有檔案資訊 */
$file->getProp(“size”) 檔案大小
$file->getProp(“name”) 亂數取檔名
$file->getProp(“real”) 上傳的真正檔名
$file->getProp(“ext”) 上傳的副檔名
$moved = $file->moveTo(“upload_data/1/”); if (!PEAR::isError($moved)) { $sql = “INSERT INTO " . FILES_TABLE . " (
userid
,file_type
,dateline
,filesize
,filename
,file_real_name
,extention
) VALUES ( ‘{$userdata[user_id]}’, ‘1’, ‘{$time}’, ‘{$file->getProp(“size”)}’, ‘{$file->getProp(“name”)}’, ‘{$file->getProp(“real”)}’, ‘{$file->getProp(“ext”)}’)”; if( !($result = $db->sql_query($sql)) ) { die(“Could not query config information " . $sql); }
DisplayErrMsg(“您已經完成上傳資料,在結束上傳日期前,都可以修改
”. sprintf($lang[‘Click_return_forum’], “”, “”)); } else { echo $moved->getMessage(); } } elseif ($file->isMissing()) { echo “No file was provided.”; } elseif ($file->isError()) { echo $file->errorMsg(); }
See also
- 來聊聊 PHP & JavaScript & CSS 的 Coding Style
- 為什麼我選擇使用 Laravel Framework?
- Laravel 搭配 Google, Facebook, Twitter 第三方 OAuth 認證
- 將 wordpress 強制使用 SSL 連線
- PHP 7 vs HHVM Benchmark 比較
- Laravel 50 個小技巧 + Laravel 5.2 新功能
- Laravel Homestead 支援 MySQL 5.7 和 Node 5.0
- PHP-FIG 新網站
- Laravel Homestead 支援 PHP 7
- Laravel 5.1 is released