test_directory_fix.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * 测试PDF目录创建修复
  4. * 在服务器上运行: php test_directory_fix.php
  5. */
  6. // 测试1: 检查storage目录结构
  7. echo "=== 测试1: 检查storage目录结构 ===\n";
  8. $storagePath = __DIR__ . '/storage';
  9. $publicPath = $storagePath . '/app/public';
  10. $examsPath = $publicPath . '/exams';
  11. echo "Storage路径: {$storagePath}\n";
  12. echo "Public路径: {$publicPath}\n";
  13. echo "Exams路径: {$examsPath}\n\n";
  14. // 检查目录是否存在
  15. echo "=== 测试2: 检查目录存在性 ===\n";
  16. echo "storage存在: " . (is_dir($storagePath) ? "是" : "否") . "\n";
  17. echo "app存在: " . (is_dir($storagePath . '/app') ? "是" : "否") . "\n";
  18. echo "public存在: " . (is_dir($publicPath) ? "是" : "否") . "\n";
  19. echo "exams存在: " . (is_dir($examsPath) ? "是" : "否") . "\n\n";
  20. // 测试3: 创建exams目录
  21. echo "=== 测试3: 创建exams目录 ===\n";
  22. if (!is_dir($examsPath)) {
  23. echo "exams目录不存在,正在创建...\n";
  24. if (mkdir($examsPath, 0755, true)) {
  25. echo "✓ exams目录创建成功\n";
  26. echo "权限: " . substr(sprintf('%o', fileperms($examsPath)), -4) . "\n";
  27. } else {
  28. echo "✗ exams目录创建失败\n";
  29. echo "错误: " . error_get_last()['message'] . "\n";
  30. }
  31. } else {
  32. echo "✓ exams目录已存在\n";
  33. echo "权限: " . substr(sprintf('%o', fileperms($examsPath)), -4) . "\n";
  34. }
  35. // 测试4: 测试文件写入
  36. echo "\n=== 测试4: 测试文件写入 ===\n";
  37. $testFile = $examsPath . '/test_write.txt';
  38. $testContent = '测试时间: ' . date('Y-m-d H:i:s') . "\n";
  39. if (file_put_contents($testFile, $testContent)) {
  40. echo "✓ 文件写入成功: {$testFile}\n";
  41. if (file_exists($testFile)) {
  42. echo "✓ 文件验证存在\n";
  43. @unlink($testFile);
  44. echo "✓ 测试文件已删除\n";
  45. }
  46. } else {
  47. echo "✗ 文件写入失败\n";
  48. echo "错误: " . error_get_last()['message'] . "\n";
  49. }
  50. // 测试5: 检查Laravel存储路径函数
  51. echo "\n=== 测试5: 检查Laravel存储路径函数 ===\n";
  52. require_once __DIR__ . '/vendor/autoload.php';
  53. try {
  54. $app = require_once __DIR__ . '/bootstrap/app.php';
  55. $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
  56. // 测试storage_path函数
  57. $storageAppPath = storage_path('app');
  58. $storagePublicPath = storage_path('app/public');
  59. $storageExamsPath = storage_path('app/public/exams');
  60. echo "storage_path('app'): {$storageAppPath}\n";
  61. echo "storage_path('app/public'): {$storagePublicPath}\n";
  62. echo "storage_path('app/public/exams'): {$storageExamsPath}\n\n";
  63. echo "=== 测试6: 使用Laravel函数创建目录 ===\n";
  64. if (!is_dir($storageExamsPath)) {
  65. echo "exams目录不存在,正在创建...\n";
  66. if (mkdir($storageExamsPath, 0755, true)) {
  67. echo "✓ Laravel路径下exams目录创建成功\n";
  68. echo "完整路径: {$storageExamsPath}\n";
  69. } else {
  70. echo "✗ Laravel路径下exams目录创建失败\n";
  71. }
  72. } else {
  73. echo "✓ Laravel路径下exams目录已存在\n";
  74. }
  75. // 测试文件写入
  76. $testFile2 = $storageExamsPath . '/laravel_test.txt';
  77. if (file_put_contents($testFile2, $testContent)) {
  78. echo "✓ Laravel路径下文件写入成功\n";
  79. @unlink($testFile2);
  80. echo "✓ 测试文件已删除\n";
  81. }
  82. } catch (Exception $e) {
  83. echo "✗ Laravel初始化失败: " . $e->getMessage() . "\n";
  84. echo "注意: 如果没有Laravel应用,这是正常的\n";
  85. }
  86. echo "\n=== 测试完成 ===\n";
  87. echo "如果所有测试都通过,说明目录创建功能正常\n";
  88. echo "如果exams目录创建失败,请检查:\n";
  89. echo "1. storage/app/public目录权限\n";
  90. echo "2. PHP进程是否有写入权限\n";
  91. echo "3. SELinux或其他安全策略\n";