dorisdong0514 2017-04-21 19:57
浏览 67
已采纳

php从一个dir重命名(移动)到另一个两个级别

my script file path is:

C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php

I need to run code from this path that will move images from path:

C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04

to:

C:\xampp\htdocs\wordpress\wp-content\uploads\images

My problem is that I have no idea how to force "rename" go two directories back in path.

I was trying something like:

$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');

error I'm getting atm (I've changed my folder permissions, so maybe it is something with path?):

Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied
. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16

edit rename code:

$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
$destPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/images');
/*$srcDir = opendir($srcPath);*/
echo $srcPath ;

sleep(1); 
rename($srcPath, $destPath);
  • 写回答

3条回答 默认 最新

  • douwen3500 2017-04-22 04:47
    关注

    I've finally after many hours.. find out how to fix it so here you go:

    $srcPath =  (realpath (dirname(__FILE__) . '\..\..' . '\uploads\2017\04') . '\obrazek2.png');
    $destPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\images') . '\obrazek2.png');
    
    rename ($srcPath , $destPath );
    

    The key was adding file name . '\obrazek2.png' after dirname (), because if filename is inside dirname () and it is destination path in rename, then it returns empty... :)

    Not sure if I'm clear enough, because of my English, but it works and hopes it will help someone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题