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

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 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?