简体   繁体   English

使用php重命名功能的php文件重命名错误

[英]php file rename error using php rename function

<?php
    while($data = mysqli_fetch_array($result))
    { 
        $q1="insert into delinked values('".$data[0]."','".$data[1]."','".$data[2]."');"; //inserts the all the data into delinked 
        //$result1=mysqli_query($conn,$q1)or die("error");//executes query
        $a=basename($data[2]).PHP_EOL;//gets the filename
        $change="D".$a; // changes the file in (D_filename format)
        $tar="/DATA".$change;

        chdir('E:\DATA');
        rename($a,$change);
    }
?>

When I try to rename the file I get the following error:当我尝试重命名文件时,出现以下错误:

****Warning: rename(E:/DATA/windows.rar ,E:/DATA/er.php): The filename, directory name, or volume label syntax is incorrect. ****警告:重命名(E:/DATA/windows.rar ,E:/DATA/er.php): 文件名、目录名或卷标语法不正确。 (code: 123) in C:\\Users\\yathi\\Desktop\\Cilicosys Project\\deactivate.php on line 27**** (代码:123)在 C:\\Users\\yathi\\Desktop\\Cilicosys Project\\deactivate.php 第 27 行****

For some reason, you are adding a "newline" character ( PHP_EOL ) to the $a variable (which is then propagated to $change ).出于某种原因,您PHP_EOL $a变量添加一个“换行符”( PHP_EOL )(然后将其传播到$change )。 This causes rename to fail because this is not an allowed character in a filename.这会导致rename失败,因为这不是文件名中允许的字符。

The basic failing can be reproduced with基本的失败可以用

$a = "test.tst".PHP_EOL;                  
$targ = "moved.tst".PHP_EOL;                      
rename($a, $targ);
/*
Outputs : Warning. No such file or directory
*/

To fix your issue, just correct the line :要解决您的问题,只需更正该行:

$a=basename($data[2]); //gets the filename

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

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