Jump to content

Recommended Posts

I wanted to create a file that lists all the files in a directory, show a thumbnail of each and give an option to delete.

Is this hard to do?

I have this code i found on the web

<?php
// open the current directory
$dhandle = opendir('.');
// define an array to hold the files
$files = array();

if ($dhandle) {
   // loop through all of the files
   while (false !== ($fname = readdir($dhandle))) {
      // if the file is not this file, and does not start with a '.' or '..',
      // then store it for later display
      if (($fname != '.') && ($fname != '..') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
          // store the filename
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
      }
   }
   // close the directory
   closedir($dhandle);
}

echo "<select name=\"file\">\n";
// Now loop through the files, echoing out a new select option for each one
foreach( $files as $fname )
{
   echo "<option>{$fname}</option>\n";
}
echo "</select>\n";
?>

It creates a nice list, but to get the files to display on click and an option to delete is over my head, any ideas or does someone have a code or link that could help mm.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/75084-delete-files-from-directory/
Share on other sites

I found a simpler code that does the same thing.

Can i use this code with some extra coding to show the selected image and then option to delete on submit?

It seems that there is a demand for this code.

<?php
echo "<select name=\"file\">\n";
foreach (new DirectoryIterator('.') as $file) {
   // if the file is not this file, and does not start with a '.' or '..',
   // then store it for later display
   if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
      echo "<option>";
      // if the element is a directory add to the file name "(Dir)"
      echo ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename();
      echo "</option>\n";
   }
}
echo "</select>\n";
?>

I used the code off this site and replaced the one that im using above.

 

<?php
$folder = "../images/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while (false !== ($file = readdir($handle)))
{
    if ($file != '.' && $file != '..')
       $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "<br />$file<a href=delete.php?$file=$file> 'Delete'</a>"; 
} 
?>

This shows all the files in my folder.

 

Then i changed the delete.php file with this code only

 

<?php
{unlink($File);}
?> 

 

and i get this error

 

Warning: unlink() [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\delete.php on line 2

I had one. This code creates a couple of text fields as well:

 

<?php
//print_r($_POST);

if($_POST["action"] == "Upload File")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>

<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<input type="file" name="image_file" size="20">
<br><br><input type="submit" value="Upload File" name="action">
</form>
<br>
<?php
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>
Folders<table width="600"  border="0" cellspacing="5" cellpadding="3">
  <?php
$files = array();
$dirs = array();
$dir=opendir($f_user);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..')  
{
 	if(!is_dir($f_user.'\\'.$file)){ 
		$files[] = $file;  
	}else{
		$dirs[] = $file;
	}
} 
} 
closedir($dir);
natcasesort($files);
natcasesort($dirs);
foreach($dirs as $v){
echo '<tr><td width="400" valign="middle"><img src="../upload_files/uploader_icons/folder_icon.png" width="16" height="14">	<a href="folderview.php?fol='.$v.'"><font color=purple>'.$v.'</a></font></td><td width="200"><a href="deletefol.php?fol='. $v .'">Delete</a></td></tr>';
}

echo "</table>\n";
echo 'Files<br><table width="600">';
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo '<tr><td width="400" valign="middle"><img src="../upload_files/uploader_icons/file_icon.png" width="14" height="16"> <a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a></td><td width="200"><a href=\delete.php?del='. $files[$i] .'>Delete</a></td></tr>';
}

echo "</table>\n";
?>
  <form action="subfol.php" method="post" name="subfolder" id="subfolder">
    <p>Add a subfolder</p>
    <p>Folder name: 
      <input name="subfol" type="text" id="subfol">
    </p>
    <p>
      <input type="submit" name="Submit" value="Create Folder!">
    </p>
  </form>

 

Might need a lot of modification. Was originally created for an image hosting system (people had their own directories, and could add subfolders). Went through it quick and took out all I could see, but knowing me there'll be loads I missed.

Thanks for trying man, looks like a lot of work.

The code here below lists all my files.

<?php
$folder = "../images/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while (false !== ($file = readdir($handle)))
{
    if ($file != '.' && $file != '..')
       $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "<br />$file<a href=delete.php?file=$file> 'Delete'</a>"; 
} 
?>

 

I will make some changes later in its appearance but the code does its thing.

 

the code i need to sort out is the delete.php

i'm a newbie to php but am getting a little drift of things.

i tried this code but to no avail

 

delete.php

<?php
{unlink($File);}
?> 

Well, firstly, the parameter you pass is called file - with a small f. The file you try to unlink is called $File with a capital F. Also, did you retrieve that variable from the $_GET array?

 

Try:

 

<?php
unlink($_GET['file']);
?>

 

I did a s you recommended and get the following error

Warning: unlink(Object) [function.unlink]: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\images\delete.php on line 2

I changed the code you pm me to this

 

<?PHP 

$folder = "../images/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while (false !== ($file = readdir($handle)))
{
    if ($file != '.' && $file != '..')
       $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "-<a href=$folder$file><label title=file>$file</a></label>";
echo "-<a href=filedelete.php?file=$file>Delete</a><br />";
    echo '<img src="$file" alt="image" width="150" />';
} 
?>

i tried to get a thumnail to display but it doesnt

As i am a newbie i think i may of found a little problem. When you are echoing the file location within the html ''img scr'' you need to include the full path details and not just $file, which is the name of the file. You need to set up a variable that contains both $folder and $file

As i am a newbie i think i may of found a little problem. When you are echoing the file location within the html ''img scr'' you need to include the full path details and not just $file, which is the name of the file. You need to set up a variable that contains both $folder and $file

Gave that a go before, it didnt work either.

;)

thanks for trying champ, and yes that makes sense

seems so strange as the file names echo correctly, why doesnt the link or thumnail work.

 

Code again with a couple of changes

<?PHP 

$folder = "../images/"; 
$handle = opendir($folder);

# Making an array containing the files in the current directory: 
while (false !== ($file = readdir($handle)))
{
    if ($file != '.' && $file != '..')
       $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "<a href=$folder$file><label title=$file>$file</a><br /></label>";
echo '<img src="$folder$file" alt="image" width="150" /><br />';
echo "<a href=filedelete.php?file=$file>Delete</a><br /><br />";

} 
?>

loaded the page and read the souce code. its here

 

<?php
<a href=../images/Conventoncntr.jpg><label title=Conventoncntr.jpg>Conventoncntr.jpg</a><br /></label><img src="$folder$file" alt="image" width="150" /><br /><a href=filedelete.php?file=Conventoncntr.jpg>Delete</a><br />
?>

 

something not right there.

this is the code generated from

 

<?php
#echo the files 
foreach ($files as $file) { 
    echo "<a href=$folder$file><label title=$file>$file</a><br /></label>";
echo '<img src="$folder$file" alt="image" width="150" /><br />';
echo "<a href=filedelete.php?file=$file>Delete</a><br /><br />";

} 
?>

i have this code

<?php
echo "<img src='$folder$file' alt="image" width="150" /><br />";?>

and i get this error

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\filelist.php on line 17

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.