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

报告相同问题?

悬赏问题

  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果