Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> How To Display Images Of A Directory
apple
post Feb 24 2008, 02:03 AM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 31
Joined: 9-August 06
Member No.: 28,049



I am trying to do a simple thing. I want to display all the images of a directory on a single page with the checkbox next to each image, so that i can select multi images and i can delete selected images.


Following few lines of code display the images of a directory.. i need help to put the check boxes with each image. and I dont understand how can i select multi images with check box and then delete them. I hope someone can help.
thanks.

CODE
<?php

$path = "./";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);

?>

Go to the top of the page
 
+Quote Post
apacheNewbie
post Feb 24 2008, 09:19 AM
Post #2


Newbie [Level 2]
**

Group: Members
Posts: 29
Joined: 23-November 06
Member No.: 33,877



I would use file name as check box identifier. So first, we will add checkbox in your code
CODE
<?php

$path = "./";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type=CHECKBOX name=$file>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);

?>


That is, now you have images with check box beside them. In order to delete the chosen images, you need to capture which checkbox is selected.
This is the example of how to know which check box is checked (assuming, you are using POST method to submit the form using delete button):

CODE
<?php
$path = "./";
$dir_handle = @opendir($path) or die("Unable to open folder");

//We list the name of the files again, since the name of the checkbox is the same with the name of the file
while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

if(isset($_POST[$file])){
   $checkbox = $_POST[$file];
   if($checkbox == on) { //checkbox is selected
      //Delete the file
      if(!unlink($file)) die("Failed to delete file");
   }
}
?>


Hopefully it helps smile.gif

This post has been edited by apacheNewbie: Feb 24 2008, 09:20 AM
Go to the top of the page
 
+Quote Post
iGuest
post Jun 26 2008, 09:22 AM
Post #3


Hail Caesar!
*********************

Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



PHP display a column in vertical way
How To Display Images Of A Directory

HI, I'm new in php, if anyone can help me, I would really appreaciate it, my problem is, iwant to display a column or one column in vertical way like this:

1 2
3 4
5 6

Not the usual way like this:
1
2
3
4
5
6

Hoping for anyones reply. Many thanks in advance.

-reply by marc
Go to the top of the page
 
+Quote Post
optiplex
post Jun 26 2008, 05:36 PM
Post #4


Newbie [Level 3]
***

Group: [HOSTED]
Posts: 40
Joined: 26-June 08
Member No.: 64,213



This should help, not sure though :

CODE
<?php
/* by optiplex */
$path = "./";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file)){
echo "<img src='$file' alt='$file'><br />";
}


}
closedir($dir_handle);


- optiplex

?>


This post has been edited by optiplex: Jun 26 2008, 05:36 PM
Go to the top of the page
 
+Quote Post
magiccode9
post Jun 29 2008, 02:10 PM
Post #5


Premium Member
********

Group: Members
Posts: 165
Joined: 1-November 05
From: SATA II
Member No.: 13,683



This may be help a bit.

CODE
<?php

$file_ary = isset($_POST['rfile']) ? $_POST['rfile'] : array();

/**
*   At first visit, show the file list
*/
if (empty($file_ary))
{
    $image_path = dirname(__FILE__) . '/images';
    $dir_handle = null;
    
    if (!($dir_handle = opendir($image_path)))
    {
        //trigger_error('error, path not found');
        return;
    }
    $html = '<html><form method="post">';
    $file = readdir($dir_handle);
    
    while ($file)
    {
        $html .= "{$file}<input type=\"checkbox\" name=\"rfile[]\" value=\"{$file}\" /><br />\r\n";
        $file = readdir($dir_handle);
    }
    closedir($dir_handle);
    
    $html .= '<input type="submit" name="submit" value="Submit" />';
    $html .= "</form></html>";
    echo $html;
}

/**
*   Now, we have list, display it or do anything you need to ..
*/
if (!empty($file_ary))
{
    echo '<pre>';
    print_r($file_ary);
    echo '</pre>';
}

?>


You may also check out this svn repository for updates when available
https://opensvn.csie.org/freescript
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting List Of Directories And Files Using Php(6)
  2. Vote Script Help(1)
  3. Script: Phpbb Mod, Total Users Registered(1)
  4. Including Php-made Images In A Page?(5)
  5. Securing Images(2)
  6. Directory Function(1)
  7. How To Create Images?(3)
  8. Help With Removing Files And Folders(4)
  9. Can You Add Images Into A Mysql Database?(20)
  10. <textarea> Tag Is Not Accepting Php Variable(2)
  11. How I Can Display Forum Topic On Main Website ?(4)
  12. How To Sort Files Of A Directory using Php(11)
  13. Directory Files Displaying(5)
  14. How To List Files In A Directory + Subdirectory And Then Use Them.(8)
  15. Displaying Files Of A Directory(2)
  1. Display MySQL results in a Table(8)
  2. Changin My Phpnuke Directory...please Help(13)
  3. Display Random File In A Directory(9)
  4. Help Needed With Directory/file Listing Code Infinite Loop(5)
  5. Display The Current Date/time(3)
  6. Reading Files From Directory To Array, And Using $_get To Get Them(2)
  7. My Code Doesnt Resize Large Images, Please Help.(2)
  8. How To Display The Latest Forum Post On Main Page(4)
  9. Unofficial Trap17 Hosted Members Directory(13)
  10. Php Ftp Upload Form(1)


 



- Lo-Fi Version Time is now: 11th October 2008 - 08:32 AM