Page 1 of 1
replace apostrophes & ! in filename and save
Posted: Thu Mar 09, 2006 10:47 am
by comedydave
Hi guys,
I have a bit of code that displays all folders and files in a folder, but i need it to rename the folders without a ' or !
before it writes the folder name or filename it's stored in $file, so i'm thinking i need to get the filename out of $file, rename it, and then write it back before writing it to the page.
Not sure if this is possible but it might be!
Thanks alot,
Dave
Posted: Thu Mar 09, 2006 11:00 am
by neophyte
Sounds like a job for
str_replace
Posted: Thu Mar 09, 2006 11:02 am
by Burrito
Posted: Thu Mar 09, 2006 11:15 am
by comedydave
I'm not too sure how to do it, i've tried
Code: Select all
$chars = array("'","!","-");
foreach ($files as $file){
if (eregi("$chars", $file)){
$file = str_replace("$chars");
rename ("$file");
}
Can't seem to get it to work!
Any ideas?
Thanks!
Dave
Posted: Thu Mar 09, 2006 11:16 am
by comedydave
Actually do i need to use
aswell?
Posted: Thu Mar 09, 2006 11:16 am
by Burrito
str_replace() and rename() take more arguments than that

Posted: Thu Mar 09, 2006 11:19 am
by comedydave
yeh i figured that, thats why i asked!
Any ideas on what the code goes like?
I'm relatively new to PHP so i apologise for any stupidity!
Thanks,
Dave
Posted: Thu Mar 09, 2006 11:19 am
by Burrito
Posted: Thu Mar 09, 2006 11:21 am
by Burrito
if I get a min in the next little bit, I'll write up a snippet for you....
Posted: Thu Mar 09, 2006 11:22 am
by comedydave
Excellent, thanks mate
Posted: Thu Mar 09, 2006 11:51 am
by Burrito
you could use str_replace() as we discussed, but I think for the sake of less code, preg_replace will be better.
try this out:
untested
Code: Select all
<?
$dir = "c:/inetpub/wwwroot/somefolder/";
if ($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
{
if(is_dir($dir.$file))
{
$pattern = "/(!|-|')/";
preg_match($pattern,$file,$matches);
if(!isset($matches[1]))
echo "$file is good<br>";
else
{
$fold = $file;
$file = preg_replace($pattern,"",$file);
echo "renamed $fold to $file<br>";
rename($dir.$fold,$dir.$file);
}
}
}
}
?>
Posted: Fri Mar 10, 2006 3:43 am
by comedydave
Thanks for that, much appreciated!
Can't get it to work though, all it seems to do is return
Would this code rename folders too when working?
Thanks!
Dave
Posted: Fri Mar 10, 2006 5:32 am
by newmember
$pattern = "/(!|-|')/";
just in case try :
$pattern = "#([\!\'])#";
Posted: Fri Mar 10, 2006 8:16 am
by comedydave
Brilliant - i've got it working, any ideas on how to get it to work in the following search code so it will rename folders and files as it goes, and then return the file path as it does now?
Code: Select all
function directoryTraverse($directory, $searchSubdirectories = false)
{
$searchstring = $_REQUEST['searchstring'];
if ($searchstring == ""){
print "Please enter some text to search for!";
return;
}
$directories = array($directory);
$files = array();
for($dir = 0; $dir < count($directories); $dir++) {
$data = glob($directories[$dir] . DIRECTORY_SEPARATOR . '*');
set_time_limit(60);
foreach($data as $file) {
if(is_file($file)) {
$ext = explode('.', $file);
$extension = $ext[count($ext)-1];
if (($extension == "mp3")&&(eregi("$searchstring", $file))) {
$files[] = $file;
print"<table width='330'><tr><td><img src='images/music_icon.gif' alt='$file' border='0'><img src='images/spacer.gif' border='0' width='5' ><a href='playmedia.php?fileid=$file&download=yes' target='player' style='font-family: Arial, Helvetica, sans-serif; font-size:10px; color:001F63'>$file</a><br><tr><td></table>";
}
} elseif(is_dir($file)) {
$directories[] = $file;
}
}
if(!$searchSubdirectories) {
break;
}
} if(count($files) == 0) {
print"No MP3's found matching "$searchstring"<br><br>Please try again...";
}
return array($directories,$files);
}
list($dirs, $files) = directoryTraverse('media', true);
Thanks again,
Dave