Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Over flow images

Hi,

I have a problem with over flow images.  

How can I remove images that belongs to an inactive user from my folder.

so, if the user have an ad online and his/her membership expired or their membership is set to inactive.  I need to remove all their images that are stored in my server folder. Otherwise I will have tones of pictures stored.  

In my database I have a path stored to the images.

<cfif len(SmlPathImage1) and fileExists(expandPath(SmlPathImage1))>

My folder name is "LoadImages "

this will delete all ads that belong to the inactive users

  Delete from ADS
  where UserID in (select UserTbl.UserID
     from UserTbl
     where UserTbl.UserStatus = "I")

____________________________________

PathImage1 
/LoadImages/4_BTContactUs11.gif

PathImage2
/LoadImages/4_BTChat11.gif

PathImage3
/LoadImages/4_BTContactUs2.gif

PathImage4
/LoadImages/4_BThome2.gif

Open in new window

Avatar of Brijesh Chauhan
Brijesh Chauhan
Flag of India image

You can just use cffile action as delete to delete the files if you are deleting the user..

<cfif len(SmlPathImage1) and fileExists(expandPath(SmlPathImage1))>

<cffile  
    action = "delete"
    file = "#expandPath(SmlPathImage1)#">

</cfif>
So you have to do this

1. Get ALL the inactive users which you want to DELETE (Get the path of images), make it as a LIST

<cfquery name="getInactivePath" datasource="xxx">
select UserTbl.SmlPathImage1
     from UserTbl
     where UserTbl.UserStatus = "I"
</cfquery>

<cfset picDeleteList = ValueList(getInactivePath.SmlPathImage1);

2. DELETE inactive users (as with query in your post)

3. DELETE the pictures of users which are inactive, with help of LIST created in step 1 (Loop through the list and use CFFILE to delete it).
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulu50

ASKER

gdemaria:

this is great!!!  
what you posted is the answer to my question.

I do have the path to the image stored in the ADS table.

but, this is another question that I have to open a new question for it.

what if I want to delete all images that have no database records associated with them.

because, say the user have uploaded 4 images. than a few days later the user wanted to change the images so, the path to the images got changed but the previous images are still stored in the my folder. so, now I have 8 images stored in my folder for the same ad but 4 of them have path stored in my database.

to clean up my folder I also do have to delete all images that have no database records associated with them.

right now it will delete all images if the user is inactive but not any previous images uploaded by the same user.

ah, I just remembered that when the user upload the image to my folder I did rename each image so, if the user upload an image name chair.gif  the image is renamed before stored in my folder. It is renamed as the user id then the image name like this:

stored in my folder like this:
say the userid is 4

4_chair.gif
 I concatenate the user id with the name of the image. this is the original image

4_smlchair.gif  
 I concatenate the user id with the name of the image "sml" because I resized the image before save it in my folder

so, this is what I think I need to do

instead of concatenate the userid with the image name I should concatenate the Adid with the image name or maybe concatenate them all
Userid-Adid-imageName.gif

the same user can have more than 4 images stored in my folder but for each ad can only have 4 images that's why I have use the adid.


I can grab the AdID (primary key) and do a count if the AdID exist more than 4 time than check the path /LoadImages/5_bgSearch.gif  and file if exist.  If do not exist than they need to be deleted.


Thanks,

Avatar of lulu50

ASKER

Thanks

When I store images, I am always afraid of the name of the file the user is giving me.  It's entirely possible the way the user named the image, will be a bad name for saving on your disk.

What if they named the file :   Sam's #1 (Best) Picture.jpeg

Saving a name like that to the disk could cause you problems.  So this is what I do...

I change the name to match the record ID of the database record.  In your case, the Ad ID.   But since you are storing 4 images in for different fields, you can add something to it.  Perhaps a format like   AdID_Number.jpeg  such as     10_1.jpg  10_2.jpg   10_3.jpg  these would be use by ad ID 10 and be images 1, 2, 3.... out of four.

This could solve a couple problems - it would protect against bad file names and it would allow you to automatically over write images that are replaced (if image 3 is replaced for ad ID 10, you would write over 10_3.jpg)

Let me know if that appeals to you, otherwise, there are other strategies we can use.


Also, consider this... what if they ever get more than 4 images per ad?   Should you code a child table instead of having four seperate fields?

Ads
  ad_id
  ad_name
  ... etc....

AdImages
  image_id
  ad_id
  fileName
 
this could make coding easier for you now because you don't have to repeat everything 4 times for the 4 different fields, you just repeat the same code for X number of records in the database....

You could even add features like hiding an image so they can use one temporarily or something...

Something to think about... clients always change their mind in the future :)