thinkphpʵÏÖ±íµ¥ÉÏ´«¸½¼þ
ÔÚweb¿ª·¢ÖУ¬±íµ¥µÄÉÏ´«¸½¼þ¹¦Ð§ÊǷǾ³£¼ûµÄ£¬Õâ¸ö¹¦Ð§Äܹ»ÈÃÓû§Àû±ãµØÉÏ´«×Ô¼ºËùÐèÒªµÄÎļþ£¬ºÃ±ÈͼƬ¡¢Îĵµ¡¢ÒôƵµÈµÈ¡£¶øÔÚphpÓïÑÔÖУ¬¿ÉÒÔʹÓÃthinkphp¿ò¼ÜÀ´ÊµÏÖ±íµ¥ÉÏ´«¸½¼þµÄ¹¦Ð§¡£ÏÂÃæÎÒÃÇÀ´Ò»²½Ò»²½µØѧϰÔõÑùʹÓÃthinkphpʵÏÖ±íµ¥ÉÏ´«¸½¼þ¡£
Ò»¡¢ÇéÐÎ×¼±¸
ÔÚʹÓÃthinkphp¿ò¼Ü֮ǰ£¬ÎÒÃÇÐèÒªÏȴһ¸öÊʵ±µÄÔËÐÐÇéÐΡ£ÏêϸµÄ°ì·¨ÈçÏ£º
×°ÖÃphpÇéÐÎ
×°ÖÃcomposer
н¨Ò»¸öthinkphpÏîÄ¿
ÉèÖÃÊý¾Ý¿âºÍ·ÓÉ
×°ÖÃÐëÒªµÄÀ©Õ¹¿â
¶þ¡¢½¨ÉèÉÏ´«±íµ¥
ÔÚthinkphp¿ò¼ÜÖУ¬¿ÉÒÔʹÓÃform±íµ¥ÌìÉúÆ÷À´¿ìËÙÌìÉúÒ»¸öÉÏ´«±íµ¥¡£ÏêϸʵÏÖ°ì·¨ÈçÏ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
½¨ÉèÒ»¸öÃûΪuploadµÄ¿ØÖÆÆ÷£¬ÔڸÿØÖÆÆ÷ÖУ¬Ìí¼ÓÒ»¸öindexÒªÁì¡£
namespace appindexcontroller; use thinkController; class Upload extends Controller { public function index() { return $this->fetch(); } }
µÇ¼ºó¸´ÖÆ
ÔÚviewÎļþ¼ÐÖÐн¨Ò»¸öupload.htmlÎļþ¡£
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="ÉÏ´«"> </form> </body> </html>
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë»áÌìÉúÒ»¸ö°üÀ¨ÎļþÉÏ´«¹¦Ð§µÄ±íµ¥£¬ÆäÖÐinput±êÇ©µÄnameÊôÐÔΪfile£¬¼´ÉÏ´«ÎļþµÄÃû³Æ¡£
Èý¡¢´¦ÀíÉÏ´«Îļþ
µ±Óû§µã»÷ÉÏ´«°´Å¥²¢Ñ¡ÔñÁËÉÏ´«Îļþºó£¬ÎÒÃǾÍÐèÒª½«ÉÏ´«µÄÎļþÉúÑĵ½Ö¸¶¨µÄλÖá£Õâ¸ö¹¦Ð§ÐèÒªÔÚ¿ØÖÆÆ÷ÖÐʵÏÖ£¬Ïêϸ°ì·¨ÈçÏ£º
ÐÞ¸Äupload.htmlµÄform±íµ¥actionÊôÐÔΪupload/uploadFile¡£
<form action="upload/uploadFile" method="post" enctype="multipart/form-data">
µÇ¼ºó¸´ÖÆ
ÔÚupload¿ØÖÆÆ÷ÖÐÌí¼ÓuploadFileÒªÁ죬¸ÃÒªÌå»á½«ÉÏ´«ÎļþÉúÑĵ½Ö¸¶¨µÄλÖò¢·µ»ØÉÏ´«ÎļþµÄÐÅÏ¢¡£ÓÉÓÚthinkphp¿ò¼Ü×Ô´øÁËÎļþÉÏ´«À࣬ÎÒÃÇ¿ÉÒÔÖ±½ÓʹÓøÃÀàÀ´Íê³ÉÎļþÉÏ´«µÄ¹¦Ð§¡£
public function uploadFile() { $file = request()->file('file'); $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { return 'ÎļþÉÏ´«Àֳɣº' . $info->getSaveName(); } else { return $file->getError(); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë¿ÉÒÔ»ñÈ¡µ½ÉÏ´«µÄÎļþ¹¤¾ß£¬È»ºóʹÓÃÎļþÉÏ´«ÀàÀ´ÉúÑÄÎļþµ½Ö¸¶¨µÄλÖá£ÈôÊÇÉÏ´«Àֳɣ¬Ôò·µ»ØÉÏ´«ÎļþµÄÐÅÏ¢£»²»È»¾Í·µ»ØÉÏ´«¹ýʧÐÅÏ¢¡£
ÔÚÍê³ÉÒÔÉÏ°ì·¨ºó£¬ÎÒÃǵÄÉÏ´«¸½¼þ¹¦Ð§¾ÍÍê³ÉÁË¡£Óû§¿ÉÒÔͨ¹ýÉÏ´«±íµ¥ÇáËɵØÉÏ´«×Ô¼ºµÄÎļþÁË¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇʹÓÃthinkphp¿ò¼ÜÀ´ÊµÏÖÁË±íµ¥ÉÏ´«¸½¼þµÄ¹¦Ð§¡£Õâ¸öʵÏÖ°ì·¨½ÏÁ¿¼òÆÓ£¬Ö»ÐèҪƾ֤ÉÏÊö°ì·¨Ò»²½Ò»²½µØÍê³É¼´¿É¡£ÖµµÃ×¢ÖصÄÊÇ£¬thinkphp¿ò¼ÜÌṩÁ˺ÜÊǸ»ºñµÄ¹¦Ð§ºÍÀà¿â£¬ÉîÈëѧϰthinkphp¿ò¼Ü¿ÉÒÔÈÃÎÒÃÇÔÚweb¿ª·¢ÖÐÔ½·¢¸ßЧµØÍê³ÉʹÃü¡£
ÒÔÉϾÍÊÇthinkphpʵÏÖ±íµ¥ÉÏ´«¸½¼þµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡