Welcome to WebmasterWorld Guest from 54.147.44.13

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Can PHP randomly rename a file?

     
5:16 am on Mar 22, 2014 (gmt 0)

Junior Member

joined:Sept 30, 2012
posts: 51
votes: 0


Is it possible for PHP to randomly rename a file in a directory? I have seen a few examples of PHP code that rename files when using an upload script, but not quite what I am after.

For example say I have in a directory one php file and I want this php file to be renamed to a new random name every 30 minutes (using a cron job) could I use a PHP script to do this and let crontab execute it? Or would I better off just making a python script to do this, as it doesn't matter what programming langauge it is in as long as it can be executed by a cron job on a server.
10:52 am on Mar 22, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:943
votes: 0


Could probably just do this with bash, since you say you'll have the file by itself in a directory

ls /path/to/directory | xargs -I {} mv {} /path/to/directory/$RANDOM.php

To do this every 30 mins, add it to your crontab thusly:

30 * * * * ls /path/to/directory | xargs -I {} mv {} /path/to/directory/$RANDOM.php >/dev/null 2>&1
4:03 am on Mar 23, 2014 (gmt 0)

Junior Member

joined:Sept 30, 2012
posts: 51
votes: 0


WOW! Thank you so much Readie! I never even thought of doing this, I will try it out.
5:01 am on Mar 23, 2014 (gmt 0)

Junior Member

joined:Sept 30, 2012
posts: 51
votes: 0


It didn't work :-(
I CHMOD the access properties to 777 just to be sure that it wasn't a permissions error but it still did not rename the .php file in the directory.

I checked all my directory paths were correct as well.
Any ideas what I am doing wrong?

I added the cronjob via Cpanel.
12:49 pm on Mar 23, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:943
votes: 0


My bad sorry :)

ls /path/to/directory | xargs -I {} mv /path/to/directory/{} /path/to/directory/$RANDOM.php

Forgot to fully qualify one little bit :)

[php]\> mkdir test
[php]\> touch test/12345.php
[php]\> ls test | xargs -I {} mv test/{} test/$RANDOM.php
[php]\> ls test
25580.php
[php]\> ls test | xargs -I {} mv test/{} test/$RANDOM.php
[php]\> ls test
15566.php
[php]\>
8:41 am on Mar 24, 2014 (gmt 0)

Junior Member

joined:Sept 30, 2012
posts: 51
votes: 0


Thank you so much for this Readie, it works perfectly!
11:19 am on Apr 7, 2014 (gmt 0)

Junior Member

joined:Sept 30, 2012
posts: 51
votes: 0


One last question, can this also be applied to a folder? E.g. a folder can be randomnly renamed?
11:50 am on Apr 7, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:943
votes: 0


Yea, ls called like that makes no distinction. Just remove the .php from the mv command :)
12:26 pm on Apr 7, 2014 (gmt 0)

Junior Member

joined:Sept 30, 2012
posts: 51
votes: 0


Thank you again Readie, you have helped me so much :-)