Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php扫描目录以创建指向PDF的链接?_Php_File - Fatal编程技术网

如何使用php扫描目录以创建指向PDF的链接?

如何使用php扫描目录以创建指向PDF的链接?,php,file,Php,File,我对PHP真的很陌生,但我想做的是为我的一些公司提供的工作制作一个动态目录 因此,如果我将所有工作描述的PDF文件放在名为../joboops的文件夹中,php文件位于根目录中,我需要做什么。 <?php $directory = "/jobops"; $contents = scandir($directory); if ($contents) { foreach($contents as $key => $value) {

我对PHP真的很陌生,但我想做的是为我的一些公司提供的工作制作一个动态目录

因此,如果我将所有工作描述的PDF文件放在名为../joboops的文件夹中,php文件位于根目录中,我需要做什么。


<?php
  $directory = "/jobops";
  $contents = scandir($directory);
    if ($contents) {
       foreach($contents as $key => $value) {
             if ($value == "." || $value == "..") {
                unset($key);
             }
       }
    }
        echo "<ul>";
    foreach($contents as $k => $v) {
      echo "<li><a href=\"$directory/" . $v . "\">link text</a></li>";
    }
        echo "</ul>";

?>
应该有用。获取目录,扫描它,将所有文件名映射到一个数组中
$contents
删除相对URL(“.”和“.”),然后将其回显

记住,
foreach
是相对昂贵的;因此,您可能希望简单地取消设置
$contents[0]
$contents[1]


根据以下内容编辑(来自OP):

警告: scandir(www.markonsolutions.com/joboops) [function.scandir]:打开失败 目录:中没有这样的文件或目录 /home/content/t/i/m/timhish/html/test.php 第5行警告:scandir() [function.scandir]:(errno 2):没有这样的 中的文件或目录 /home/content/t/i/m/timhish/h/i/s/h/html/test.php 第5行警告:参数无效 为中的foreach()提供 /home/content/t/i/m/timhish/html/test.php 第15行

我从“/joboops”更改了ti 认为它是一个相对目录 但显然不是这样。而且 我不知道这是怎么回事 /主页/内容。。。。。。。事情是这样的,但我是 目前与go daddy一起主持 他们就是这样储存东西的

$directory
变量与调用脚本的位置有关。在我的例子中,它是从根文件夹运行的,因此对我来说,它应该是,
$directory=“jobops”
假设文件夹和脚本存储在同一个位置。在不了解服务器目录结构的情况下,我无法真正帮助您,但我建议排除
scandir()
函数的问题

为此,在与脚本相同的目录中创建一个文件夹,调用任意名称,用至少一个图像填充它(以便下面的
if()
不会取消设置整个数组),然后查看是否有效。如果是这样的话,那么你就必须找到文件夹的相对路径。如果没有,那我自己也会陷进去


<?php
  // open this directory 
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
        print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<td>");
        print(filetype($dirArray[$index]));
        print("</td>");
        print("<td>");
        print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}
print("</TABLE>\n");


?>
应该有用。获取目录,扫描它,将所有文件名映射到一个数组中
$contents
删除相对URL(“.”和“.”),然后将其回显

记住,
foreach
是相对昂贵的;因此,您可能希望简单地取消设置
$contents[0]
$contents[1]


根据以下内容编辑(来自OP):

警告: scandir(www.markonsolutions.com/joboops) [function.scandir]:打开失败 目录:中没有这样的文件或目录 /home/content/t/i/m/timhish/html/test.php 第5行警告:scandir() [function.scandir]:(errno 2):没有这样的 中的文件或目录 /home/content/t/i/m/timhish/h/i/s/h/html/test.php 第5行警告:参数无效 为中的foreach()提供 /home/content/t/i/m/timhish/html/test.php 第15行

我从“/joboops”更改了ti 认为它是一个相对目录 但显然不是这样。而且 我不知道这是怎么回事 /主页/内容。。。。。。。事情是这样的,但我是 目前与go daddy一起主持 他们就是这样储存东西的

$directory
变量与调用脚本的位置有关。在我的例子中,它是从根文件夹运行的,因此对我来说,它应该是,
$directory=“jobops”
假设文件夹和脚本存储在同一个位置。在不了解服务器目录结构的情况下,我无法真正帮助您,但我建议排除
scandir()
函数的问题

为此,在与脚本相同的目录中创建一个文件夹,调用任意名称,用至少一个图像填充它(以便下面的
if()
不会取消设置整个数组),然后查看是否有效。如果是这样的话,那么你就必须找到文件夹的相对路径。如果没有,那我自己也会陷进去

看一看或函数

<?php
  // open this directory 
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
        print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<td>");
        print(filetype($dirArray[$index]));
        print("</td>");
        print("<td>");
        print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}
print("</TABLE>\n");


?>
(我很想把它作为评论发布,但是链接太长了)

看看它的功能

(我很想把它作为评论发布,但是链接太长了)

PHP5提供了一个很好的目录遍历界面:

// whitelist of valid file types (extension => display name)
$valid = array(
    'pdf' => 'PDF',
    'doc' => 'Word'
);

$files = array();    

$dir = new DirectoryIterator($directory_path);

foreach($dir as $file)
{
    // filter out directories
    if($file->isDot() || !$file->isFile()) continue;

    // Use pathinfo to get the file extension
    $info = pathinfo($file->getPathname());

    // Check there is an extension and it is in the whitelist
    if(isset($info['extension']) && isset($valid[$info['extension']])) 
    {
        $files[] = array(
            'filename' => $file->getFilename(),
            'size' => $file->getSize(),
            'type' => $valid[$info['extension']], // 'PDF' or 'Word'
            'created' => date('Y-m-d H:i:s', $file->getCTime())
        );
    }
}
(PHP5)提供了一个很好的目录遍历接口:

// whitelist of valid file types (extension => display name)
$valid = array(
    'pdf' => 'PDF',
    'doc' => 'Word'
);

$files = array();    

$dir = new DirectoryIterator($directory_path);

foreach($dir as $file)
{
    // filter out directories
    if($file->isDot() || !$file->isFile()) continue;

    // Use pathinfo to get the file extension
    $info = pathinfo($file->getPathname());

    // Check there is an extension and it is in the whitelist
    if(isset($info['extension']) && isset($valid[$info['extension']])) 
    {
        $files[] = array(
            'filename' => $file->getFilename(),
            'size' => $file->getSize(),
            'type' => $valid[$info['extension']], // 'PDF' or 'Word'
            'created' => date('Y-m-d H:i:s', $file->getCTime())
        );
    }
}

甜蜜的感谢我现在正在尝试(我必须稍微调整一下)你能解释一下我没有遵循的unset吗?
unset($key)
取消变量(
$content[0]
$content[1]
。如果你省略
unset()
步骤,你会看到它删除了什么,以及为什么(我认为)你应该这样做。警告:scandir(www.markonsolutions.com/joboops)[function.scandir]:在第5行的/home/content/t/i/m/timhish/html/test.php中打开目录失败警告:scandir()[function.scandir]:(errno 2):在第5行的/home/content/t/i/m/timhish/html/test.php中没有这样的文件或目录警告:为foreach()提供的参数无效在第15行的/home/content/t/i/m/timhish/html/test.php中,我将ti从“/joboops”改为“认为这是一个相对直接的尝试,但显然不是。我也不确定/home/content是什么……但我目前与go daddy一起托管,也许他们就是这样存储东西的?非常感谢我现在正在尝试(我必须稍微调整一下)你能详细解释一下我没有遵循的unset吗?
unset($key)
会取消变量(
$content[0]
$content[1]
。如果你省略了
unset()
步骤,你会看到它删除了什么,以及(我认为)你应该删除的原因。警告:scandir(www.markonsolutions.com/joboops)[function.scandir]:无法打开目录:第5行的/home/content/t/i/m/timhish/html/test.php中没有此类文件或目录警告:scandir()[fu