2012-02-14T12:32:16+01:00
2012-02-14T12:40:42+01:00
2022-06-29T04:15:52+02:00
tomtom92
Egy honlapon azt szeretnénk megoldani php-ban, ha egy könyvtárba feltöltünk bármilyen kiterjesztésű fájt, akkor azt egy kilistázás után, a fájl nevére kattintva ( ami egy linkként jelenik meg) töltse le.
Ez a kódrészlet kilistázza amik fent vannak a tárhelyen a doc mappában és ha rákattintok akkor megnyitja.
<?php
$dir = "../doc";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if (!is_dir($dir."/".$filename)) {
echo "<a href=".$dir."/".$filename.">".$filename."</a><br>";
}
}
?>
Ez a kódrészlet kilistázza amik fent vannak a tárhelyen a doc mappában és ha rákattintok akkor megnyitja.
Mutasd a teljes hozzászólást!
- PolicyMaster2megoldása
- 2012.02.14. 12:40
- permalink
Élőszőr is a kérdés a prog.hu-n tedd fel. De hogy ne így engedjelek oda, íme egy tipp:
1. Keress a php manuallban ITT
2. Esetleg egy nagyon egyszerü script:
Esetleg:
1. Keress a php manuallban ITT
2. Esetleg egy nagyon egyszerü script:
List all files in a directory
<?php
// Note that !== did not exist until 4.0.0-RC2
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
}
?>
List all files in the current directory and strip out . and ..
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Esetleg:
<?php
// This is highly stolen from Ash Young <ash@evoluted.net>. www.evoluted.net
// I rewrote it to do what I wanted it to do.
//Global Settings
$root = "/your/path/to/a/directory/"; // this will the the root position of this script
$filetypes = array (
'png' => 'jpg.gif',
'jpeg' => 'jpg.gif',
'bmp' => 'jpg.gif',
'jpg' => 'jpg.gif',
'gif' => 'gif.gif',
'zip' => 'archive.png',
'rar' => 'archive.png',
'exe' => 'exe.gif',
'setup' => 'setup.gif',
'txt' => 'text.png',
'htm' => 'html.gif',
'html' => 'html.gif',
'fla' => 'fla.gif',
'swf' => 'swf.gif',
'xls' => 'xls.gif',
'doc' => 'doc.gif',
'sig' => 'sig.gif',
'fh10' => 'fh10.gif',
'pdf' => 'pdf.gif',
'psd' => 'psd.gif',
'rm' => 'real.gif',
'mpg' => 'video.gif',
'mpeg' => 'video.gif',
'mov' => 'video2.gif',
'avi' => 'video.gif',
'eps' => 'eps.gif',
'gz' => 'archive.png',
'asc' => 'sig.gif',
'wma' => 'audio.jpg',
'mp3' => 'audio.jpg',
);
//Set our root position and make sure the URL input is not manually manipulated
if ((substr($_GET['dir'],0,2) != '/.') and (substr($_GET['dir'],0,1) != '.') and ($_GET['dir'] != '')) {
$mydir = $root . $_GET['dir']; }
else {
$mydir = $root;
}
//Get and sort the directory listing
$files = myscan($mydir);
sort($files);
//Compile our breadcrumb trail for the file paths
$breadcrumbs = "<a href='" . $_SERVER['PHP_SELF'] . "?dir='>Root</a>";
$bits = split('/', $_GET['dir']);
for ($x=1; $x<sizeof($bits); $x++) {
if ($bits[$x] != "..") {
$sofar = $sofar . "/" . $bits[$x];
$breadcrumbs = $breadcrumbs . " > <a href='" . $_SERVER['PHP_SELF'] . "?dir=" . $sofar . "'>" . $bits[$x] . "</a>";
}
}
//Common functions used
function myscan($dir) {
$arrfiles = array();
$arrfiles = opendir($dir);
while (false !== ($filename = readdir($arrfiles))) {
$files[] = $filename;
}
return $files;
}
?>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Directory Listing of Root<?=$sofar;?></title>
<link rel="stylesheet" type="text/css" href="dlf/styles.css" />
</head>
<body>
<div id="container">
<h1>Directory Listing of Root<?=$sofar;?></h1>
<div id="breadcrumbs"> <? echo $breadcrumbs; ?> </div>
<div id="listingcontainer">
<div id="listingheader">
<div id="headerfile">File</div>
<div id="headersize">Size</div>
<div id="headermodified">Last Modified</div>
</div>
<div id="listing">
<?
for ($x=0; $x<sizeof($files); $x++) {
if (($files[$x] != '.') and ($files[$x] != "..")) {
echo "<div>";
if(is_dir($mydir . "/" . $files[$x])) {
echo "<a href='" . $_SERVER['PHP_SELF'] . "?dir=" . $sofar . "/" . $files[$x] . "' class='g'><img
src='dlf/folder.png'><strong>" . $files[$x] . "</strong>";
}
else {
$ext = strtolower(substr($files[$x], strrpos($files[$x], '.')+1));
if($filetypes[$ext]) {
$icon = $filetypes[$ext];
} else {
$icon = 'unknown.png';
}
if (strlen($files[$x]) > 70) {
echo "<a class='g'><img src='dlf/" . $icon . "'<strong>" . substr($files[$x],0,70) . "...</strong>"; }
else {
echo "<a class='g'><img src='dlf/" . $icon . "'<strong>" . $files[$x] . "</strong>";
}
}
echo "<em>" . round(filesize($mydir . "/" . $files[$x])/1024) . " KB</em>";
echo date("M d Y h:i:s A", filemtime($mydir . "/" . $files[$x]));
echo "</a></div>";
}
}
?>
</div>
</div>
</div>
</body>
</HTML>
Mutasd a teljes hozzászólást!
Ez a téma lezárásra került a moderátor által. A lezárás oka: Ilyen jellegű kérdéssel a Prog.hu-t keresd fel a jövőben!