1

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);
riten
  • 185
  • 1
  • 12

3 Answers3

0

It seems that you're in a Windows machine, so you're using the wrong type of slashes: /../..' . '/uploads/2017/04/obrazek.png'

You could try to replace them with backslashes:\..\..\uploads\2017\04\obrazek.png'

Or you could try something like this:

$path = substr($srcPath, 0, strrpos($srcPath, '\20'));

// $path now contaits: 'C:\xampp\htdocs\wordpress\wp-content\uploads';

// Then you can do:

$srcPath = $path . '\images';

Regarding the warning error:

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

It seems that you try to rename a file to directory, so probably you forgot to append the file name to the new path.

$srcPath contains a file. $destPath contains a directory, but it should contain the file name.

aletzo
  • 2,471
  • 1
  • 27
  • 31
0

From looking at it, it looks wrong

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

it should be

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

You have an extra / slash at the beginning.

unixmiah
  • 3,081
  • 1
  • 12
  • 26
  • 1
    I was even getting "Warning: rename(,): Invalid argument" after removing first slash :) – riten Apr 21 '17 at 20:18
0

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.

riten
  • 185
  • 1
  • 12