Peuplarchie Posted May 10, 2009 Share Posted May 10, 2009 Good day to you all, here I'm trying to add to a listing directory code, another piece of code that send the list result to an html page. It's seems like the "sending to the other page code never get executed ? I always have this function proble can somebody so me my mistake ! Here is my code : <?php error_reporting(E_ALL); ini_set("display_errors", "on"); set_time_limit(0); $directory = "Art/"; function dirList ($directory) { //create 2 arrays - one for folders and one for files $folders = array(); $files = array(); // create a handler for the directory $handler = opendir($directory); // keep going until all files in directory have been read while (false !== ($file = readdir($handler))) { // if $file isn't this directory or its parent, // add it to the results array if ($file != '.' && $file != '..') // If file is directory, mark it in bold. if(is_dir($directory.$file)) { array_push($folders,$file); if (time() - filemtime($directory.$file) < 604800) { $folder_modified[] = "<span style=\"color:#DB1212;\"><li><div class=\"folder\"><img src=\"plus_icon.gif\" onclick=\"Toggle('".preg_replace('/\//','_',$directory."".$file)."'); return false;\" /><b>".$file."</b></div>"; } elseif (time() - filemtime($directory.$file) < 31556926) { $folder_modified[] = "<span style=\"color:#003366;\"><li><div class=\"folder\"><img src=\"plus_icon.gif\" onclick=\"Toggle('".preg_replace('/\//','_',$directory."".$file)."'); return false;\" /><b>".$file."</b></div>"; } else {$folder_modified[] = "<span style=\"color:#000000;\"><li><div class=\"folder\"><img src=\"plus_icon.gif\" onclick=\"Toggle('".preg_replace('/\//','_',$directory."".$file)."'); return false;\" /><b>".$file."</b></div>";} // Else not styled }else{ array_push($files,$file); $filenamecor = substr($file, 0, -4); if (time() - filemtime($directory.$file) < 604800) { $file_modified[] = '<span style="color:#DB1212;">'.$filenamecor.'<span>'; } elseif (time() - filemtime($directory.$file) < 31556926) { $file_modified[] = '<span style="color:#003366;">'.$filenamecor.'<span>'; } else {$file_modified[] = '<span style="color:#000000;">'.$filenamecor.'<span>';} } } $output = ""; $output .= "<ul id=\"".preg_replace('/\//','_',substr($directory,0,strlen($directory)-1))."\">\n"; //start a new unordered list for every iteration through dirList $dircor = $directory; // tidy up: close the handler closedir($handler); foreach($folders as $folder=>$file) { $output .= "<li><div class=\"folder\">".$folder_modified[$folder]."</div>"; //echo the folder name enclosed in a list item dirList($directory.$file.'/'); //loop through the contents of $folder $output .= "</li>\n"; //close this list item after all files and folders in $folder have been looped through } foreach($files as $key=>$file) { $output .= "<li><a href=\"index.html\" onclick=\"load('image_view.php?dir=".$dircor."&file=".$file."','boxdisp');return false;\"> ".$file_modified[$key]."</a></li>\n"; //echo the file name enclosed in a list item } $output .= "</ul>\n"; //close the unordered list return $output; $filename = 'menu.html'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle,$output) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote () to file (".$filename.")"; fclose($handle); } else { echo "The file ".$filename." is not writable"; } } dirList ($directory); ?> the result of this is a blank page. Thanks ! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 10, 2009 Share Posted May 10, 2009 I'm not sure if i follow. Are you having troubles writing to menu.html? Why are you doing this anyway? You'd be better off changing menu.html to a php file and simply echoing the information there. Quote Link to comment Share on other sites More sharing options...
Peuplarchie Posted May 10, 2009 Author Share Posted May 10, 2009 ok, how would you do it? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 11, 2009 Share Posted May 11, 2009 ok, how would you do it? Well i'm still not entirely sure what it is you're doing. Maybe if you explain that we can suggest a good way. Quote Link to comment Share on other sites More sharing options...
Peuplarchie Posted May 11, 2009 Author Share Posted May 11, 2009 I'm triyng to : 1. List recurcively a directory 2. add some html and css to the result 3. send the result to another page, so I don't have to read the list again. Thanks ! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Hello, Would you mind using better formatting? That code is a nightmare to read through with all the odd indentations. Thanks! Quote Link to comment Share on other sites More sharing options...
Peuplarchie Posted May 11, 2009 Author Share Posted May 11, 2009 I would love to, I'm learning so please show me ! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Allow me to introduce you to Google Quote Link to comment Share on other sites More sharing options...
gevans Posted May 11, 2009 Share Posted May 11, 2009 I haven't really got a huge idea what's going on, but I;ve goine through it and touched it up a little, and fixed a couple of mistakes.... Try running this and see what you get. <?php error_reporting(E_ALL); ini_set("display_errors", "on"); set_time_limit(0); $directory = "Art/"; function dirList($directory) { //create 2 arrays - one for folders and one for files $folders = array(); $files = array(); // create a handler for the directory $handler = opendir($directory); // keep going until all files in directory have been read while(false !== ($file = readdir($handler))) { // if $file isn't this directory or its parent, // add it to the results array if ($file != '.' && $file != '..') {} // If file is directory, mark it in bold. if(is_dir($directory.$file)) { array_push($folders,$file); if (time() - filemtime($directory.$file) < 604800) { $folder_modified[] = "<span style=\"color:#DB1212;\"><li><div class=\"folder\"><img src=\"plus_icon.gif\" onclick=\"Toggle('".preg_replace('/\//','_',$directory."".$file)."'); return false;\" /><b>".$file."</b></div>"; } elseif (time() - filemtime($directory.$file) < 31556926) { $folder_modified[] = "<span style=\"color:#003366;\"><li><div class=\"folder\"><img src=\"plus_icon.gif\" onclick=\"Toggle('".preg_replace('/\//','_',$directory."".$file)."'); return false;\" /><b>".$file."</b></div>"; } else { $folder_modified[] = "<span style=\"color:#000000;\"><li><div class=\"folder\"><img src=\"plus_icon.gif\" onclick=\"Toggle('".preg_replace('/\//','_',$directory."".$file)."'); return false;\" /><b>".$file."</b></div>"; } // Else not styled } else { array_push($files,$file); $filenamecor = substr($file, 0, -4); if (time() - filemtime($directory.$file) < 604800) { $file_modified[] = '<span style="color:#DB1212;">'.$filenamecor.'<span>'; } elseif (time() - filemtime($directory.$file) < 31556926) { $file_modified[] = '<span style="color:#003366;">'.$filenamecor.'<span>'; } else { $file_modified[] = '<span style="color:#000000;">'.$filenamecor.'<span>'; } } } $output = ""; $output .= "<ul id=\"".preg_replace('/\//','_',substr($directory,0,strlen($directory)-1))."\">\n"; //start a new unordered list for every iteration through dirList $dircor = $directory; // tidy up: close the handler closedir($handler); foreach($folders as $folder=>$file) { $output .= "<li><div class=\"folder\">".$folder_modified[$folder]."</div>"; //echo the folder name enclosed in a list item dirList($directory.$file.'/'); //loop through the contents of $folder $output .= "</li>\n"; //close this list item after all files and folders in $folder have been looped through } foreach($files as $key=>$file) { $output .= "<li><a href=\"index.html\" onclick=\"load('image_view.php?dir=".$dircor."&file=".$file."','boxdisp');return false;\"> ".$file_modified[$key]."</a></li>\n"; //echo the file name enclosed in a list item } $output .= "</ul>\n"; //close the unordered list $filename = 'menu.html'; // Let's make sure the file exists and is writable first. if(is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle,$output) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote () to file ($filename)"; fclose($handle); } else { echo "The file ".$filename." is not writable"; } return $output; } echo dirList ($directory); ?> Also, try and keep your code neater if you want people to help you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.