[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Over flow images

Posted on 2011-10-27
6
Medium Priority
?
216 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:lulu50
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 37037193
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>
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 37037218
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).
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 total points
ID: 37038094
I don't see it stated, but I am going to assume that the smlPathImage column is in the ADS table, not the UserTbl

So, along the same logic as brijesh, but some changes: fetch records from the ADS table based on inactive users and delete all of it's files before deleting the record from the Ad table.    Users are not deleted.


If you have already deleted the record in the ads table, it's a lot trickier to go and find files that have no database record associated with them.  Hopefully that is not the case.




<cfquery name="getFiles" datasource="#request.datasource#">
  select *
   from ADS
  where UserID in (select UserTbl.UserID
     from UserTbl
     where UserTbl.UserStatus = "I")
</cfquery>
<cfloop query="getFiles">
  
  <cfif len(getFiles.SmlPathImage1)>
    <cffile action = "delete" file = "#expandPath(getFiles.SmlPathImage1)#">
  </cfif>
  <cfif len(getFiles.SmlPathImage2)>
    <cffile action = "delete" file = "#expandPath(getFiles.SmlPathImage2)#">
  </cfif>
  <cfif len(getFiles.SmlPathImage3)>
    <cffile action = "delete" file = "#expandPath(getFiles.SmlPathImage3)#">
  </cfif>
  <cfif len(getFiles.SmlPathImage4)>
    <cffile action = "delete" file = "#expandPath(getFiles.SmlPathImage4)#">
  </cfif>
  <!---- files are deleted, now we can remove the data record ----->
  <cfquery name="removeAdd" datasource="#request.datasource#">
   delete from ADS where AdID = #val(getFiles.adID)#
  </cfquery>

</cfloop>

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:lulu50
ID: 37049760
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,

0
 

Author Closing Comment

by:lulu50
ID: 37049782
Thanks
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 37050034

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 :)

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question