Jump to content

Recommended Posts

 

 

i need a help regarding the following code pasted below.

 

<?php

function dirList ($directory) {

$results = array();

$handler = opendir($directory);

while ($file = readdir($handler)) {

if ($file != '.' && $file != '..') {

$results[] = $file;

dirList($directory);

}

}

closedir($handler);

return $results;

}

$results=$_POST['results'];

$dir = "c:\php";

$files = dirList($dir);

foreach ($files as $file) {

echo "<a href=\"$file\" title=\"$file\">$file</a><br />";

 

}

?>

 

the above code is fine in displaying the files and folders but when i am trying to click the any of the files and folders iam not able to open the file and folders.

 

can any one modify the above code and please reply to me .it sjould have access to open the file or folder when clicked to the path provided by us.

Link to comment
https://forums.phpfreaks.com/topic/95537-need-help-get-function/
Share on other sites

<?php
function dirList ($directory) {   
$results = array();
   $handler = opendir($directory);
   while ($file = readdir($handler)) {
      if ($file != '.' && $file != '..') {
         $results[] = $file;
dirList($directory);
      }
   }
   closedir($handler);
   return $results;
}
$results=$_POST['results'];
$dir = "c:\php";
$files = dirList($dir);
foreach ($files as $file) {
   echo "<a href=\"$file\" title=\"$file\">$file[/url]
";

}
?>

<?php
function dirList ($directory) {   
$results = array();
   $handler = opendir($directory);
   while ($file = readdir($handler)) {
      if ($file != '.' && $file != '..') {
         $results[] = $file;
dirList($directory);
      }
   }
   closedir($handler);
   return $results;
}
$results=$_POST['results'];
$dir = "c:\php";
$files = dirList($dir);
foreach ($files as $file) {
   echo "<a href=\"$file\" title=\"$file\">$file[/url]</a>";

}
?>

no closing <a> tag

no closing <a> tag

 

infact their was but the forum converted it to [/url] as it wasn't posted in a code tag,

 

the reason you can't open the file is because you need link to them via the web link not the file link

 

i guess you could do this

<?php
function dirList ($directory, $startpath)
{   
$results = array();
   $handler = opendir($directory);
   while ($file = readdir($handler))
   {
      if ($file != '.' && $file != '..')
      {
        $nextDir = $directory."/".$file;
        $results[] = preg_replace('%^'.preg_quote($startpath).'/%', '', $nextDir);
	if(is_dir($nextDir))
	{
        	$results = $results + dirList($nextDir, $startpath);
	}
      }
   }
   closedir($handler);
   return $results;
}
$results=$_POST['results'];
$dir = dirname(__FILE__);//"c:\php";
$files = dirList($dir, $dir);
foreach ($files as $file) {
   echo "<a href=\"$file\" title=\"$file\">$file</a><br>";
}
?>

I didnt get what u told, but I am sending the exact code which i am using for executing. The problem it is showing all files in c:\, and more over I am not able to access them.

<?php
function dirList ($directory, $startpath)
{   
$results = array();
   $handler = opendir($directory);
   while ($file = readdir($handler))
   {
      if ($file != '.' && $file != '..')
      {
        $nextDir = $directory."/".$file;
        $results[] = preg_replace('%^'.preg_quote($startpath).'/%', '', $nextDir);
	if(is_dir($nextDir))
	{
        	$results = $results + dirList($nextDir, $startpath);
	}
      }
   }
   closedir($handler);
   return $results;
}
$results=$_POST['results'];
$dir = dirname(""c:\php");//"c:\php";
$files = dirList($dir, $dir);
foreach ($files as $file) {
   echo "<a href=\"$file\" title=\"$file\">$file</a><br>";
}
?>

 

Please make requried modifiction.I am new to PHP.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.