使用 PHP 重命名文件


Renaming file with PHP

我正在使用我在这里找到的代码,它工作得很好,但是当我尝试访问子目录中的文件时,它只是不想工作。

它获取一个文件,创建一个要写入的临时文件,然后在文件中查找一些文本并用新文本替换该文本,然后保存更新的文件,然后删除临时文件。

以下工作正常:

        $reading = fopen('links.htm', 'r');
        $writing = fopen('links.tmp', 'w+');
        $replaced = false;
        while (!feof($reading)) {
          $line = fgets($reading);
          if (stristr($line,'::template::')) {
            $line = "replacement line!'n";
            $replaced = true;
          }
          fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced) 
        {
          rename('links.tmp', 'links.htm');
        } else {
          unlink('links.tmp');
        }

这不起作用:

        $reading = fopen('path/to/links.htm', 'r');
        $writing = fopen('path/to/links.tmp', 'w+');
        $replaced = false;
        while (!feof($reading)) {
          $line = fgets($reading);
          if (stristr($line,'::template::')) {
            $line = "replacement line!'n";
            $replaced = true;
          }
          fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced) 
        {
          rename('path/to/links.tmp', 'path/to/links.htm');
        } else {
          unlink('path/to/links.tmp');
        }

有什么建议吗?

path是绝对的,我之前在代码中定义了它,并使用它来创建文件和写入文件,但是当我希望上面的代码以相同的方式工作时,它只是不想这样做。

此外,文件夹权限已设置为写入和读取,这也工作正常。

在子文件夹中运行代码并且工作正常,但不能从顶级目录运行。

错误报告已关闭,现在打开:

该进程无法访问该文件,因为它正由另一个进程使用。(代码:32)

因此,经过一周的环顾四周,测试,破坏和重新编码,我找到了一种简单的方法来做到这一点。

由于大多数内容以及链接.htm文件都是动态创建的,因此很难找到访问该文件的位置和时间,因为代码访问了大约 300 个客户端站点并更新了链接文件。

简单修复:

$path = "path/on/server/";
$conn_id = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn_id,"admin","ert456");
$old_file = $path.'links.htm';
$new_file = $path.'_template.htm';
ftp_rename($conn_id, $old_file, $new_file);
$source = fopen($new_file, "w");
$localTarget = "path/to/links.htm";
ftp_fget($conn,$source,$localTarget,FTP_ASCII);
    $reading = fopen('path/to/links.htm', 'r');
    $writing = fopen('path/to/_template.htm', 'w+');
    $replaced = false;
    while (!feof($reading)) {
      $line = fgets($reading);
      if (stristr($line,'::template::')) {
        $line = "replacement line!'n";
        $replaced = true;
      }
      fputs($writing, $line);
    }
    fclose($reading); fclose($writing);
    // might as well not overwrite the file if we didn't replace anything
    if ($replaced) 
    {
      rename('path/to/_template.htm', 'path/to/links.htm');
    } else {
      unlink('path/to/_template.htm');
    }
 $local_file = "path/to/links.htm";
 ftp_put($conn_id, $path, $local_file, FTP_BINARY);
 ftp_close($conn_id);

可能有更简单的方法可以做到这一点,但这对我有用,希望它对某人有所帮助。

PHP 需要对该目录具有写入权限才能写入该目录。 可能是它对当前目录.有写入权限,但没有写入子目录./path/to/的权限。

编辑:如果您收到PHP错误,则应将其包含在您的问题中。

OP 编辑后编辑:

该错误意味着当前已打开links.htm内容。 我看到您在重命名文件之前fclose()文件,所以我的猜测是您可能在其他应用程序(例如浏览器或文本编辑器)中打开了links.htm

编辑#3:

如果您没有在另一个应用程序中打开links.htmlinks.tmp文件之一,则可能是您正在使用 Windows - 在这种情况下,rename()调用将在fclose()之前执行,即使它在代码中位于它之后。 解决方案是在关闭句柄后添加sleep()调用:

fclose($reading); fclose($writing);
sleep(1); // this should allow the handle to be properly closed before the rename() call