homepage Welcome to WebmasterWorld Guest from 23.20.220.79
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Can PHP randomly rename a file?
HaloPlayer




msg:4656165
 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




msg:4656199
 10:52 am on Mar 22, 2014 (gmt 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

HaloPlayer




msg:4656382
 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




msg:4656388
 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




msg:4656444
 12:49 pm on Mar 23, 2014 (gmt 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]\>

HaloPlayer




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

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

HaloPlayer




msg:4661069
 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




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

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

HaloPlayer




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved