Jump to content

[SOLVED] Directories


cheechm

Recommended Posts

Hi,

Got this at the moment:

  if (!$action)
        {
            // directory path can be either absolute or relative
            $dirPath = '/home/niick/public_html/qw/forum/language/en/mods/';

            // open the specified directory and check if it's opened successfully
            if ($handle = opendir($dirPath))
            {

                // keep reading the directory entries 'til the end
                while (false !== ($file = readdir($handle)))
                {

                    // just skip the reference to current and parent directory
                    if ($file != "." && $file != "..")
                    {
                        if (is_dir("$dirPath/$file"))
                        {
                            $template->assign_vars(array('S_DIR' => true));
                            $template->assign_block_vars('language', array('DIR' => $file));
                            // found a directory, do something with it?

                        } else
                        {
                            // found an ordinary file
                            $template->assign_block_vars('language', array('FILE' => $file));

                        }
                    }
                }

                // Close what we opened
                closedir($handle);
            }

        }

 

I want to remove the extension (ie .php / .html) when the files are listed. Is this possible?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/101761-solved-directories/
Share on other sites

If you have file names that contain "."  also, you can explode the strings with "." and count the number of values in the array. Remove the last index and implode the remaining indexes with ".".

 

Could you give me some code for that? Just like a starting code or something.

Link to comment
https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520654
Share on other sites

I apologize, implode will not work, but here's some code and a little loop to do what I had meant by using implode().

 

<?php
$file = 'index.php';
$file2 = 'index.file.php';

$filearray = explode('.', $file);
$filearray2 = explode('.', $file2);

$filearray[(sizeof($filearray)-1)] = "";
$filearray2[(sizeof($filearray2)-1)] = "";

$file = "";
for ($x = 0; $x < (sizeof($filearray)-1); $x++) {
$file .= $filearray[$x];
if ($x !== (sizeof($filearray)-2))
$file .= '.';
}

$file2 = "";
for ($x = 0; $x < (sizeof($filearray2)-1); $x++) {
$file2 .= $filearray2[$x];
if ($x !== (sizeof($filearray2)-2))
$file2 .= '.';
}

echo $file . "\n"; // Will output "index"
echo $file2;       // Will output "index.file"
?>

Link to comment
https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520662
Share on other sites

<?php

function remove_ext ( $filename ) {

$arr = explode('.', $filename);
if ( count($arr) === 1 )
	return $filename;
else
	return substr( $filename, 0, (strlen( end($arr) ) + 1) * -1 );

}

$file = 'this.is.a.test.jpg';

echo remove_ext($file);

?>

 

Hope that helps

 

[edit] using pathinfo() is probably best here [/edit]

Link to comment
https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520671
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.