replace apostrophes & ! in filename and save
Moderator: General Moderators
-
comedydave
- Forum Newbie
- Posts: 16
- Joined: Thu Mar 09, 2006 8:51 am
replace apostrophes & ! in filename and save
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
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
Sounds like a job for str_replace
take a look at str_replace() and rename()
-
comedydave
- Forum Newbie
- Posts: 16
- Joined: Thu Mar 09, 2006 8:51 am
I'm not too sure how to do it, i've tried
Can't seem to get it to work!
Any ideas?
Thanks!
Dave
Code: Select all
$chars = array("'","!","-");
foreach ($files as $file){
if (eregi("$chars", $file)){
$file = str_replace("$chars");
rename ("$file");
}Any ideas?
Thanks!
Dave
-
comedydave
- Forum Newbie
- Posts: 16
- Joined: Thu Mar 09, 2006 8:51 am
Actually do i need to use aswell?
Code: Select all
fopen-
comedydave
- Forum Newbie
- Posts: 16
- Joined: Thu Mar 09, 2006 8:51 am
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
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
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
Can't get it to work though, all it seems to do is return
Code: Select all
. is good
.. is goodThanks!
Dave
-
comedydave
- Forum Newbie
- Posts: 16
- Joined: Thu Mar 09, 2006 8:51 am
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?
Thanks again,
Dave
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);Dave