作者 lyh

变更数据

... ... @@ -69,24 +69,56 @@ class LyhImportTest extends Command
* @method :post
* @time :2025/10/22 16:47
*/
use PhpOffice\PhpSpreadsheet\IOFactory;
use Illuminate\Support\Facades\DB;
public function download_1517_action($url)
{
// 下载到 Laravel storage 的临时路径
$tempPath = storage_path('app/temp_url.xlsx');
file_put_contents($tempPath, file_get_contents($url));
// 载入 Excel
// 临时文件路径(确保目录存在)
$tempDir = storage_path('app');
if (!is_dir($tempDir)) {
mkdir($tempDir, 0755, true);
}
$tempPath = $tempDir . '/temp_url.xlsx';
try {
// 使用 cURL 下载文件,防止403问题
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true, // 跟随重定向
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; LaravelApp/1.0)', // 模拟浏览器
CURLOPT_TIMEOUT => 30, // 超时防止卡死
]);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 检查下载是否成功
if ($httpCode !== 200 || !$data) {
echo "文件下载失败,HTTP状态码:{$httpCode}";
}
// 保存到临时文件
file_put_contents($tempPath, $data);
// 加载 Excel 文件
$spreadsheet = IOFactory::load($tempPath);
$sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray();
unlink($tempPath);
// 删除临时文件
if (file_exists($tempPath)) {
unlink($tempPath);
}
dd($rows);
}catch (\Exception $e){
echo '文件打不开'.PHP_EOL;
} catch (\Exception $e) {
// 输出更清晰的错误信息
echo '文件下载或解析失败:' . $e->getMessage() . PHP_EOL;
// 清理资源
if (file_exists($tempPath)) {
unlink($tempPath);
}
DB::disconnect('custom_mysql');
}
}
/**
* @remark :3951项目导入产品
* @name :import3951Product
... ...