PHP function to change directory name

guerilla

All we do is win
Aug 18, 2007
11,426
428
0
No
I need a function to call from an URL, preferably PHP, that will change the name of a directory.

I will use it to change the name of a directory to open. People will be able to access open.

I will also use the same function to change the directory name to closed to keep people from being able to load the files in that directory.

In this way, I can use cron to call the two name changes to allow and disallow access to a certain directory during certain times.

Any ideas?

TIA. + rep for useful help.
 


So this is what I have so far

PHP:
<?php
rename("/home/xxxxx/public_html/chatx", "/home/xxxxx/public_html/chatx--99");

echo "<h2>Chat is closed!</h2>";
?>
The problem is, if the directory is not already chatx (like if it is already chatx--99), it throws an error.

How can I implement some checking to see if the directory is already the change-to-name, before running?

TIA.
 
Do some thing like
if (file_exists("/home/xxxxx/public_html/chatx--99") = false)
{
rename..
}
else
{
whatever you want to do if the file already exists
}
 
  • Like
Reactions: guerilla
$dir = getcwd();
if(is_dir($dir."/open")) { rename($dir."/open", $dir."/closed"); }
else if (is_dir($dir."/closed")) { rename($dir."/closed", $dir."/open"); }
else { echo "fuck off"; }

sorry for any syntax errors but that should work
 
Ah, sorry. I was referring to what dably posted above. file_exists() will check for files, not for directories, so you'd have to use is_dir() instead of that.

What stumpy posted should work just fine though.

From php.net: file_exists — Checks whether a file or directory exists

I always assumed you might use is_dir after file_exists (ie. all directories are "files" but not all files are directories..?)

Anyways guerilla, why aren't you just doing this directly in your cron entry with a shell command? (Or are you wanting to be able to change the dir name from your site as well?)
 
Ah, sorry. I was referring to what dably posted above. file_exists() will check for files, not for directories, so you'd have to use is_dir() instead of that.

What stumpy posted should work just fine though.

I always used file_exists() for both, based on the php.net documentation. is_dir() should work as well though.