|
@@ -373,145 +373,17 @@ class PublicController extends AbstractController
|
|
$fileType = 'zip';
|
|
$fileType = 'zip';
|
|
$date = date('Ymd');
|
|
$date = date('Ymd');
|
|
$filePath = $fileType . DIRECTORY_SEPARATOR . $date;
|
|
$filePath = $fileType . DIRECTORY_SEPARATOR . $date;
|
|
- // 使用绝对路径并规范化
|
|
|
|
- $allDir = BASE_PATH . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . $filePath;
|
|
|
|
- $allDir = realpath($allDir) ?: $allDir;
|
|
|
|
-
|
|
|
|
- // 打印目录路径以便调试
|
|
|
|
- error_log("DownloadFile: Full directory path: $allDir");
|
|
|
|
-
|
|
|
|
|
|
+ $allDir = 'public' . DIRECTORY_SEPARATOR . $filePath;
|
|
if (!is_dir($allDir)) {
|
|
if (!is_dir($allDir)) {
|
|
if (!mkdir($allDir, 0755, true)) {
|
|
if (!mkdir($allDir, 0755, true)) {
|
|
- error_log("DownloadFile: Failed to create directory: $allDir");
|
|
|
|
return Result::error('创建文件夹失败');
|
|
return Result::error('创建文件夹失败');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // 生成绝对路径的ZIP文件名
|
|
|
|
- $zipFileName = $allDir . DIRECTORY_SEPARATOR . $requireData['fileName'] . time() . mt_rand(1, 1000000) . '.zip';
|
|
|
|
- error_log("DownloadFile: ZIP file path: $zipFileName");
|
|
|
|
- error_log("DownloadFile: Directory exists: " . (is_dir($allDir) ? 'yes' : 'no'));
|
|
|
|
- error_log("DownloadFile: Directory writable: " . (is_writable($allDir) ? 'yes' : 'no'));
|
|
|
|
-
|
|
|
|
- // 添加OVERWRITE标志确保文件可以被创建
|
|
|
|
- if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
|
|
|
|
- // 将要下载的文件逐个添加到zip文件中
|
|
|
|
- /** @var array<string> $files */
|
|
|
|
- $files = [];
|
|
|
|
- if (is_array($requireData['files'])) {
|
|
|
|
- $files = $requireData['files'];
|
|
|
|
- } elseif (is_string($requireData['files'])) {
|
|
|
|
- $files = [$requireData['files']];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 添加进度日志
|
|
|
|
- error_log("DownloadFile: Processing " . count($files) . " files");
|
|
|
|
-
|
|
|
|
- $addedCount = 0;
|
|
|
|
- $failedCount = 0;
|
|
|
|
-
|
|
|
|
- foreach ($files as $key => $filePathu) {
|
|
|
|
- if (!is_string($filePathu)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 生成文件名:key + 1
|
|
|
|
- $innerFileName = $requireData['names'][$key] ?? 'file_' . ($key + 1); //($key + 1) . $this->getFileExtension($filePathu);
|
|
|
|
-
|
|
|
|
- // 处理远程URL文件
|
|
|
|
- if (filter_var($filePathu, FILTER_VALIDATE_URL)) {
|
|
|
|
- error_log("DownloadFile: Processing remote file $key: $filePathu");
|
|
|
|
- $this->addRemoteFileToZip($zip, $filePathu, $innerFileName);
|
|
|
|
- $addedCount++;
|
|
|
|
- } else {
|
|
|
|
- // 处理本地文件
|
|
|
|
- error_log("DownloadFile: Processing local file $key: $filePathu");
|
|
|
|
- if (!file_exists($filePathu)) {
|
|
|
|
- // 尝试添加public前缀
|
|
|
|
- $fullPath = 'public/' . $filePathu;
|
|
|
|
- if (!file_exists($fullPath)) {
|
|
|
|
- var_dump("local路径:",BASE_PATH);
|
|
|
|
- // 尝试使用绝对路径
|
|
|
|
- $fullPath = BASE_PATH . '/public/' . $filePathu;
|
|
|
|
- if (!file_exists($fullPath)) {
|
|
|
|
- error_log("DownloadFile: File not found: $filePathu");
|
|
|
|
- $failedCount++;
|
|
|
|
- continue; // 跳过不存在的文件
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- $filePathu = $fullPath;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 确保文件存在且可读
|
|
|
|
- if (is_file($filePathu) && is_readable($filePathu)) {
|
|
|
|
- if ($zip->addFile($filePathu, $innerFileName)) {
|
|
|
|
- $addedCount++;
|
|
|
|
- error_log("DownloadFile: Successfully added local file: $innerFileName");
|
|
|
|
- } else {
|
|
|
|
- $failedCount++;
|
|
|
|
- error_log("DownloadFile: Failed to add local file: $innerFileName");
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- $failedCount++;
|
|
|
|
- error_log("DownloadFile: File not readable: $filePathu");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- error_log("DownloadFile: Summary - Added: $addedCount, Failed: $failedCount");
|
|
|
|
-
|
|
|
|
- // 关闭zip文件
|
|
|
|
- $closeResult = $zip->close();
|
|
|
|
- error_log("DownloadFile: ZIP close result: " . ($closeResult ? 'success' : 'failed'));
|
|
|
|
-
|
|
|
|
- if (!$closeResult) {
|
|
|
|
- error_log("DownloadFile: Failed to close ZIP file: $zipFileName");
|
|
|
|
- return Result::error('ZIP文件关闭失败');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 检查ZIP文件是否创建成功
|
|
|
|
- if (!file_exists($zipFileName)) {
|
|
|
|
- error_log("DownloadFile: ZIP file does not exist after close: $zipFileName");
|
|
|
|
- return Result::error('ZIP文件创建失败');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $zipFileSize = filesize($zipFileName);
|
|
|
|
- if ($zipFileSize === false || $zipFileSize === 0) {
|
|
|
|
- error_log("DownloadFile: ZIP file is empty or cannot get size: $zipFileName");
|
|
|
|
- return Result::error('ZIP文件为空');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- error_log("DownloadFile: ZIP file created successfully: $zipFileName (size: $zipFileSize bytes)");
|
|
|
|
-
|
|
|
|
- // 将zip文件提供给用户进行下载
|
|
|
|
- $fileUrlName = explode("public", $zipFileName);
|
|
|
|
- if (count($fileUrlName) < 2) {
|
|
|
|
- return Result::error('无法生成下载链接');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $downloadUrl = env('HOST') . $fileUrlName[1];
|
|
|
|
- error_log("DownloadFile: Download URL: $downloadUrl");
|
|
|
|
-
|
|
|
|
- return Result::success(['fileUrl' => $downloadUrl]);
|
|
|
|
- } else {
|
|
|
|
- return Result::error('无法创建zip文件');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!is_dir($allDir)) {
|
|
|
|
- if (!mkdir($allDir, 0755, true)) {
|
|
|
|
- error_log("DownloadFile: Failed to create directory: $allDir");
|
|
|
|
- return Result::error('创建文件夹失败');
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // 生成绝对路径的ZIP文件名
|
|
|
|
|
|
+ $fileName = $requireData['fileName'] . time() . mt_rand(1, 1000000) . '.zip';
|
|
$zipFileName = $allDir . DIRECTORY_SEPARATOR . $requireData['fileName'] . time() . mt_rand(1, 1000000) . '.zip';
|
|
$zipFileName = $allDir . DIRECTORY_SEPARATOR . $requireData['fileName'] . time() . mt_rand(1, 1000000) . '.zip';
|
|
// $zipFileName = 'public/zip/files.zip';
|
|
// $zipFileName = 'public/zip/files.zip';
|
|
|
|
|
|
- error_log("DownloadFile: ZIP file path: $zipFileName");
|
|
|
|
- error_log("DownloadFile: Directory exists: " . (is_dir($allDir) ? 'yes' : 'no'));
|
|
|
|
- error_log("DownloadFile: Directory writable: " . (is_writable($allDir) ? 'yes' : 'no'));
|
|
|
|
-
|
|
|
|
- // 添加OVERWRITE标志确保文件可以被创建
|
|
|
|
- if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
|
|
|
|
|
|
+ if ($zip->open($zipFileName, ZipArchive::CREATE) === true) {
|
|
// 将要下载的文件逐个添加到zip文件中
|
|
// 将要下载的文件逐个添加到zip文件中
|
|
/** @var array<string> $files */
|
|
/** @var array<string> $files */
|
|
$files = [];
|
|
$files = [];
|
|
@@ -521,38 +393,26 @@ class PublicController extends AbstractController
|
|
$files = [$requireData['files']];
|
|
$files = [$requireData['files']];
|
|
}
|
|
}
|
|
|
|
|
|
- // 添加进度日志
|
|
|
|
- error_log("DownloadFile: Processing " . count($files) . " files");
|
|
|
|
-
|
|
|
|
- $addedCount = 0;
|
|
|
|
- $failedCount = 0;
|
|
|
|
-
|
|
|
|
foreach ($files as $key => $filePathu) {
|
|
foreach ($files as $key => $filePathu) {
|
|
if (!is_string($filePathu)) {
|
|
if (!is_string($filePathu)) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
// 生成文件名:key + 1
|
|
// 生成文件名:key + 1
|
|
- $innerFileName = $requireData['names'][$key] ?? 'file_' . ($key + 1); //($key + 1) . $this->getFileExtension($filePathu);
|
|
|
|
|
|
+ $fileName = $requireData['names'][$key]; //($key + 1) . $this->getFileExtension($filePathu);
|
|
|
|
|
|
// 处理远程URL文件
|
|
// 处理远程URL文件
|
|
if (filter_var($filePathu, FILTER_VALIDATE_URL)) {
|
|
if (filter_var($filePathu, FILTER_VALIDATE_URL)) {
|
|
- error_log("DownloadFile: Processing remote file $key: $filePathu");
|
|
|
|
- $this->addRemoteFileToZip($zip, $filePathu, $innerFileName);
|
|
|
|
- $addedCount++;
|
|
|
|
|
|
+ $this->addRemoteFileToZip($zip, $filePathu, $fileName);
|
|
} else {
|
|
} else {
|
|
// 处理本地文件
|
|
// 处理本地文件
|
|
- error_log("DownloadFile: Processing local file $key: $filePathu");
|
|
|
|
if (!file_exists($filePathu)) {
|
|
if (!file_exists($filePathu)) {
|
|
// 尝试添加public前缀
|
|
// 尝试添加public前缀
|
|
$fullPath = 'public/' . $filePathu;
|
|
$fullPath = 'public/' . $filePathu;
|
|
if (!file_exists($fullPath)) {
|
|
if (!file_exists($fullPath)) {
|
|
- var_dump("local路径:",BASE_PATH);
|
|
|
|
// 尝试使用绝对路径
|
|
// 尝试使用绝对路径
|
|
$fullPath = BASE_PATH . '/public/' . $filePathu;
|
|
$fullPath = BASE_PATH . '/public/' . $filePathu;
|
|
if (!file_exists($fullPath)) {
|
|
if (!file_exists($fullPath)) {
|
|
- error_log("DownloadFile: File not found: $filePathu");
|
|
|
|
- $failedCount++;
|
|
|
|
continue; // 跳过不存在的文件
|
|
continue; // 跳过不存在的文件
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -561,55 +421,15 @@ class PublicController extends AbstractController
|
|
|
|
|
|
// 确保文件存在且可读
|
|
// 确保文件存在且可读
|
|
if (is_file($filePathu) && is_readable($filePathu)) {
|
|
if (is_file($filePathu) && is_readable($filePathu)) {
|
|
- if ($zip->addFile($filePathu, $innerFileName)) {
|
|
|
|
- $addedCount++;
|
|
|
|
- error_log("DownloadFile: Successfully added local file: $innerFileName");
|
|
|
|
- } else {
|
|
|
|
- $failedCount++;
|
|
|
|
- error_log("DownloadFile: Failed to add local file: $innerFileName");
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- $failedCount++;
|
|
|
|
- error_log("DownloadFile: File not readable: $filePathu");
|
|
|
|
|
|
+ $zip->addFile($filePathu, $fileName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- error_log("DownloadFile: Summary - Added: $addedCount, Failed: $failedCount");
|
|
|
|
-
|
|
|
|
// 关闭zip文件
|
|
// 关闭zip文件
|
|
- $closeResult = $zip->close();
|
|
|
|
- error_log("DownloadFile: ZIP close result: " . ($closeResult ? 'success' : 'failed'));
|
|
|
|
-
|
|
|
|
- if (!$closeResult) {
|
|
|
|
- error_log("DownloadFile: Failed to close ZIP file: $zipFileName");
|
|
|
|
- return Result::error('ZIP文件关闭失败');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 检查ZIP文件是否创建成功
|
|
|
|
- if (!file_exists($zipFileName)) {
|
|
|
|
- error_log("DownloadFile: ZIP file does not exist after close: $zipFileName");
|
|
|
|
- return Result::error('ZIP文件创建失败');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $zipFileSize = filesize($zipFileName);
|
|
|
|
- if ($zipFileSize === false || $zipFileSize === 0) {
|
|
|
|
- error_log("DownloadFile: ZIP file is empty or cannot get size: $zipFileName");
|
|
|
|
- return Result::error('ZIP文件为空');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- error_log("DownloadFile: ZIP file created successfully: $zipFileName (size: $zipFileSize bytes)");
|
|
|
|
-
|
|
|
|
|
|
+ $zip->close();
|
|
// 将zip文件提供给用户进行下载
|
|
// 将zip文件提供给用户进行下载
|
|
$fileUrlName = explode("public", $zipFileName);
|
|
$fileUrlName = explode("public", $zipFileName);
|
|
- if (count($fileUrlName) < 2) {
|
|
|
|
- return Result::error('无法生成下载链接');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $downloadUrl = env('HOST') . $fileUrlName[1];
|
|
|
|
- error_log("DownloadFile: Download URL: $downloadUrl");
|
|
|
|
-
|
|
|
|
- return Result::success(['fileUrl' => $downloadUrl]);
|
|
|
|
|
|
+ return Result::success(['fileUrl' => env('HOST') . $fileUrlName[1]]);
|
|
} else {
|
|
} else {
|
|
return Result::error('无法创建zip文件');
|
|
return Result::error('无法创建zip文件');
|
|
}
|
|
}
|
|
@@ -620,15 +440,9 @@ class PublicController extends AbstractController
|
|
*/
|
|
*/
|
|
private function addRemoteFileToZip($zip, $url, $fileName = null): void
|
|
private function addRemoteFileToZip($zip, $url, $fileName = null): void
|
|
{
|
|
{
|
|
- var_dump("$$$$$$$$$$$$$$$$$$$$");
|
|
|
|
- $tempFile = null;
|
|
|
|
try {
|
|
try {
|
|
// 创建临时文件
|
|
// 创建临时文件
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'remote_file_');
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'remote_file_');
|
|
- if ($tempFile === false) {
|
|
|
|
- error_log("DownloadFile: Failed to create temp file for URL: $url");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
|
|
// 设置更长的超时时间和更好的下载参数
|
|
// 设置更长的超时时间和更好的下载参数
|
|
$context = stream_context_create([
|
|
$context = stream_context_create([
|
|
@@ -659,26 +473,26 @@ class PublicController extends AbstractController
|
|
}
|
|
}
|
|
|
|
|
|
if ($fileContent === false) {
|
|
if ($fileContent === false) {
|
|
- error_log("DownloadFile: Failed to download remote file: " . $url);
|
|
|
|
|
|
+ error_log("Failed to download remote file: " . $url);
|
|
return; // 下载失败,跳过
|
|
return; // 下载失败,跳过
|
|
}
|
|
}
|
|
|
|
|
|
// 验证文件内容
|
|
// 验证文件内容
|
|
if (empty($fileContent) || strlen($fileContent) < 100) {
|
|
if (empty($fileContent) || strlen($fileContent) < 100) {
|
|
- error_log("DownloadFile: Downloaded file is too small or empty: " . $url . " (size: " . strlen($fileContent) . ")");
|
|
|
|
|
|
+ error_log("Downloaded file is too small or empty: " . $url);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 写入临时文件并验证
|
|
// 写入临时文件并验证
|
|
$bytesWritten = file_put_contents($tempFile, $fileContent);
|
|
$bytesWritten = file_put_contents($tempFile, $fileContent);
|
|
if ($bytesWritten === false || $bytesWritten !== strlen($fileContent)) {
|
|
if ($bytesWritten === false || $bytesWritten !== strlen($fileContent)) {
|
|
- error_log("DownloadFile: Failed to write file content: " . $url . " (written: $bytesWritten, expected: " . strlen($fileContent) . ")");
|
|
|
|
|
|
+ error_log("Failed to write file content: " . $url);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 验证文件是否可读
|
|
// 验证文件是否可读
|
|
if (!is_readable($tempFile) || filesize($tempFile) < 100) {
|
|
if (!is_readable($tempFile) || filesize($tempFile) < 100) {
|
|
- error_log("DownloadFile: Temporary file is not readable or too small: " . $tempFile . " (size: " . filesize($tempFile) . ")");
|
|
|
|
|
|
+ error_log("Temporary file is not readable or too small: " . $tempFile);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -692,20 +506,13 @@ class PublicController extends AbstractController
|
|
|
|
|
|
// 添加到zip并验证
|
|
// 添加到zip并验证
|
|
if (!$zip->addFile($tempFile, $fileName)) {
|
|
if (!$zip->addFile($tempFile, $fileName)) {
|
|
- error_log("DownloadFile: Failed to add file to zip: " . $fileName);
|
|
|
|
|
|
+ error_log("Failed to add file to zip: " . $fileName);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- error_log("DownloadFile: Successfully added remote file to ZIP: $fileName");
|
|
|
|
-
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
// 记录错误但继续处理其他文件
|
|
// 记录错误但继续处理其他文件
|
|
- error_log("DownloadFile: Exception while downloading remote file: " . $url . " - " . $e->getMessage());
|
|
|
|
- } finally {
|
|
|
|
- // 清理临时文件
|
|
|
|
- if ($tempFile !== null && file_exists($tempFile)) {
|
|
|
|
- @unlink($tempFile);
|
|
|
|
- }
|
|
|
|
|
|
+ error_log("Failed to download remote file: " . $url . " - " . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -742,24 +549,23 @@ class PublicController extends AbstractController
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
|
|
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
|
|
$actualLength = strlen($content);
|
|
$actualLength = strlen($content);
|
|
- $error = curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
curl_close($ch);
|
|
|
|
|
|
// 检查HTTP状态码和内容长度
|
|
// 检查HTTP状态码和内容长度
|
|
if ($content === false || $httpCode !== 200) {
|
|
if ($content === false || $httpCode !== 200) {
|
|
- error_log("DownloadFile: cURL download failed for URL: $url, HTTP Code: $httpCode, Error: $error");
|
|
|
|
|
|
+ error_log("cURL download failed for URL: $url, HTTP Code: $httpCode");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// 检查内容长度是否匹配
|
|
// 检查内容长度是否匹配
|
|
if ($contentLength > 0 && $actualLength !== $contentLength) {
|
|
if ($contentLength > 0 && $actualLength !== $contentLength) {
|
|
- error_log("DownloadFile: Content length mismatch for URL: $url, Expected: $contentLength, Actual: $actualLength");
|
|
|
|
|
|
+ error_log("Content length mismatch for URL: $url, Expected: $contentLength, Actual: $actualLength");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// 检查文件是否为空或太小
|
|
// 检查文件是否为空或太小
|
|
if (empty($content) || $actualLength < 100) {
|
|
if (empty($content) || $actualLength < 100) {
|
|
- error_log("DownloadFile: File too small or empty for URL: $url, Size: $actualLength");
|
|
|
|
|
|
+ error_log("File too small or empty for URL: $url, Size: $actualLength");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|