dongliao4353
dongliao4353
2018-07-12 12:34

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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douluanzhao6689 douluanzhao6689 3年前

    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>
    
    点赞 评论 复制链接分享

相关推荐