在 Ubuntu + ThinkPHP 环境下“导入数据”,常见有几种场景。下面按最常用方式给你说明,你可以对照自己的需求选。
适合:整库、整表导入(如 .sql 文件)
ssh user@your_server_ip
mysql -u 用户名 -p 数据库名 < /path/to/data.sql
示例:
mysql -u root -p test_db < /home/ubuntu/data.sql
若 ThinkPHP 使用的是 MySQL / MariaDB,这是最快方式。
适合:在代码中执行 SQL 文件
executeDb::execute(file_get_contents('./data.sql'));
php think db:execute < data.sql
适合:Excel / CSV / API 数据
use app\model\User;
$data = [
['name' => '张三', 'age' => 20],
['name' => '李四', 'age' => 22],
];
User::insertAll($data);
composer require phpoffice/phpspreadsheet
use PhpOffice\PhpSpreadsheet\IOFactory;
use app\model\User;
$spreadsheet = IOFactory::load('test.xlsx');
$sheet = $spreadsheet->getActiveSheet();
foreach ($sheet->toArray() as $row) {
User::create([
'name' => $row[0],
'age' => $row[1]
]);
}
php think migrate:run
php think seed:run
sudo chmod -R 755 /var/www/html
sudo chown -R www-data:www-data /var/www/html
修改:
sudo vim /etc/php/8.1/apache2/php.ini
upload_max_filesize = 20M
post_max_size = 20M
我可以给你精确命令或代码。