2017-12-30 8 views
-1

Wie kann ich die Dateien in meinem Ordner durch Klicken auf Next Schaltfläche und Previous Schaltfläche dann den Dateiinhalt in einer Tabellenzelle anzeigen?Wie können Sie Dateien durchsuchen und ihren Inhalt anzeigen, indem Sie auf Schaltflächen in PHP klicken?

Hier ist meine Beispielausgabe, aber leider seine Arbeit nicht zu mir:

Image Output

Hier ist der Code, das ich verwenden:

<!DOCTYPE html> 
<html> 
<head> 
<title>Browse Files</title> 
</head> 
<body> 
<?php 
$dir = '/xampp/htdocs/files'; 
$file = array_diff(scandir($dir), array('.','..')); 

function showContent($fdata) { 
    $fopen = fopen($fdata,'r') or die ("Could not open file"); 
    $content = fread($fopen,filesize($fdata)) or die("Could not read the file"); 
    echo $content; 
    fclose($fopen); 
} 
    ?> 
<table border = "1" cellpadding = "10" align = "center" width = "500"> 
    <frame method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>"> 
     <tr> 
      <td> 
       <h3>File Contents: </h3> <br /> 
       <?php 
        if (isset($_POST['next'])) { 
         for($i = 0; $i < sizeof($file); $i++) { 
          $path = $dir ."/". $file[$i]; 
          showContent($path); 
         } 
        } 

        if (isset($_POST['previous'])) { 
         for($x = sizeof($file); $x > sizeof($file); $i--) { 
          $path = $dir ."/". $file[$i]; 
          showContent($path); 
         } 
        } 
       ?>  
      </td> 
     </tr> 
      <td align = "center"> 
       <input type = "submit" name = "previous" value = "Previous"> 
       <input type = "submit" name = "next" value = "Next"> 
      </td> 
    </frame> 
</table> 
</body> 
</html> 

Antwort

0

ich nicht wirklich von jedem denken kann, Möglichkeit, es neben dem ersten Inhalt des Ordners zu tun:

# You want to feed in the base directory, the current file path, and whether 
# to advance or previous 
function getContents($dir,$current = false,$advance = false) 
{ 
    # You don't want to get the file contents of images, it's garbled 
    $img  = ['gif','jpg','jpeg','png']; 
    # Get the contents of the folder and remove directories and dots 
    $contents = array_values(array_filter(scandir($dir),function($v) use ($dir){ 
     if(in_array($v,['.','..'])) 
      return false; 

     if(is_dir($dir.'/'.$v)) 
      return false; 

     return true; 
    })); 
    # If no current file, grab the first one 
    if(empty($current)) { 
     if(!empty($contents)) { 
      $nPath = array_shift($contents); 
      $next = $dir.'/'.$nPath;     
      return [ 
       'img' => (is_file($next) && in_array(strtolower(pathinfo($next,PATHINFO_EXTENSION)),$img)), 
       'file' => (is_file($next))? basename($next) : false, 
       'current'=> (!empty($nPath))? $next : false, 
       'data'=>(!empty($nPath) && is_file($next))? file_get_contents($next) : false 
      ]; 
     } 
    } 
    else { 
     # If there is a current file being viewed.. 
     foreach($contents as $key => $value) { 
      # See if this file is the file being viewed 
      if($dir.'/'.$value == $current) { 
       # If want to see next, get the next key 
       if($advance == 'next') 
        $nPath = (!empty($contents[$key+1]))? $contents[$key+1] : $contents[0]; 
       # Rewind the current array 
       else { 
        prev($contents); 
        prev($contents); 
        $nPath = (!empty($contents[key($contents)]))? $contents[key($contents)] : $contents[$key]; 
       } 
       $next = $dir.'/'.$nPath;     
       return [ 
        'img' => (is_file($next) && in_array(strtolower(pathinfo($next,PATHINFO_EXTENSION)),$img)), 
        'file' => (is_file($next))? basename($next) : false, 
        'current'=>$next, 
        'data'=>(!empty($nPath) && is_file($next))? file_get_contents($next) : false 
       ]; 
      } 
     } 
    } 
} 
# Set the action 
$action  = (!empty($_POST['action']))? strtolower($_POST['action']) : false; 
# Capture current path 
$curr  = (!empty($_POST['current']))? $_POST['current'] : false; 
# Retrieve the next or prev 
$contents = getContents(__DIR__,$curr,$action); 
?> 
<table border = "1" cellpadding = "10" align = "center" width = "500"> 
     <tr> 
      <td> 
       <h3>File Contents: <?php echo $contents['file'] ?></h3> <br /> 
       <?php if(!empty($contents['img'])): ?> 
       <img src="<?php echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$contents['current']) ?>" style="max-width: 100px;" /> 
       <?php else: ?> 
       <pre><?php echo htmlspecialchars($contents['data']) ?></pre> 
       <?php endif ?> 
      </td> 
     </tr> 
      <td align = "center"> 
       <form method="POST" action=""> 
        <input type="hidden" name="current" value="<?php echo $contents['current'] ?>" /> 
        <input type = "submit" name="action" value = "Previous"> 
        <input type = "submit" name="action" value = "Next"> 
       </form> 
      </td> 
    </frame> 
</table> 
</body> 
</html> 

Sie müssen das wahrscheinlich ein wenig verfeinern, aber es ist ziemlich nah ... Ich weiß nicht, wie effizient es ist.

+0

Vielen Dank für Ihre Antwort, ich schätze es und BTW, Frohes Neues Jahr :) Ich habe Ihren Code oben versucht, aber ich habe ein Problem, wenn ich auf die Schaltfläche "Zurück" klicke - wenn ich darauf klickte, war die Zelle leer oder ich habe einen "nicht identifizierten Fehler" erhalten. – madmhan84

+0

Ja, für mich gab es einen Punkt, der mehr Fehlersuche erforderte. Es funktionierte, aber wenn es ans Ende des Verzeichnisses kommt, hat es ein Problem. Ich konnte das nächste oder vorherige tun, außer wenn es auf diesen Punkt traf. Ich könnte es wahrscheinlich beheben, aber was ich habe, sollte der Hauptteil der Arbeit sein. – Rasclatt

+0

Lassen Sie mich sehen, ob ich es schnell schieße ... – Rasclatt

Verwandte Themen