thinkphpÔõôÉÏ´«txtµ½Êý¾Ý¿â
thinkphpÊÇÒ»¿îÓÅÒìµÄphp¿ª·¢¿ò¼Ü£¬¸Ã¿ò¼ÜÌṩÁËÐí¶à±ã½ÝµÄ¹¦Ð§£¬°üÀ¨ÎļþÉÏ´«¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓÃthinkphp½«txtÎļþÉÏ´«µ½Êý¾Ý¿â¡£
½¨ÉèÊý¾Ý¿â±í
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÊý¾Ý¿â±íÀ´´æ´¢ÉÏ´«µÄtxtÎļþ¡£ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«½¨ÉèÒ»¸öÃûΪ¡°txt_data¡±µÄ±í£¬¸Ã±í°üÀ¨Á½¸ö×ֶΣº¡°id¡±ºÍ¡°content¡±¡£ÆäÖУ¬¡°id¡±ÊÇÖ÷¼ü£¬×Ô¶¯µÝÔö£¬¶ø¡°content¡±ÊÇÓÃÓÚ´æ´¢txtÎļþÄÚÈݵÄ×ֶΡ£
½¨ÉèÉÏ´«±íµ¥
½ÓÏÂÀ´£¬ÎÒÃǽ«½¨ÉèÒ»¸öÉÏ´«±íµ¥£¬ÒÔ±ãÓû§¿ÉÒÔÑ¡ÕªÒªÉÏ´«µÄtxtÎļþ¡£ÔÚ¸Ã±íµ¥ÖУ¬ÎÒÃÇʹÓá°form¡±±êÇ©°ü×°ÎļþÉÏ´«ÊäÈë¿ò¡£
´¦ÀíÉÏ´«ÇëÇó
µ±Óû§Ñ¡ÔñÁËÒªÉÏ´«µÄtxtÎļþ²¢Ìá½»±íµ¥Ê±£¬ÐèÒªÔÚЧÀÍÆ÷¶Ë´¦ÀíÉÏ´«ÇëÇó¡£¹ØÓÚthinkphp¿ò¼Ü¶øÑÔ£¬¿ÉÒÔʹÓá°Request¡±¹¤¾ßÀ´»ñÈ¡ÉÏ´«µÄÎļþ¡£
¶ÁÈ¡ÎļþÄÚÈݲ¢½«Æä´æ´¢µ½Êý¾Ý¿â
ÔÚ»ñÈ¡µ½ÉÏ´«µÄÎļþºó£¬ÎÒÃÇÐèÒª¶ÁÈ¡¸ÃÎļþµÄÄÚÈݲ¢½«Æä´æ´¢µ½Êý¾Ý¿âÖС£¹ØÓÚtxtÎļþ¶øÑÔ£¬ÎÒÃÇ¿ÉÒÔʹÓÃPHPÄÚÖõġ°file_get_contents()¡±º¯ÊýÀ´¶ÁÈ¡ÎļþÄÚÈÝ¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃthinkphp·â×°µÄ¡°Db¡±ÀàÀ´ÏòÊý¾Ý¿âÖвåÈëÒ»Ìõеļͼ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÒÔÏÂÊÇÍêÕûµÄÉÏ´«´úÂ룺
£¨1£©½¨ÉèÊý¾Ý¿â±í
CREATE TABLE txt_data (
id int(11) NOT NULL AUTO_INCREMENT,
content text NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
£¨2£©½¨ÉèÉÏ´«±íµ¥
ÉÏ´«
£¨3£©´¦ÀíÉÏ´«ÇëÇó
public function upload()
{
$file = request()->file('txt_file'); $content = file_get_contents($file->getRealPath()); $data = [ 'content' => $content ]; $result = Db::table('txt_data')->insert($data); if ($result) { return 'ÉÏ´«ÀÖ³É'; } else { return 'ÉÏ´«Ê§°Ü'; }
µÇ¼ºó¸´ÖÆ
}
×ܽ᣺
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇѧϰÁËÔõÑùʹÓÃthinkphp¿ò¼Ü½«txtÎļþÉÏ´«µ½Êý¾Ý¿â¡£Ïêϸ¶øÑÔ£¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪ¡°txt_data¡±µÄÊý¾Ý¿â±íÀ´´æ´¢ÉÏ´«µÄtxtÎļþÄÚÈÝ£¬È»ºó½¨ÉèÁËÒ»¸öÉÏ´«±íµ¥£¬²¢ÔÚЧÀÍÆ÷¶Ë´¦ÀíÁËÉÏ´«ÇëÇó¡£×îºó£¬ÎÒÃÇʹÓá°file_get_contents()¡±º¯Êý¶ÁÈ¡ÉÏ´«µÄtxtÎļþÄÚÈÝ£¬²¢Ê¹Óá°Db¡±ÀཫÆä´æ´¢µ½Êý¾Ý¿âÖС£
ÒÔÉϾÍÊÇthinkphpÔõôÉÏ´«txtµ½Êý¾Ý¿âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡