用PHP重命名文件

时间:2012-04-20 12:41:40

标签: php replace file-handling

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

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

以下工作正常:

        $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是绝对的,我之前在代码中定义它并使用它来创建文件和写文件,但是当我希望上面的代码以它不想要的相同方式工作时。 / p>

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

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

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

  
    

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

  

2 个答案:

答案 0 :(得分:2)

因此,经过一周的环顾,测试,破解和重新编码后,我发现了一种简单的方法。

由于大多数内容都是动态创建的以及links.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);

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

答案 1 :(得分:1)

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
相关问题