массивы — Как просматривать файлы и показывать их содержимое, нажимая кнопки в PHP?

Как я могу просмотреть файлы в моей папке, нажав Next кнопка и Previous кнопку затем показать содержимое файла внутри ячейки таблицы?

Вот мой пример вывода, но, к сожалению, он не работает для меня:

Вывод изображения

Вот код, который я использовал:

<!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>

-1

Решение

Я не могу придумать, как это сделать, кроме как сначала получить содержимое папки:

# 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>

Возможно, вам придется немного уловить это, но это довольно близко … Хотя я не знаю, насколько это эффективно.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector