SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post 30 maximum execution time!!!!

    Good day to you all,
    I have a problem of maximum execution time over an directory listing.

    1. Is there a way to do this faster ?
    2. Or is it a setting on my server ? isn't the default 30 seconds.
    3. I'm working with a lot of files !
    4. Is there a better way of doing this?

    Error :
    Fatal error: Maximum execution time of 30 seconds exceeded in Photos_gallery\update_menu.php on line 27


    Code :
    PHP Code:




    <?php
     
    $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($file0, -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;\">&nbsp;".$file_modified[$key]."</a></li>\n"//echo the file name enclosed in a list item
        
    }
     
     
        echo 
    "</ul>\n"//close the unordered list
    }
     



    $filename 'menu.html';
    $somecontent dirList($directory);

    // 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";
    }
    What I'm trying to do is list a directory recurcively and write the result in a html page.
    So it faster to load for user

    to get it content later from the photos gallery page the user will only see..

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    set_time_limit(0) removes the time limit. Of course if there's a problem that results in an infinite loop, your script will never stop running.

    When working with a similar script, I've found that echoing the success message with the full HTML nested list adds a lot of overhead. By comparison, just writing it to the file happens almost instantly.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    you could take a look at the DirectoryIterator - even if you arent using PHP5 there is some code at the top of the page that might help.

    NB This search throws up a PHP4 directory iterator class too;
    http://www.google.co.uk/search?q=directory+iterator

    You can change the max execution time using;

    ini_set() at the top of the script.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not a good way to do if you have large number of directory/files structure.

    You can create a cron job and duplicate dir/file structure into database and then use database to locate dir/file

    Or if you dont like to put the structure in database, read the structure in to 2-D array using cron job and serialize it and save it as a flat data file. Then use that file to location dir/files.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •