NRGdallas
NRGdallas

Reputation: 395

file url issue in PHP

the below script, when I reference the URL, if I shorten it in any way, it fails to locate the files, however when left full-length, it outputs the full server url to the link and the href - is there a correct way to change the included directory to prevent this?

<?php

function getFiles($dir)
{
    $arr=scandir($dir);
    $res=array();
    foreach ($arr as $item)
    {
        if ($item=='..' || $item=='.'){continue;}
        $path=$dir.'/'.$item;
        if (is_dir($path))
        {
            $res=array_merge($res,getFiles($path));
        }
        else
        {
            $res[$path]=filemtime($path);
        }
    }
    return $res;
}

$files=getFiles('/var/www/vhosts/mysite.com/httpdocs/Manuals/');
arsort($files);
$files=array_slice(array_keys($files),0,11);
foreach ($files as $file)
{
    echo '<a href="'.$file.'">'.$file.'</a><br />';
}
?>

Upvotes: 0

Views: 87

Answers (1)

bluevector
bluevector

Reputation: 3493

Reference those files as relative to the script's __FILE__ and use dirname(...) to get the full, actual path. Then you can output the relative URLs in the html:

 $files=getFiles(dirname('Manuals'));
 arsort($files);
 $files=array_slice(array_keys($files),0,11);
 foreach ($files as $file)
 {
     $file = pathinfo($file)['filename'];
     echo '<a href="Manuals/'.$file.'">'.$file.'</a><br />';
 }

Upvotes: 1

Related Questions