I need a way of deleting images from a gallery. The images are in 2 folders: thumbs and full size. The pathnames are in 2 tables: image_thumbs and images. The gallery displays clickable thumbs. Should I delete the images from both folders and the entries from both table or abb a 'hide' field to image_thumbs, so if it has a value then the gallery does not show that image?


5 Years
Discussion Span
Last Post by diafol

you can keep status flag in table in mysql with each image.

say active inactive

and when u display gallery in php, just put where status='active'
so it will display only active images.

so i suggest not to delete images physically


I just want to be able to remove older images from the gallery. Hiding them sounds simpler than deleting them! I think I'll give it a go.

Urtrivedi, let me see if i have picked this up right.
Would I add 2 more fields to my table: active, inactive?


Physically deleting files shouldn't be harder than a flag change. Just a couple of lines of code. Setting a flag value will allow you to 'retrieve' hidden images if you so wish. Once the images are physically deleted, they're pretty much gone. Example of delete:

if(file_exists('thumbs/' . $filename)){
    unlink('thumbs/' . $filename);
if(file_exists('main/' . $filename)){
    unlink('main/' . $filename);

I now have a 'status' field of type enum ('active, 'inactive') as suggested.
When i add a photo do I need to insert the value active into 'status'? Then wheen I want to hoide an image change that value to 'inactive'?

When I try to add the value 'active' on addition to the table it doesnt work for some reason.
Here is the query, can anyone spot where I'm going wrong?

            $query = "INSERT INTO reptile_thumbs (id, species, imagepath, name, status) VALUE('', '$thumb_file', '$aname' 'active')";

Thanks for looking..........................


You may find it easier to store the status field as a tinyint [1], set to 1 (active) or 0 (inactive) as this is effectively a boolean field. You can set the field to default to 0 or 1, so when you add a record, you don't need to include it in your sql.

Edited by diafol


I just tried that, it stil doesnt work, goes staight to die()
Again if I drop status from the table it works ok, but then I have no status!
I cant figure out wwhats wrong!


What did you try? Include any code with your responses. I would favour the status method, but not if storing as [varchar] if you can use [tinyint].

As a side issue - your thumbs structure seems to be replicating your earlier table structure. This would suggest a dependency - probably best to avoid it.

You just need one images table:

image_id (PK) | filename | status | subcat_id

The subcat_id field may be useful if you need to provide an image selector for an animal that has no individual image. But could be left out. SO just one table for every image that you upload and create a thumb for.
The thumbnail imagename should be the same as the main image - just in two different folders. The paths shouldn't be stored in the DB, just the filenames themselves.

You can relate this to your animals thus:

animal_id | .... | image_id

Anyway, just a thought.


This is how the table are set up.

'id' int(11) primary ai
'imagepath' varchar(100)
'name' varchar(1000)
'status' tinyint(1) default(1)

'id' int(11) primary ai
imagepath varchar(1000)

and this is the code to add the thumbs to reptile thumbs annd reptile_gallery

 //add the files pathname to the database table image_thumbs
            $aname = $_POST['aname'];
            $query = "INSERT INTO reptile_thumbs VALUE('', '$thumb_file', '$aname')";
            $result = mysql_query($query);
                $error = 'An error occured: '. mysql_error().'<br />';
                $error.= 'Query was: '.$query;
            $query = "SELECT `id` FROM `reptile_thumbs` WHERE `imagepath` LIKE '$thumb_file'";
            $result = mysql_query($query);
                $error = 'An error occured: '. mysql_error().'<br />';
                $error.= 'Query was: '.$query;
            $q2 = mysql_fetch_assoc($result);
            $id = $q2['id'];
        // get width and height of original image
        //if landscape make width 500, if portrait make height 500
        list($image_width, $image_height) = getimagesize($remote_file);
        if($image_width>$max_upload_width || $image_height >$max_upload_height)
            $proportions = $image_width/$image_height;
                $new_width = $max_upload_width;
                $new_height = round($max_upload_width/$proportions);
                $new_height = $max_upload_height;
                $new_width = round($max_upload_height*$proportions);
        $new_image = imagecreatetruecolor($new_width , $new_height);
        $image_source = imagecreatefromjpeg($remote_file);
        imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);

        //add the files pathname to the database table table_images.;,
        $query = "INSERT INTO reptile_gallery VALUE($id, '$remote_file' 'a')";
        $result = mysql_query($query);
            $error = 'An error occured: '. mysql_error().'<br />';
            $error.= 'Query was: '.$query;
        header("Location: gallery_submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);

This goes to my die($message) with the 'status' field in reptile_thumbs. If I remove that field then it works finne, adding the image to the relevant places


When I set 'status' to 'default: none' it works using this query

$query = "INSERT INTO reptile_thumbs VALUE('', '$thumb_file', '$aname', '$astatus')";

With $astatus being 1. So that when I add an image I give it the status value of 1.

I'm still unsure of how these 2 tables can be 1 table?

INSERT INTO reptile_thumbs VALUE('', '$thumb_file', '$aname')

should be

INSERT INTO reptile_thumbs VALUES('', '$thumb_file', '$aname')

anyway. Will have a look at the rest when I have time, unless somebody else gets in.


OK, let me get this right from the point of view of what you need to do:

1) You upload an image
2) Store the original image in a folder
3) Create a thumbnail of the image and store it in a different folder but using the same filename
4) Write the filename to the DB

If this is the case, I still can't see why you need a separate thumbs table.

  1. Upload.
  2. Save to an 'images' folder and give it a unique name - possibly a timestamp, e.g. img_1352793331.png
  3. Create the thumbnail
  4. Save to the 'thumbs' folder - same name: img_1352793331.png
  5. Write filename to 'images' table

The paths could change at some time in the future. It could be a laborious process to have to change all the image path data in your DB, so just leave it out. Use some constants in your config file:

define('IMAGE_FOLDER', 'images/');
define('THUMB_FOLDER', 'thumbs/');

So when you retrieve your image filenames, you can code accordingly:

<img src="<?php echo THUMB_FOLDER . $filename;?>" /> 

Edited by diafol


Hey thanks for the reply.

Your correct in what I need to do.

As it stands now, my code gets the uploaded image, resizes it to thumbnail, saves it in a thumbnail folder then adds the path to a thumbnail table.

It then gets the uploaded image again, resizes it to a certain size for the full sized image, saves it to another folder then adds the pathname to another table.

It makes 'id' the same in both tables so that when you click on a thumb it links to the full size image with the same 'id'.

Maybe I am being a bit obtuse, but I dont understand how I do this using 1 table!
I am quite new to this all



Would this be correct?

<a href='<?php echo IMAGE_FOLDER . $filename;?>
<img src="<?php echo THUMB_FOLDER . $filename;?>" /></a>

You're duplicating all over the place with this, so as I mentioned previously:


Have one images table - that's it - no subcategory_thumbs tables, like reptiles_thumbs etc.

**image_id (PK, int, autoinc) | filename (varchar, 20) | description | status (tinyint) | [subcat_id (FK, int) - optional]

  1. Upload form - resize img, rename (see below) when saving to 'thumbs' folder.
  2. Resize original file to new dimensions and save as the new name to 'main' folder.
  3. Add record (new name) to the images table.

Rename image:

$ext = pathinfo($filename, PATHINFO_EXTENSION); 
$newname = "img_" . time() . "." . $ext;

When you want to retrieve the image an show a thumbnail with a link to the main image:

define("IMAGE_THUMB", "thumb/");
define("IMAGE_MAIN", "main/");


//assume records from DB as $row:

echo '<a href="' . IMAGE_MAIN . $row['filename']  . '"><img src= "' . IMAGE_THUMB . $row['filename'] .  '" /></a>';

You can add the image description to the img tag too.

Edited by diafol


Thanks for that.

I'm having a problem with this script now.

      $chk = (array) $_POST['zero'];
      $p = implode(',',array_keys($chk));  
      if ($sql = mysql_query("UPDATE reptile_images SET status = 0 WHERE `id` = $p")){
         echo 'Image hidden from gallery.';
      echo 'There has been a problem. Go back and try again.';

    else if(isset($_POST['submit'])){
      $chk = (array) $_POST['one'];
      $p = implode(',',array_keys($chk)); 
      if ($sql = mysql_query("UPDATE reptile_images SET status = 1 WHERE `id` = $p")){
         echo 'Image visible in gallery.';
      echo 'There has been a problem. Go back and try again.';

The problem is that when you choose more than 1 checkbox it does not change the value of status for anything and it goes straight to the 'else' statement.
Any ideads on why?


Edited by GlenRogers


I just changed

WHERE `id` = $p

to this

WHERE id IN ($p)

and it works fine, changging tthe value of status for every checkbox checked..................


Yay! Success? Yep, if you need to check (select) against multiple values or update multiple values use the IN syntax. It's much easier than concatenating a multiple OR clause. Whether it's slower or not I couldn't say.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.