首页 > 解决方案 > PHP - 提取文件的修改时间设置为服务器的时区 - 如何更改?

问题描述

因此,我们的代码创建了一个 zip 文件,其中包含来自特定文件夹的 PDF。一切正常,除了当这个 zip 文件下载到计算机并提取文件时 - 每个 PDF 文件的修改时间遵循服务器的时区。(我们的网站位于 GoDaddy 共享服务器上,它们位于亚利桑那州。我们位于新加坡。)

有没有办法修改这个?我试图设置默认时区或使用 touch() 但没有运气。

// Initialize archive object
$zip = new ZipArchive();
$res = $zip->open(plugin_dir_path( __FILE__ ).'MortgageWise-All-Reports.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

if ($res === TRUE) {
 // Create recursive directory iterator
 /** @var SplFileInfo[] $files */
 $files = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($rootPath)
 );

 foreach ($files as $name => $file) {
 // Skip directories (they would be added automatically)
  if (!$file->isDir()) {
   // Get real and relative path for current file
   $filePath = $file->getRealPath();
   $relativePath = substr($filePath, strlen($rootPath));

   date_default_timezone_set("Asia/Singapore");

   //touch($filePath, );

   // Add current file to archive
   $zip->addFile($filePath, $relativePath);
  }
 }

 // Zip archive will be created only after closing object
 $zip->close();
}
else {
 echo "File not created.";
}

标签: phptimezonetimestampzipextract

解决方案


我会先尝试在脚本开头设置时区,然后再创建 ZIP,而不是在 foreach 循环中。

但是话虽如此,我不相信它会因为 PDF 文件已经存在而产生很大的不同?因此已经将这些属性分配给了它们!

您可以访问 Web Host Manager 或类似的吗?如果您有 VPS 或专用服务器,您可以自己更改服务器的时区。

编辑:抱歉,您已经声明您在共享服务器上,强烈建议您使用 VPS 来完全控制这些设置,每月只需 50 美元


推荐阅读