ÔõÑùÔÚThinkPHP6ÖоÙÐжàÎļþÉÏ´«²Ù×÷£¿
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄһֱǰ½ø£¬Ô½À´Ô½¶àµÄÍøÕ¾ºÍÓ¦ÓÃÐèÒª¾ÙÐÐÎļþÉÏ´«²Ù×÷¡£ÔÚ´ËÅä¾°Ï£¬thinkphp6×÷Ϊһ¿îÓÅÒìµÄphp¿ò¼Ü£¬Ò²Îª¶àÎļþÉÏ´«ÌṩÁ˱ã½ÝµÄ²Ù×÷·½·¨¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6ÖоÙÐжàÎļþÉÏ´«²Ù×÷¡£
Ò»¡¢ÉÏ´«ÎļþµÄÏà¹Ø´úÂë
ÔÚThinkPHP6ÖУ¬ÉÏ´«ÎļþµÄ´úÂëλÓÚ¿ØÖÆÆ÷ÎļþÖС£ÒÔÏÂÊÇÒ»¶ÎÉÏ´«µ¥¸öÎļþµÄ´úÂ룺
public function upload() { //»ñÈ¡ÉÏ´«µÄÎļþ¹¤¾ß $file = request()->file('file'); //½«ÉÏ´«µÄÎļþÒƶ¯µ½Ö¸¶¨Ä¿Â¼ $info = $file->move('./uploads'); if ($info) { //ÉÏ´«Àֳɣ¬·µ»ØÎļþÃûºÍ·¾¶ return json(['code' => 0, 'msg' => 'ÉÏ´«ÀÖ³É', 'data' => ['file_name' => $info->getFilename(), 'file_path' => '/uploads/'.$info->getSaveName()]]); } else { //ÉÏ´«Ê§°Ü£¬·µ»Ø¹ýʧÐÅÏ¢ return json(['code' => 1, 'msg' => $file->getError()]); } }
µÇ¼ºó¸´ÖÆ
¶þ¡¢ÉÏ´«¶à¸öÎļþµÄ²Ù×÷°ì·¨
½ÓÏÂÀ´£¬ÎÒÃÇÀ´ÏÈÈÝÔõÑùÔÚThinkPHP6ÖоÙÐжàÎļþÉÏ´«²Ù×÷¡£ÆäÏêϸ°ì·¨Îª£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1.ÔÚÇ°¶Ë½çÃæÖÐÌí¼Ó¶à¸öÎļþÉÏ´«¿ò£¬²¢ÉèÖÃnameÊôÐÔÖµÏàͬ¡£
<form enctype="multipart/form-data" method="post" action="#"> <input type="file" name="files[]" multiple> <button type="submit">ÉÏ´«</button> </form>
µÇ¼ºó¸´ÖÆ
2.ÔÚ¿ØÖÆÆ÷ÎļþÖÐÉèÖÃÉÏ´«¶à¸öÎļþµÄ´úÂë¡£
public function upload() { $files = request()->file('files'); $data = array(); foreach ($files as $file) { $info = $file->validate(['size' => 1024*1024*10, 'ext' => 'jpg,png,gif'])->move('./uploads'); if ($info) { $data[] = ['file_name' => $info->getFilename(), 'file_path' => '/uploads/'.$info->getSaveName()]; } else { return json(['code' => 1, 'msg' => $file->getError()]); } } return json(['code' => 0, 'msg' => 'ÉÏ´«ÀÖ³É', 'data' => $data]); }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬request()->file(‘files’)¿É»ñÈ¡µ½ÉÏ´«µÄ¶à¸öÎļþ¹¤¾ß£¬Í¨¹ýÑ»·±éÏòÀ´´¦Àíÿ¸öÉÏ´«ÎļþµÄ²Ù×÷¡£
Èý¡¢ÉÏ´«¶à¸öÎļþµÄ²ÎÊýÉèÖÃ
ΪÁËÈ·±£ÉÏ´«¶à¸öÎļþµÄÇå¾²ÐÔºÍÕýµ±ÐÔ£¬ÎÒÃÇ»¹¿ÉÒÔÉèÖÃһЩ²ÎÊýÀ´ÏÞÖÆÉÏ´«ÎļþµÄ¾Þϸ¡¢ÀàÐ͵ȡ£ÀýÈ磺
1.ÏÞÖƵ¥¸öÎļþ¾Þϸ
ÔÚ±¾ÎĵÄʾÀý´úÂëÖУ¬ÎÒÃÇͨ¹ývalidate([‘size’ => 1024102410, ‘ext’ => ‘jpg,png,gif’])À´ÉèÖÃÉÏ´«µ¥¸öÎļþ¾Þϸ²»Áè¼Ý10M¡£
2.ÏÞÖÆÎļþÀàÐÍ
ÔÚvalidate()º¯ÊýÖУ¬Ò²¿ÉÒÔͨ¹ýÉèÖÃextÀ´ÏÞÖÆÉÏ´«ÎļþÀàÐÍ£¬ÀýÈ磺’ext’ => ‘jpg,png,gif’
3.ÖØÃüÃûÉÏ´«Îļþ
ͨ¹ý
$info = $file->move('./uploads', md5(uniqid()));
µÇ¼ºó¸´ÖÆ
¿ÉÒÔÉèÖÃÉÏ´«ÎļþµÄÃû³ÆÊÇÒ»¸öΨһµÄËæ»úÊý¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚThinkPHP6ÖоÙÐжàÎļþÉÏ´«²Ù×÷£¬ÐèҪʹÓõ½request()->file(‘files’)À´»ñÈ¡ÉÏ´«µÄ¶à¸öÎļþ¹¤¾ß£¬Ê¹ÓÃforeachÑ»·À´´¦Àíÿ¸öÉÏ´«Îļþ¡£Í¬Ê±£¬ÎÒÃÇ»¹¿ÉÒÔͨ¹ýÉèÖòÎÊýÀ´ÏÞÖÆÉÏ´«ÎļþµÄ¾Þϸ¡¢ÀàÐ͵ȣ¬È·±£ÉÏ´«µÄ¶à¸öÎļþµÄÇå¾²ÐÔºÍÕýµ±ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖоÙÐжàÎļþÉÏ´«²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡