replace apostrophes & ! in filename and save

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

replace apostrophes & ! in filename and save

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Sounds like a job for str_replace
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

take a look at str_replace() and rename()
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post by comedydave »

Actually do i need to use

Code: Select all

fopen
aswell?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

str_replace() and rename() take more arguments than that 8O
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I would use opendir() and readdir()
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if I get a min in the next little bit, I'll write up a snippet for you....
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post by comedydave »

Excellent, thanks mate
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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);
			}
	   }
	}
}
?>
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post by comedydave »

Thanks for that, much appreciated!

Can't get it to work though, all it seems to do is return

Code: Select all

. is good
.. is good
Would this code rename folders too when working?

Thanks!

Dave
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

$pattern = "/(!|-|')/";
just in case try :

$pattern = "#([\!\'])#";
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
Post Reply