Im tyring to display files in my folder directory. I am able to view the folder
ID: 3538336 • Letter: I
Question
Im tyring to display files in my folder directory. I am able to view the folder but not the files inside that folder.
<?php
$username=$_SESSION['username'];
if(isset($_GET['directory'])){
if(strcmp($_GET['directory'],'userFolders/')==0){
$dir='userFolders/'.$username.'/';
}else{
$dir=$_GET['directory'];
}
$myfolders=myfolders($dir);
if(empty($myfolders)){
echo '<tr><td> <b> Your folder is empty </b></td><td></td><td></td></tr>';
}else{
foreach($myfolders as $folder){
echo '<tr>
<td align="left"><a target=_blank href="honey.php?directory=',$</span><span>folder,'"> ',$folder,'</a></td>
<td align="center"> --- </td>
<td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
</tr>';
}
$dir_handle = @opendir($dir) or die("Unable to open $dir");
while ($file = readdir($dir_handle)) {
if($file == "." || $file == "..")
continue;
echo '<tr>
<td align="left"><a target="_blank" href="'.$file.'"> ',$file,'</a></td>
<td align="center"> --- </td>
<td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
</tr>';
}
closedir($dir_handle);
}
}
}
Explanation / Answer
The function myfolders() isn't defined and the statement in Line 17 has syntax errors. You probably want scandir() for this
<?php
$username=$_SESSION['username' ];
if(isset($_GET['directory']))
{
if(strcmp($_GET['directory'],' userFolders/')==0)
{
$dir='userFolders/'.$username. '/';
}
else
{
$dir=$_GET['directory'];
}
$myfolders=scandir($dir);
if(empty($myfolders))
{
echo '<tr><td> <b> Your folder is empty </b></td><td></td><td></td></ tr>';
}
else
{
foreach($myfolders as $folder)
{
echo '<tr>
<td align="left"><a target=_blank href="honey.php?directory=$folder"> $folder</a></td>
<td align="center"> --- </td>
<td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
</tr>';
}
$dir_handle = @opendir($dir) or die("Unable to open $dir");
while ($file = readdir($dir_handle))
{
if($file == "." || $file == "..")
continue;
echo '<tr>
<td align="left"><a target="_blank" href="'.$file.'"> ',$file,'</a></td>
<td align="center"> --- </td>
<td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
</tr>';
}
closedir($dir_handle);
}