Welcome to WebmasterWorld Guest from 54.221.119.45

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Can PHP randomly rename a file?

     

HaloPlayer

5:16 am on Mar 22, 2014 (gmt 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.

Readie

10:52 am on Mar 22, 2014 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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

HaloPlayer

4:03 am on Mar 23, 2014 (gmt 0)



WOW! Thank you so much Readie! I never even thought of doing this, I will try it out.

HaloPlayer

5:01 am on Mar 23, 2014 (gmt 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.

Readie

12:49 pm on Mar 23, 2014 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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]\>

HaloPlayer

8:41 am on Mar 24, 2014 (gmt 0)



Thank you so much for this Readie, it works perfectly!

HaloPlayer

11:19 am on Apr 7, 2014 (gmt 0)



One last question, can this also be applied to a folder? E.g. a folder can be randomnly renamed?

Readie

11:50 am on Apr 7, 2014 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Yea, ls called like that makes no distinction. Just remove the .php from the mv command :)

HaloPlayer

12:26 pm on Apr 7, 2014 (gmt 0)



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

Featured Threads

Hot Threads This Week

Hot Threads This Month