TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-04-2009, 04:00 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application 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..
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 05-04-2009, 05:25 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Try this in your script (at the top of it, after the php opening tag):

PHP Code:
ini_set('max_execution_time''180'); 
Keep in mind though, that not all servers allow overwriting ini settings at runtime, so you might want to var_dump the results of the call above and see if that returns true.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 05-04-2009, 05:42 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Try this in your script (at the top of it, after the php opening tag):

PHP Code:
ini_set('max_execution_time''180'); 
Keep in mind though, that not all servers allow overwriting ini settings at runtime, so you might want to var_dump the results of the call above and see if that returns true.
Yeah also, if he's exceeding the 30 seconds, maybe there's a loop somewhere that's unnecessary....
allworknoplay is offline  
Reply With Quote
Old 05-04-2009, 10:21 PM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

You can use the set_time_limit() function to remove the timeout:

PHP Code:
set_time_limit(0); 
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 05-05-2009, 01:25 AM   #5 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Try this in your script (at the top of it, after the php opening tag):

PHP Code:
ini_set('max_execution_time''180'); 
Keep in mind though, that not all servers allow overwriting ini settings at runtime, so you might want to var_dump the results of the call above and see if that returns true.

Won't/Should you set the value back to 30 before the php end tag?
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 05-05-2009, 03:27 AM   #6 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Quote:
Originally Posted by adamdecaf View Post
Won't/Should you set the value back to 30 before the php end tag?
No because all ini_set(), ini set aliases like set_time_limit etc. all revert back to their original stage afterwards, and it will only change for the current request being processed.
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Need more ideas for date and time class Wildhoney General 2 01-18-2008 02:22 PM
Two Different Ways to Format the Time CMellor Tips & Tricks 0 09-24-2007 11:12 PM
Script Execution Time Class Wildhoney Script Giveaway 2 09-14-2007 11:36 PM
Calculating the Execution Time Wildhoney Tips & Tricks 0 09-14-2007 11:14 PM


All times are GMT. The time now is 10:30 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design