ÁÄÁÄThinkPHP¿ò¼ÜÉÏ´«Ä¿Â¼µÄÉèÖÃÒªÁì
ÔÚʹÓÃthinkphp¿ò¼Ü¾ÙÐÐÎļþÉÏ´«Ê±£¬ÎÒÃÇÐèÒª¶ÔÉÏ´«ÎļþµÄĿ¼¾ÙÐÐÉèÖá£ÉÏ´«ÎļþµÄĿ¼ÉèÖÿÉÒÔ°ü¹ÜÉÏ´«µÄÎļþ°´ÎÒÃǵĻ®¶¨¾ÙÐд洢£¬Àû±ã¶ÔÎļþµÄÖÎÀíºÍά»¤¡£
ÏÂÃæÎÒÃǾÍÀ´ÏÈÈÝÒ»ÏÂThinkPHP¿ò¼ÜÉÏ´«Ä¿Â¼µÄÉèÖÃÒªÁì¡£
ÉèÖÃÎļþÖÐÉèÖÃ
ÔÚThinkPHP¿ò¼ÜÖУ¬ÉÏ´«Ä¿Â¼µÄÉèÖÿÉÒÔÔÚÓ¦ÓõÄÉèÖÃÎļþÖоÙÐÐÉèÖ᣷¿ªÓ¦ÓõÄÉèÖÃÎļþ£¨Ò»Ñùƽ³£ÊÇconfig.php»òÕßconfigĿ¼ÏµÄapp.php£©£¬ÕÒµ½¡°file_upload_path¡±»òÆäËûÏìÓ¦µÄ²ÎÊý£¬ÐÞ¸ÄÆäֵΪÎÒÃÇÏëÒªµÄÉÏ´«Ä¿Â¼Â·¾¶¼´¿É¡£
ÀýÈ磺
// config.php return [ // ... 'file_upload_path' => './uploads/', // ÉÏ´«Ä¿Â¼ÉèÖà // ... ];
µÇ¼ºó¸´ÖÆ
ÕâÀォÉÏ´«Îļþ´æ´¢µ½uploadsĿ¼Ï¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¿ØÖÆÆ÷ÖÐÉèÖÃ
ÎÒÃÇÒ²¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖжÔÉÏ´«Ä¿Â¼¾ÙÐÐÉèÖã¬ÕâÑù¿ÉÒÔ¶¯Ì¬¼òÖ±¶¨ÉÏ´«ÎļþµÄ´æ´¢Ä¿Â¼¡£
ÔÚÐèÒªÉÏ´«ÎļþµÄ¿ØÖÆÆ÷ÖУ¬ÉèÖÃÉÏ´«ÎļþµÄÉúÑÄ·¾¶¼´¿É¡£Ê¹ÓÃThinkPHP¿ò¼Ü×Ô´øµÄ¡°Request¡±ÀàµÄ¡°file¡±ÒªÁì»ñÈ¡ÉÏ´«Îļþ£¬²¢ÔÚÉúÑÄʱָ¶¨´æ´¢Â·¾¶¼´¿É¡£
ÀýÈ磺
namespace app\index\controller; use think\Controller; use think\Request; class Upload extends Controller { public function index() { // »ñÈ¡ÉÏ´«µÄͼƬÐÅÏ¢ $file = Request::instance()->file('image'); // ÉúÑÄÔÚÒ»¸ö×Ô½ç˵µÄĿ¼ÖÐ $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if($info){ echo $info->getSaveName(); // Êä³öuploads/a.jpg }else{ echo $file->getError(); } } }
µÇ¼ºó¸´ÖÆ
ÕâÀïÉÏ´«µÄͼƬ½«ÉúÑĵ½public/uploadsĿ¼Ï¡£
Ä£×ÓÖÐÉèÖÃ
ÎÒÃÇÒ²¿ÉÒÔÔÚÄ£×ÓÖÐÉèÖÃÉÏ´«Ä¿Â¼£¬ÕâÑù¿ÉÒÔʵÏÖ¶ÔÉÏ´«ÎļþµÄͳһÖÎÀí¡£
ÔÚÄ£×ÓÖÐÌí¼ÓÒ»¸ö»ñÈ¡ÉÏ´«Ä¿Â¼µÄÒªÁ죬ÿ´ÎÉÏ´«ÎļþʱŲÓøÃÒªÁìÀ´»ñÈ¡ÉÏ´«Ä¿Â¼¡£ÉÏ´«µÄÎļþ½«Æ¾Ö¤¸ÃĿ¼¾ÙÐд洢¡£
ÀýÈ磺
namespace app\index\model; use think\Model; class Upload extends Model { // »ñÈ¡ÉÏ´«Ä¿Â¼ public function getUploadDir() { return './uploads/'; } // ÉÏ´«Îļþ public function upload($file) { $path = $this->getUploadDir(); // ... } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏ´«Îļþʱ£¬Å²ÓÃÄ£×ÓÖеÄuploadÒªÁ죬²¢»ñÈ¡ÉÏ´«Ä¿Â¼¾ÙÐд洢¼´¿É¡£
×ܽá
ÔÚThinkPHP¿ò¼ÜÖУ¬ÉÏ´«Ä¿Â¼µÄÉèÖÿÉÒÔÔÚÉèÖÃÎļþ¡¢¿ØÖÆÆ÷¡¢Ä£×ÓÖоÙÐÐÉèÖá£ÕâЩÉèÖö¼¿ÉÒÔʵÏÖÉÏ´«ÎļþµÄ·¾¶Í³Ò»ÖÎÀí£¬Àû±ã¶ÔÉÏ´«ÎļþµÄÖÎÀíºÍά»¤¡£
ÒÔÉϾÍÊÇÁÄÁÄThinkPHP¿ò¼ÜÉÏ´«Ä¿Â¼µÄÉèÖÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡