【问题标题】:Unable to rename file using PHP无法使用 PHP 重命名文件
【发布时间】:2021-04-07 17:54:25
【问题描述】:

你写了简单的代码来重命名我在服务器上的文件,但总是出现如下错误

rename(/public_html/mystore/images,/public_html/mystore/images/test.jpeg): No such file or directory in /home/innovero/public_html/mystore/move.php on line 18

下面是重命名的PHP代码,

<?php
   

/* Store the path of source file */

$filePath = '/public_html/mystore/test.jpeg';
      
/* Store the path of destination file */

$destinationFilePath = '/public_html/mystore/images/test.jpeg';
    

/* Copy File from images to copyImages folder */
    if( !rename($filePath, $destinationFilePath) ) {  

    echo "File can't be moved!";  

}  

else {  

    echo "File has been moved!";  

} 

  

?>

我的代码有什么问题,我的 move.php 位于 mystore 文件夹中,我想将文件从 mystore 移动到作为 mystore 子文件夹的 images 文件夹。请帮忙

【问题讨论】:

  • 前导斜杠表示文件存储在文件系统的根目录,与当前脚本无关
  • 你能建议什么是正确的路径写入方式。
  • 更新了错误,将 / 替换为 ./ 作为重命名(./public_html/mystore/images,./public_html/mystore/images/test.jpeg):/home/innovero 中没有这样的文件或目录/public_html/mystore/move.php 第 18 行
  • 然后使用完整路径:/home/innovero/public_html/...
  • 您到底想在此处复制/移动什么文件?来自$filePath = '/public_html/mystore/images';public_html/mystore/images 是一个文件吗?

标签: php file rename move


【解决方案1】:

似乎您正在尝试重命名目录而不是文件

试试这个代码:

<?php
  $filePath = '/public_html/mystore/images/test.jpeg';    
  $destinationFilePath = '/public_html/mystore/images/newtest.jpeg';

  if(rename($filePath, $destinationFilePath) ) {  
      echo "File has been moved!";  
  }else {
      echo "Unable to move file";  
}

?>

【讨论】:

  • 你会仔细检查文件吗?
  • 需要这样的完整地址 /home/innovero/public_html/mystore/test.jpeg
【解决方案2】:

终于开始使用下面的代码了

<?php
if(file_exists('/home/innovero/public_html/mystore/test.jpg')){
   
    rename('/home/innovero/public_html/mystore/test.jpg','/home/innovero/public_html/mystore/images/test.jpg');
}else{
    echo "No File";
}

【讨论】:

    【解决方案3】:

    您将目录复制到文件 将 /public_html/mystore/images 复制到 /public_html/mystore/images/test.jpeg

    试试

    rename(/public_html/mystore/images/test-old.jpg, /public_html/mystore/images/test-new.jpeg)
    

    【讨论】:

      猜你喜欢
      • 2020-04-05
      • 2015-03-12
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      相关资源
      最近更新 更多