ããä¸ä¼ 表å
ããå¨ThinkPHPä¸ä½¿ç¨ä¸ä¼ åè½æ éè¿è¡ç¹å«å¤çãä¾å¦ï¼ä¸é¢æ¯ä¸ä¸ªå¸¦æé件ä¸ä¼ ç表åæ交ï¼
ãã<form action="__URL__/upload" enctype="multipart/form-data" method="post" >
ãã<input type="text" name="name" />
ãã<input type="file" name="photo" />
ãã<input type="submit" value="æ交" >
ãã</form>
ãã注æï¼è¦ä½¿ç¨ä¸ä¼ åè½ ä½ ç表åéè¦è®¾ç½® enctype="multipart/form-data"
ããå¤æ件ä¸ä¼ æ¯æ
ããå¦æéè¦ä½¿ç¨å¤ä¸ªæ件ä¸ä¼ ï¼åªéè¦ä¿®æ¹è¡¨åï¼æ
ãã<input type='file' name='photo'>
ããæ¹ä¸º
ãã<input type='file' name='photo1'>
ãã<input type='file' name='photo2'>
ãã<input type='file' name='photo3'>
ããæè
ãã<input type='file' name='photo[]'>
ãã<input type='file' name='photo[]'>
ãã<input type='file' name='photo[]'>
ãã两ç§æ¹å¼çå¤é件ä¸ä¼ ç³»ç»çæ件ä¸ä¼ ç±»é½å¯ä»¥èªå¨è¯å«ã
ããä¸ä¼ æä½
ããThinkPHPæ件ä¸ä¼ æä½ä½¿ç¨Think\Uploadç±»ï¼å设åé¢ç表åæ交å°å½åæ§å¶å¨çuploadæ¹æ³ï¼æ们æ¥çä¸uploadæ¹æ³çå®ç°ä»£ç ï¼
ããpublic function upload(){
ãã$upload = new \Think\Upload();// å®ä¾åä¸ä¼ ç±»
ãã$upload->maxSize = 3145728 ;// 设置é件ä¸ä¼ 大å°
ãã$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置é件ä¸ä¼ ç±»å
ãã$upload->rootPath = './Uploads/'; // 设置é件ä¸ä¼ æ ¹ç®å½
ãã$upload->savePath = ''; // 设置é件ä¸ä¼ ï¼åï¼ç®å½
ãã// ä¸ä¼ æ件
ãã$info = $upload->upload();
ããif(!$info) {// ä¸ä¼ é误æ示é误信æ¯
ãã$this->error($upload->getError());
ãã}else{// ä¸ä¼ æå
ãã$this->success('ä¸ä¼ æåï¼');
ãã}
ãã}
ããä¸ä¼ 类对å¾çæ件çä¸ä¼ å®å
¨åäºæ¯æï¼å¦æä¼å¾ä¸ä¼ éæ³çå¾åæ件ï¼ç³»ç»ä¼æ示 éæ³å¾åæ件ã 为äºæ´å¥½ç使ç¨ä¸ä¼ åè½ï¼å»ºè®®ä½ çæå¡å¨å¼å¯finfo模åæ¯æ
ããä¸ä¼ åæ°
ããå¨ä¸ä¼ æä½ä¹åï¼æ们å¯ä»¥å¯¹ä¸ä¼ çå±æ§è¿è¡ä¸äºè®¾ç½®ï¼Uploadç±»æ¯æçå±æ§è®¾ç½®å
æ¬ï¼
ããå±æ§æè¿°
ããmaxSizeæ件ä¸ä¼ çæ大æ件大å°ï¼ä»¥åè为åä½ï¼ï¼0为ä¸é大å°
ããrootPathæ件ä¸ä¼ ä¿åçæ ¹è·¯å¾
ããsavePathæ件ä¸ä¼ çä¿åè·¯å¾ï¼ç¸å¯¹äºæ ¹è·¯å¾ï¼
ããsaveNameä¸ä¼ æ件çä¿åè§åï¼æ¯ææ°ç»åå符串æ¹å¼å®ä¹
ããsaveExtä¸ä¼ æ件çä¿ååç¼ï¼ä¸è®¾ç½®çè¯ä½¿ç¨åæ件åç¼
ããreplaceåå¨ååæ件æ¯å¦æ¯è¦çï¼é»è®¤ä¸ºfalse
ããextså
许ä¸ä¼ çæ件åç¼ï¼ç空为ä¸éå¶ï¼ï¼ä½¿ç¨æ°ç»æè
éå·åéçå符串设置ï¼é»è®¤ä¸ºç©º
ããmimeså
许ä¸ä¼ çæ件类åï¼ç空为ä¸éå¶ï¼ï¼ä½¿ç¨æ°ç»æè
éå·åéçå符串设置ï¼é»è®¤ä¸ºç©º
ããautoSubèªå¨ä½¿ç¨åç®å½ä¿åä¸ä¼ æ件 é»è®¤ä¸ºtrue
ããsubNameåç®å½å建æ¹å¼ï¼éç¨æ°ç»æè
å符串æ¹å¼å®ä¹
ããhashæ¯å¦çææ件çhashç¼ç é»è®¤ä¸ºtrue
ããcallbackæ£æµæ件æ¯å¦åå¨åè°ï¼å¦æåå¨è¿åæ件信æ¯æ°ç»
ããä¸é¢çå±æ§å¯ä»¥éè¿ä¸¤ç§æ¹å¼ä¼ å
¥ï¼
ããå®ä¾åä¼ å
¥
ããæ们å¯ä»¥å¨å®ä¾åçæ¶åç´æ¥ä¼ å
¥åæ°æ°ç»ï¼ä¾å¦ï¼
ãã$config = array(
ãã'maxSize' => 3145728,
ãã'rootPath' => './Uploads/',
ãã'savePath' => '',
ãã'saveName' => array('uniqid',''),
ãã'exts' => array('jpg', 'gif', 'png', 'jpeg'),
ãã'autoSub' => true,
ãã'subName' => array('date','Ymd'),
ãã);
ãã$upload = new \Think\Upload($config);// å®ä¾åä¸ä¼ ç±»
ããå
³äºsaveNameåsubNameç使ç¨åé¢æ们ä¼æ详ç»çæè¿°ã
ããå¨æèµå¼
ããæ¯æå¨å®ä¾ååå¨æèµå¼ä¸ä¼ åæ°ï¼ä¾å¦ï¼
ãã$upload = new \Think\Upload();// å®ä¾åä¸ä¼ ç±»
ãã$upload->maxSize = 3145728;
ãã$upload->rootPath = './Uploads/';
ãã$upload->savePath = '';
ãã$upload->saveName = array('uniqid','');
ãã$upload->exts = array('jpg', 'gif', 'png', 'jpeg');
ãã$upload->autoSub = true;
ãã$upload->subName = array('date','Ymd');
ããä¸é¢ç设置åå®ä¾åä¼ å
¥çæææ¯ä¸è´çã
ããä¸ä¼ æ件信æ¯
ãã设置好ä¸ä¼ çåæ°åï¼å°±å¯ä»¥è°ç¨Think\Uploadç±»çuploadæ¹æ³è¿è¡é件ä¸ä¼ ï¼å¦æ失败ï¼è¿åfalseï¼å¹¶ä¸ç¨getErroræ¹æ³è·åé误æ示信æ¯ï¼å¦æä¸ä¼ æåï¼å°±è¿åæåä¸ä¼ çæ件信æ¯æ°ç»ã
ãã$upload = new \Think\Upload();// å®ä¾åä¸ä¼ ç±»
ãã$upload->maxSize = 3145728 ;// 设置é件ä¸ä¼ 大å°
ãã$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置é件ä¸ä¼ ç±»å
ãã$upload->rootPath = './Uploads/'; // 设置é件ä¸ä¼ æ ¹ç®å½
ãã$upload->savePath = ''; // 设置é件ä¸ä¼ ï¼åï¼ç®å½
ãã// ä¸ä¼ æ件
ãã$info = $upload->upload();
ããif(!$info) {// ä¸ä¼ é误æ示é误信æ¯
ãã$this->error($upload->getError());
ãã}else{// ä¸ä¼ æå è·åä¸ä¼ æ件信æ¯
ããforeach($info as $file){
ããecho $file['savepath'].$file['savename'];
ãã}
ãã}
ããæ¯ä¸ªæ件信æ¯åæ¯ä¸ä¸ªè®°å½äºä¸é¢ä¿¡æ¯çæ°ç»ï¼å
æ¬ï¼
ããå±æ§æè¿°
ããkeyé件ä¸ä¼ ç表åå称
ããsavepathä¸ä¼ æ件çä¿åè·¯å¾
ããnameä¸ä¼ æ件çåå§å称
ããsavenameä¸ä¼ æ件çä¿åå称
ããsizeä¸ä¼ æ件ç大å°
ããtypeä¸ä¼ æ件çMIMEç±»å
ããextä¸ä¼ æ件çåç¼ç±»å
ããmd5ä¸ä¼ æ件çmd5åå¸éªè¯å符串 ä»
å½hash设置å¼å¯åææ
ããsha1ä¸ä¼ æ件çsha1åå¸éªè¯å符串 ä»
å½hash设置å¼å¯åææ
ããæ件ä¸ä¼ æååï¼å°±å¯ä»¥ä½¿ç¨è¿äºæ件信æ¯æ¥è¿è¡å
¶ä»çæ°æ®æä½ï¼ä¾å¦ä¿åå°å½åæ°æ®è¡¨æè
åç¬çé件æ°æ®è¡¨ã
ããä¾å¦ï¼ä¸é¢è¡¨ç¤ºæä¸ä¼ ä¿¡æ¯ä¿åå°æ°æ®è¡¨çå段ï¼
ãã$model = M('Photo');
ãã// åå¾æåä¸ä¼ çæ件信æ¯
ãã$info = $upload->upload();
ãã// ä¿åå½åæ°æ®å¯¹è±¡
ãã$data['photo'] = $info[0]['savename'];
ãã$data['create_time'] = NOW_TIME;
ãã$model->add($data);
ããåæ件ä¸ä¼
ããuploadæ¹æ³æ¯æå¤æ件ä¸ä¼ ï¼ææ¶åï¼æ们åªéè¦ä¸ä¼ ä¸ä¸ªæ件ï¼å°±å¯ä»¥ä½¿ç¨Uploadç±»æä¾çuploadOneæ¹æ³ä¸ä¼ å个æ件ï¼ä¾å¦ï¼
ããpublic function upload(){
ãã$upload = new \Think\Upload();// å®ä¾åä¸ä¼ ç±»
ãã$upload->maxSize = 3145728 ;// 设置é件ä¸ä¼ 大å°
ãã$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置é件ä¸ä¼ ç±»å
ãã$upload->rootPath = './Uploads/'; // 设置é件ä¸ä¼ æ ¹ç®å½
ãã// ä¸ä¼ å个æ件
ãã$info = $upload->uploadOne($_FILES['photo1']);
ããif(!$info) {// ä¸ä¼ é误æ示é误信æ¯
ãã$this->error($upload->getError());
ãã}else{// ä¸ä¼ æå è·åä¸ä¼ æ件信æ¯
ããecho $info['savepath'].$info['savename'];
ãã}
ãã}
ããuploadOneæ¹æ³ä¸ä¼ æååè¿åçæ件信æ¯åuploadæ¹æ³çåºå«æ¯åªæå个æ件信æ¯çä¸ç»´æ°ç»ã
ããä¸ä¼ æ件çå½åè§å
ããä¸ä¼ æ件çå½åè§åï¼saveNameï¼ç¨äºç¡®ä¿æ件ä¸ä¼äº§çå²çªæè
è¦ççæ
åµãå½åè§åçå®ä¹å¯ä»¥æ ¹æ®ä½ çä¸å¡é»è¾æ¥è°æ´ï¼ä¸æ¯åºå®çãä¾å¦ï¼å¦æä½ éç¨æ¶é´æ³çæ¹å¼æ¥å®ä¹å½åè§èï¼é£ä¹å¨åæ¶ä¸ä¼ å¤ä¸ªæ件çæ¶åå¯è½äº§çå²çªï¼å 为åä¸ç§å
å¯ä»¥ä¸ä¼ å¤ä¸ªæ件ï¼ï¼å æ¤ä½ éè¦æ ¹æ®ä½ çä¸å¡éæ±æ¥è®¾ç½®åéçä¸ä¼ å½åè§åãè¿é顺便æ¥è¯´ä¸saveNameåæ°çå
·ä½ç¨æ³ã
ããä¸ãéç¨å½æ°æ¹å¼
ããå¦æä¼ å
¥çå符串æ¯ä¸ä¸ªå½æ°åï¼é£ä¹è¡¨ç¤ºéç¨å½æ°å¨æçæä¸ä¼ æ件åï¼ä¸å
æ¬æ件åç¼ï¼ï¼ä¾å¦ï¼
ãã// éç¨æ¶é´æ³å½å
ãã$upload->saveName = 'time';
ãã// éç¨GUIDåºåå½å
ãã$upload->saveName = 'com_create_guid';
ããä¹å¯ä»¥éç¨ç¨æ·èªå®ä¹å½æ°
ãã// éç¨èªå®ä¹å½æ°å½å
ãã$upload->saveName = 'myfun';
ããé»è®¤çå½åè§å设置æ¯éç¨uniqidå½æ°çæä¸ä¸ªå¯ä¸çå符串åºåã
ããsaveNameçå¼æ¯ææ°ç»åå符串两ç§æ¹å¼ï¼å¦ææ¯åªæä¸ä¸ªåæ°æè
没æåæ°çå½æ°ï¼ç´æ¥ä½¿ç¨å符串设置å³å¯ï¼å¦æéè¦ä¼ å
¥é¢å¤çåæ°ï¼å¯ä»¥ä½¿ç¨æ°ç»æ¹å¼ï¼ä¾å¦ï¼
ãã// éç¨dateå½æ°çæå½åè§å ä¼ å
¥Y-m-dåæ°
ãã$upload->saveName = array('date','Y-m-d');
ãã// å¦ææå¤ä¸ªåæ°éè¦ä¼ å
¥çè¯ å¯ä»¥ä½¿ç¨æ°ç»
ãã$upload->saveName = array('myFun',array('__FILE__','val1','val2'));
ããå¦æéè¦ä½¿ç¨ä¸ä¼ çåå§æ件åï¼å¯ä»¥éç¨__FILE__ä¼ å
¥ï¼æ以ä¸é¢çå®ä¹è§åï¼æç»çç»ææ¯ myFun('ä¸ä¼ æ件å','val1','val2')æ§è¡çç»æã
ããäºãç´æ¥è®¾ç½®ä¸ä¼ æ件å
ããå¦æä¼ å
¥çåæ°ä¸æ¯ä¸ä¸ªå½æ°åï¼é£ä¹å°±ä¼ç´æ¥å½åæ¯ä¸ä¼ æ件åï¼ä¾å¦ï¼
ãã$upload->saveName = time().'_'.mt_rand();
ãã表示ä¸ä¼ çæ件å½åéç¨æ¶é´æ³å ä¸ä¸ªéæºæ°çç»åå符串æ¹å¼ã
ããå½ç¶ï¼å¦æè§å¾æå¿
è¦ï¼ä½ è¿å¯ä»¥åºå®è®¾ç½®ä¸ä¸ªä¸ä¼ æ件çå½åè§åï¼ç¨äºåºå®ä¿åæ个ä¸ä¼ æ件ã
ãã$upload->saveName = 'ThinkPHP';
ããä¸ãä¿æä¸ä¼ æ件åä¸å
ããå¦æä½ æ³ä¿æä¸ä¼ çæ件åä¸åï¼é£ä¹åªéè¦è®¾ç½®å½åè§è为空å³å¯ï¼ä¾å¦ï¼
ãã$upload->saveName = '';
ããä¸è¬æ¥è¯´ä¸å»ºè®®ä¿æä¸åï¼å 为ä¼å¯¼è´ç¸åçæ件åä¸ä¼ å被è¦ççæ
åµã
ããåç®å½ä¿å
ããsaveNameåªæ¯ç¨äºè®¾ç½®æ件çä¿åè§åï¼ä¸æ¶åå°ç®å½ï¼å¦æå¸æ对ä¸ä¼ çæ件ååç®å½ä¿åï¼å¯ä»¥è®¾ç½®autoSubåsubNameåæ°æ¥å®æï¼ä¾å¦ï¼
ãã// å¼å¯åç®å½ä¿å 并以æ¥æï¼æ ¼å¼ä¸ºYmdï¼ä¸ºåç®å½
ãã$upload->autoSub = true;
ãã$upload->subName = array('date','Ymd');
ããå¯ä»¥ä½¿ç¨èªå®ä¹å½æ°æ¥ä¿åï¼ä¾å¦ï¼
ãã// å¼å¯åç®å½ä¿å 并è°ç¨èªå®ä¹å½æ°get_user_idçæåç®å½
ãã$upload->autoSub = true;
ãã$upload->subName = 'get_user_id';
ããåsaveNameåæ°ä¸æ ·ï¼subNameçå®ä¹å¯ä»¥éç¨æ°ç»åå符串çæ¹å¼ã
ãã注æï¼å¦æget_user_idå½æ°æªå®ä¹çè¯ï¼ä¼ç´æ¥ä»¥get_user_idå符串ä½ä¸ºåç®å½çå称ä¿åã
ããåç®å½ä¿ååæ件å½åè§åå¯ä»¥ç»å使ç¨ã
ããä¸ä¼ 驱å¨
ããä¸ä¼ ç±»å¯ä»¥æ¯æä¸åçç¯å¢ï¼éè¿ç¸åºçä¸ä¼ 驱å¨æ¥è§£å³ï¼é»è®¤æ
åµä¸ä½¿ç¨æ¬å°ï¼Localï¼ä¸ä¼ 驱å¨ï¼å½ç¶ï¼ä½ è¿å¯ä»¥è®¾ç½®å½åé»è®¤çä¸ä¼ 驱å¨ç±»åï¼ä¾å¦ï¼
ãã'FILE_UPLOAD_TYPE' => 'Ftp',
ãã'UPLOAD_TYPE_CONFIG' => array(
ãã'host' => '192.168.1.200', //æå¡å¨
ãã'port' => 21, //端å£
ãã'timeout' => 90, //è¶
æ¶æ¶é´
ãã'username' => 'ftp_user', //ç¨æ·å
ãã'password' => 'ftp_pwd', //å¯ç ),
ãã表示å½å使ç¨Ftpä½ä¸ºä¸ä¼ ç±»ç驱å¨ï¼ä¸ä¼ çæ件ä¼éè¿FTPä¼ å°æå®çè¿ç¨æå¡å¨ã
ããä¹å¯ä»¥å¨å®ä¾åä¸ä¼ ç±»çæ¶åæå®ï¼ä¾å¦ï¼
ãã$config = array(
ãã'maxSize' = 3145728,
ãã'rootPath' = './Uploads/',
ãã'savePath' = '',
ãã'saveName' = array('uniqid',''),
ãã'exts' = array('jpg', 'gif', 'png', 'jpeg'),
ãã'autoSub' = true,
ãã'subName' = array('date','Ymd'),
ãã);
ãã$ftpConfig = array(
ãã'host' => '192.168.1.200', //æå¡å¨
ãã'port' => 21, //端å£
ãã'timeout' => 90, //è¶
æ¶æ¶é´
ãã'username' => 'ftp_user', //ç¨æ·å
ãã'password' => 'ftp_pwd', //å¯ç );
ãã$upload = new \Think\Upload($config,'Ftp',$ftpConfig);// å®ä¾åä¸ä¼ ç±»
ããç®åå·²ç»æ¯æçä¸ä¼ 驱å¨å
æ¬LocalãFtpãSaeãBcsãä¸çååæäºçã
温馨提示:内容为网友见解,仅供参考