Peuplarchie Posted May 4, 2009 Share Posted May 4, 2009 Good day to you, here i'm listing files and send the result into a html file. My problem, the result don,t seems to be written in the html file. Don't give me any error, just a black page. here is my code : <?php set_time_limit(90); $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>';} } } echo "<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) { echo "<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 echo "</li>\n"; //close this list item after all files and folders in $folder have been looped through } foreach($files as $key=>$file) { echo "<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 } echo "</ul>\n"; //close the unordered list } $filess=fopen(”menu.txt”,”w”) or die(”myfile.txt does not exist!”); fwrite($filess, dirList($directory)); fclose($filess); ?> Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/ Share on other sites More sharing options...
premiso Posted May 4, 2009 Share Posted May 4, 2009 Add this to the top of your script right after <?php error_reporting(E_ALL); ini_set("display_errors", "on"); And see if an error shows up. If not chances are you have an infinite loop. If so post the errors here. Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825934 Share on other sites More sharing options...
Peuplarchie Posted May 4, 2009 Author Share Posted May 4, 2009 Parse error: syntax error, unexpected T_STRING in Photos_gallery\update_menu.php on line 80 Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825943 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Parse error: syntax error, unexpected T_STRING in Photos_gallery\update_menu.php on line 80 What's line 80? Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825947 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 You should be using " and not ” on the last few lines of your script Also nothing will be written to menu.txt as you're using echo within your function which will cause it to not return anything. Instead of doing echo 'whatever'; you should do $output .= 'whatever'; Then at the last line of your function place the following line return $output; Now your function will return something which will get written to your file. Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825953 Share on other sites More sharing options...
Peuplarchie Posted May 4, 2009 Author Share Posted May 4, 2009 It don't work for me, error on line 58, first line where I replaced echo. <?php error_reporting(E_ALL); ini_set("display_errors", "on"); set_time_limit(90); $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 $filess=fopen("menu.txt","w") or die("myfile.txt does not exist!"); fwrite($filess, $output); fclose($filess); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825969 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Syntax error near the bottom. return $output; See the semi-colon? But why are you returning it? You're not in a function. Just echo. Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825974 Share on other sites More sharing options...
Peuplarchie Posted May 4, 2009 Author Share Posted May 4, 2009 Notice: Undefined variable: output in Photos_gallery\update_menu.php on line 89 Success, wrote () to file (menu.html) <?php error_reporting(E_ALL); ini_set("display_errors", "on"); set_time_limit(90); $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; } dirList ($directory); $filename = 'menu.html'; $somecontent = $output; // 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, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156817-send-function-result-to-html-page/#findComment-825979 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.