简体   繁体   English

PHP重命名问题!

[英]PHP Rename problem!

I have a problem using php rename function. 我在使用php重命名功能时遇到问题。 If I use the following syntax it works fine. 如果我使用以下语法,则可以正常工作。

<?php
rename("pages/file.php", "pages/xfile.php");
?>

but instead if I use this: 但是如果我使用这个:

<?php
$val = '"pages/file.php"';
$rval = '"pages/xfile.php"';
rename($val, $rval);
?>

It does not work and gives an error: 它不起作用并给出错误:

Warning: rename("pages/file.php","pages/xfile.php") [function.rename]: The system cannot find the path specified. (code: 123) in C:\wamp\www\page_rename.php on line 2

Remove a set of quotes from the path names. 从路径名中删除一组引号。

$val = "pages/file.php";
$rval = "pages/xfile.php";
rename($val, $rval);

You have too many quotation marks in the second version. 在第二个版本中,引号太多。 Use this: 用这个:

<?php
$val = 'pages/file.php';
$rval = 'pages/xfile.php'; 
rename($val, $rval);
?>

Use the following wrapper for rename (part of a PHP static Utility Class). 使用以下包装器进行重命名(PHP静态实用工具类的一部分)。 You can also use the wrapper to rename file with a kind of wildcharacters in the filename. 您也可以使用包装器来重命名文件名,文件名中带有通配符。

/**
* Renames files in a given directory with a form of wildchars.
* @param  string $dir The directory.
* @param  string $src Name of the source to be changed. (i.e. XX)
* @param  string $dst Name of the result                (i.e. YZ)
* @return null | true
* @since 1.0.6.2
*/
public static function rename($dir, $src, $dst) {
  // First check if it is a directory.
  if (! is_dir( $dir ) ) {
     return null;
  }
  // Renames with some kind of wildchars
  if ($handle = opendir($dir) ) {
     while (false !== ( $fileName = readdir($handle) ) ) {
        // Skips the files
        if ($fileName == "." || $fileName == ".." || $fileName == ".DS_Store" ) {
           continue;
        }
        $newName = str_replace($src, $dst, $fileName);
        rename( sprintf("%s/%s", $dir, $fileName),
                sprintf("%s/%s", $dir, $newName ));
     }
     closedir($handle);
  }
  return true;
}

尝试取出双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM