Really slow script, can it be written better

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

lovrboy
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Sep 12, 2011 11:07 am

Really slow script, can it be written better

Postby lovrboy » Mon Sep 12, 2011 11:14 am

Hi, I have made a script that creates a webpage from a list of external files and directories in a public dropbox folder. But it is really slow. Any tips on how to make it faster? I haven't implemented caching in this version, cause thats not the issue.

Code: Select all

$page = $_GET["page"];
<!DOCTYPE html> 
<html lang="en"> 
   <head>
      <meta charset="utf-8"/>
      <title><?php echo $page; ?></title>
      <link rel="stylesheet" type="text/css" href="reset.css" media="screen"/>
      <link rel="stylesheet" type="text/css" href="style.css" media="screen"/>
<?php

include_once "markdown.php";

$site_url = 'http://wpnetwork.net/hemmelig/dropbox/index.php';
$dropbox_url = 'http://dl.dropbox.com/u/36499225/dropbox-cms'; //The dropbox url daaaaah!

$lines = file( $dropbox_url.'/enfilutenlike.html', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //The file that contains the list of file and directorie paths in your dropbox
$image_ext = array('.png', '.jpg', '.jpeg');


?>
   
   
      
   </head>
   <body>



<div class="wrapper">

   <div id="header">
      
      
   </div>
<ul id="menu">
         <?php
         // Loop through our array, show HTML source as HTML source; and line numbers too.
         foreach ($lines as $key => $name) {
            $slash = strpos($name, '/');
             $colon = strpos($name, ':');
             $image = strpos($name, $image_ext);
            
            
             if ($colon !== false){ // Check if the string/path/whatever contains a slash
                $directory = htmlspecialchars(strrchr($name, '/'));
               echo "<li><a href='".$site_url."?page=".substr($directory,1,-1)."'>" . substr($directory,1,-1) . "</a></li>"; //Display the directory name withot slash
             }
            
          }
      
         ?>
      </ul>

      <div class="main_content">
      
      <?php
      // Loop through our array, show HTML source as HTML source; and line numbers too.
      foreach ($lines as $key => $name) {
         
         $slash = strpos($name, '/');
          $colon = strpos($name, ':');
          $image = strpos($name, $image_ext);
          $filepath = $dropbox_url."/".$page."/".$name;
       
          $arr = get_headers($filepath, 1);
          $headres = $arr[0];
          
          if ($colon !== false){
             $directory = htmlspecialchars(strrchr($name, '/'));
             $dir = substr($directory,1,-1); //Its a dir, save it for the next
          }
          elseif ($dir !== $page) {
             //Dir not same as page so its wrong url
          }
          else {
             
                 if (!preg_match('/(jpeg|jpg|png|gif)/', $name)){
                   echo "<div class='text'>".Markdown(file_get_contents($filepath))."</div>";
                }
                else{
                   echo "<div class='item'><!-- Start item -->";
                   echo "<img src='".$dropbox_url."/".$page."/".htmlspecialchars($name)."'/>"; //Display as an image (will display even if not)
                   echo "<p class='img_title'>".substr($name,0,-4)."</p>";
                   echo "</div> <!-- End item -->";
                }
             
          }
         
         
      }
      
      ?>
      </div>
   </div>
   </body>

</html>

Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest