dongliao4353
dongliao4353
2018-07-12 12:34
浏览 118
已采纳

PHP rename()函数:Literal Strings和。 变量

I'm currently trying to have my website automatically move a file from the root directory where it is created into another folder to help with organization. I'm using the PHP rename() function and I can get it to work with literal strings but when I try using the variable name I get the error

PHP Warning: rename() expects parameter 1 to be a valid path, resource given in...

My code works when I use,

$txt = "Additional Comments: ".$_POST['additionalComments']."
";
fwrite($myfile, $txt);      
fclose($myfile);
rename('Leadership_Review2.txt', 'Leadership_Review/Leadership_Review2.txt');

However, when I use

$txt = "Additional Comments: ".$_POST['additionalComments']."
";
fwrite($myfile, $txt);      
fclose($myfile);
$mydir = "Leadership_Review/";
rename($myfile, $mydir.$myfile);

I get the error mentioned above. It would be a lot more useful if I could use the variable name instead of having the literal string. Any help understanding rename or where I am going wrong would be greatly appreciated. Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试让我的网站自动将文件从创建它的根目录移动到另一个文件夹 帮助组织。 我正在使用PHP rename()函数,我可以使用文字字符串,但是当我尝试使用变量名时我得到错误

PHP警告 :rename()期望参数1是一个有效的路径,在...中给出 资源

我的代码在我使用时起作用, \ n

  $ txt =“Additional Comments:”。$ _ POST ['additionalComments']。“
 
”; 
 nfwrite($ myfile,$ txt);  
 nclclose($ myfile); 
rename('Leadership_Review2.txt','Leadership_Review / Leadership_Review2.txt'); 
   
 
 

但是,当我使用

  $ txt =“Additional Comments:”。$ _ POST ['additionalComments']。“
 
”; 
 nfwrite($ myfile,$ txt);  
 nclclose($ myfile); 
 $ mydir =“Leadership_Review /”; 
rename($ myfile,$ mydir。$ myfile); 
   
 
 

我得到了 上面提到的错误。 如果我可以使用变量名而不是文字字符串,那将会更有用。 任何帮助理解重命名或我出错的地方将不胜感激。 谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douluanzhao6689
    douluanzhao6689 2018-07-12 12:56
    已采纳

    PHP rename(oldpath,newpath) need full relative path of both "oldpath" and "newpath". For Example,

    <?php 
    $oldpath = "/public_html/images";
    $newpath = "/public_html/pictures";
    
    rename($oldpath,$newpath);
    ?>

    </div>
    
    点赞 评论

相关推荐