thinkphpÔõÑùÉÏ´«Îļþ
thinkphpÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü¡£ËüÌṩÁËÐí¶àÓÅÒìµÄ¹¤¾ßºÍ¹¦Ð§£¬Ê¹µÃwebÓ¦ÓõĿª·¢±äµÃÔ½·¢¸ßЧºÍ¼òÆÓ¡£ÆäÖУ¬ÎļþÉÏ´«¹¦Ð§ÊÇÍøÕ¾¿ª·¢Öо³£ÐèÒªÓõ½µÄÒ»¸ö¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphpÖÐʵÏÖÎļþÉÏ´«¡£
Ç°ÖÃÌõ¼þ
ÔÚ×îÏÈʹÓÃÉÏ´«¹¦Ð§Ç°£¬ÐèҪȷ±£ÒѾ׼ȷÉèÖÃÁËphp.iniÎļþ¡£ÏêϸÀ´Ëµ£¬ÒÔϵÄÁ½¸ö²ÎÊýÐèÒªÉèÖÃ׼ȷ£º
file_uploads = On // ÔÊÐíÉÏ´«Îļþ upload_max_filesize = 2M // ÔÊÐíÉÏ´«×î´óµÄÎļþ¾ÞϸΪ2M
µÇ¼ºó¸´ÖÆ
ÈôÊÇÕâÁ½¸ö²ÎÊýûÓÐÉèÖú㬻ᵼÖÂÉÏ´«Ê§°Ü»òÕßÎÞ·¨ÉÏ´«´óÎļþ¡£
Ò³ÃæÉè¼Æ
Ê×ÏÈ£¬ÔÚÒ³ÃæÉÏÐèÒªÉè¼ÆÒ»¸ö±íµ¥ÒÔ±ãÓû§Äܹ»ÉÏ´«Îļþ¡£ÔÚhtml´úÂëÖУ¬¿ÉÒÔÌí¼ÓÈçÏÂµÄ±íµ¥£º
<input> <input>
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬enctype=”multipart/form-data”ÊDZØÐèµÄ£¬ÓÉÓÚÕâ¸öÊôÐÔ¿ÉÒÔ°ü¹Ü±íµ¥Äܹ»×¼È·µØÉÏ´«Îļþ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¿ØÖÆÆ÷
ÔÚThinkphpÖУ¬¿ØÖÆÆ÷ÓÃÓÚÎüÊÕ±íµ¥ÖеÄÊý¾Ý£¬°üÀ¨ÉÏ´«µÄÎļþÐÅÏ¢¡£ÔÚ¿ØÖÆÆ÷ÖУ¬¿ÉÒÔʹÓÃRequestÀàÀ´ÎüÊÕ±íµ¥Êý¾ÝºÍÎļþ£¬¾ÍÏñÏÂÃæÕâÑù£º
public function upload() { $file = request()->file('image'); if ($file) { // ´¦ÀíÉÏ´«µÄÎļþ } else { // ÎļþÉÏ´«Ê§°Ü } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃrequest()º¯ÊýÎüÊÕ±íµ¥ÖеÄÎļþ¡£ÈôÊÇÎļþÉÏ´«Àֳɣ¬¾Í»á½øÈëµ½ifÓï¾ä¿éÖС£
Îļþ´¦Àí
µ±ÎļþÉÏ´«Àֳɺó£¬ÎÒÃÇÐèÒª¶ÔÉÏ´«µÄÎļþ¾ÙÐд¦Àí¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£¬½«ÉÏ´«µÄͼƬÉúÑĵ½Ö¸¶¨µÄÎļþ¼ÐÖУº
public function upload() { $file = request()->file('image'); if ($file) { // ´¦ÀíÉÏ´«µÄÎļþ $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { // ÉúÑÄÎļþÀÖ³É echo $info->getSaveName(); } else { // ÉúÑÄÎļþʧ°Ü echo $file->getError(); } } else { // ÎļþÉÏ´«Ê§°Ü echo "ûÓÐÉÏ´«Îļþ£¡"; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃmoveº¯Êý½«ÎļþÉúÑĵ½ÎÒÃÇÖ¸¶¨µÄÎļþ¼ÐÖС£ÈôÊÇÉúÑÄÀֳɣ¬¾Í»áÊä³öÎļþµÄ·¾¶¡£ÈôÊÇÉúÑÄʧ°Ü£¬¾Í»áÊäÍÉ»¯ÎóÐÅÏ¢¡£
¿¢ÊÂÓï
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃǾͿÉÒÔÔÚThinkphpÖÐʵÏÖÎļþÉÏ´«¹¦Ð§¡£ËäÈ»£¬ÕâÖ»ÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£¬ÉÏ´«ÎļþÀú³ÌÖл¹Éæ¼°µ½Çå¾²ÐÔºÍÐÔÄܵÈÎÊÌ⣬ÐèҪƾ֤ÏÖÕæÏàÐξÙÐÐÍêÉÆ¡£
ÒÔÉϾÍÊÇthinkphpÔõÑùÉÏ´«ÎļþµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡