Saturday, April 25, 2009
Make a Dierctory Items with PHP
learn how to make directory items using object oriented programming (OOP)
first make directory" images" than make page "DirectoryItems.php" and copy the class below then save it but all in the same folder
class DirectoryItems {
/*author engineer sherif sakr ,
//email :xxsherif82@yahoo.com
//http://sherif-sakr.blogspot.com/
*/
//data members
private $dicrectory ;
private $replacechar ;
private $filearray=array();
//////////////////////////////////////////////////////////////////
//constructor
/////////////////////////////////////////////////////////////////
public function __construct($directory, $replacechar = "_"){
$this->directory = $directory;
$this->replacechar = $replacechar;
$d = "";
if(is_dir($directory)){
$d = opendir($directory) or die("Failed to open directory.");
while(false !== ($f = readdir($d))){
if(is_file("$directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}else{ //error die("Must pass in a directory.");
}
}
//////////////// public function
getCount(){
return count($this->filearray);
}
/////// public function
getFileArray(){
return $this->filearray;
}
///////////////////////////////
//private functions///////////////////////////////////////////////////////////////// private function createTitle($title){ //strip extension $title = substr($title, 0, strrpos($title, ".")); //replace word separator $title = str_replace($this->replacechar, " ", $title); return $title; }
/////////////////eliminate all elements from array except images ///////////////////
public function imagesOnly(){
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
unset($this->filearray[$key]);
}
}
}
}//end class
make page test.php and put the code below then save and run it
imagesOnly();//get portion of array
$filearray = $di->getFileArray();
$count=$di->getCount();
echo 'images count ' .$count ;
$imag_counter=0 ; //instant count per line
$imag_perline=5; // max image number per line
foreach ($filearray as $key => $value){
$imag_counter=$imag_counter+1;
$imag_path='images/'.$key;
echo 'ima tag with path';
if ($imag_counter==5) {$imag_counter=0 ; echo ' break tage here';}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment