Jump to content

How to exclude file types?


Morrison

Recommended Posts

Hi folks, I'm glad to be here.  I just started with PHP tonight...I wrote this code that works:

 

<?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
           
	  $string = "$file";	 
	  

$searchArray = array("_", ".html");
$replaceArray = array(" ", " ");
$string = str_replace($searchArray,$replaceArray,$string);

echo "<a href=$file>$string\n</a><br/>"; 
        }
    }
    closedir($handle);
}

?>

 

It looks in a directory, notes what files/folders are in it, then renames them and outputs the results in a list as links to the files.  But I want to exclude all files that do not have a .html extension.  Can anyone help please?  I'm really stuck.  Thanks :)

Link to comment
Share on other sites

You can easily grab the extension of a file by using this simple snippet:

 

$extension = array_pop(explode(".", $file));

 

That will return everything after the last period. So, in the case of a filename like: index.html

It will return "html".

 

Then you can just check to see of the extension is equal to html.

 

 if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "html") {

Link to comment
Share on other sites

Thank you ProjectFear :)  I'll try that last bit of code when I'm more awake.  You have been a great help.  I am confused tho as to where exactly I put it in, all of this in new to me.  I've always stuck to CSS, flash, and html.  I'm just now getting to PHP and javascript.  Here is my code now:

 

 <?php
$extension = array_pop(explode(".", $file));
if ($handle = opendir('music_pages')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "php") {           
	  $string = "$file";
$searchArray = array("_", ".php");
$replaceArray = array(" ", "");
$string = str_replace($searchArray,$replaceArray,$string);
echo "<a href=music_pages/$file>$string\n</a><br/>"; 
        }
    }
    closedir($handle);
}
?>

 

My head hurts just looking at it, lol.

Link to comment
Share on other sites

You can easily grab the extension of a file by using this simple snippet:

 

$extension = array_pop(explode(".", $file));

 

Or you can use the built in function made to do this, pathinfo.

 

echo pathinfo('example.jpg', PATHINFO_EXTENSION);

Link to comment
Share on other sites

Hey!  I've been really busy, just now have time to work on this project.  I decided to take my thing to the next level, incorporate a database and automatic page generation.  This is the scenario:

 

I have a directory, it has several folders in it, each folder is called "Some_Band_-_Album_Name" and contains all of the songs in that album.  Then I have a page with a flash player that opens that folder and plays the song.  The way you get to this page is thru a list that is generated automatically from scanning a directory and returning folder names as links to the particular player page.  This code makes the page, thanks to ProjectFear and others :)

 

$dirFiles = array();
// opens music folder
if ($handle = opendir('music_pages')) {
    while (false !== ($file = readdir($handle))) {
    
        // strips files extensions      
        $crap   = array("php");    

        $newstring = str_replace($crap, " ", $file );   

          if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "php") {
                $dirFiles[] = $file;
        }
    }
    closedir($handle);
}

sort($dirFiles);
foreach($dirFiles as $file)

{
$string="$file";
$searchArray = array("_", ".php");
$replaceArray = array(" ", "");
$string = str_replace($searchArray,$replaceArray,$string);
    echo "<li><a href=\"music_pages/$file\">$string</a></li>\n";
}

 

Each player page calls from a folder with this line:

 

flashvars.songInfo = "musik/Arvo_Part_-_de_Profundis";

 

But now I don't even want to make the player page, I want it echoed out automatically by info in the database.  I want a script that:

 

1.goes to a directory (musik), gets folder names

2.insert these folder names into database with values equal to the names

3.now I will echo out the new page's html code, inserting the needed value into the flashvar.

4.And then print out a list of the folders as links.

 

Any help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.