Solved

updating image in database

Posted on 2003-11-18
3
208 Views
Last Modified: 2013-12-24
Hi all

I have a small application that allows the user to create a new record which includes an caption, description and image. I have all this working along with the facility to delete it all. I am trying now to update the records in the database and can update the caption and description fine but the image instead of passing in the new path, sets it to the wwroot directory with a temp file in it.

I have read 2 ways of uploading images. One is to put them in a folder and reference the folder in the database, the other is to upload the image directly (as I am currently doing). I do not know which is best though.

I am using cffile at the moment in the action page but this isnt doing the required action even with the destination set as my desired path.

Thanks in advance
0
Comment
Question by:saffcossie
  • 2
3 Comments
 

Author Comment

by:saffcossie
ID: 9773317
Does anyone know how to update an image so it overwrites to my chosen path please?
0
 
LVL 17

Accepted Solution

by:
anandkp earned 250 total points
ID: 9776340
The best is to "put images in a folder and reference the folder/file in the database"

with this - its very easy to update/delete image files !

the code for that wld be something like this :
<!---
1. New_FieldName = name of ur file_field on the previous page inside the form tag
2. Image_FieldName = name of the exisiting image
    [used only for deleting previous rec / id can also be used. alter the query below as required]
3. FilePath = full physical path where file needs to be uplaoded OR is uploaded
4. New_ImageName = variable used for assignign new name to the file after uplaod
    [incase of rename option being used]
5. FILEEXT = the extension of the file uploaded - so renaming can be handled properly
6. NewFileName = incase u wanna rename the file- the file uplaoded gets renamed with this value.
 --->

<CFIF ISDEFINED('NEW_FIELDNAME') AND TRIM(NEW_FIELDNAME) NEQ '' AND CGI.CONTENT_LENGTH LT 25000><!--- Upload --->
      <CFIF ACT EQ "EDIT"><!--- Del existing one - only incase of edit mode !!! --->
            <CFQUERY NAME="Del_Img" DATASOURCE="#DSN#" DBTYPE="ODBC" USERNAME="#USERNAME#" PASSWORD="#PASSWORD#">
                  Select New_FieldName from TableName where New_FieldName = #Image_FieldName#
            </CFQUERY>
            <CFIF DEL_IMG.RECORDCOUNT GT 0 AND FILEEXISTS('#FILEPATH##DEL_IMG.NEW_FIELDNAME#')>
                  <CFFILE ACTION="DELETE" FILE="#FilePath##Del_Img.New_FieldName#">
            </CFIF>
            <CFSET NEW_IMAGENAME = ""><!--- Since image is deleted - set it to blank --->
      </CFIF>
      <CFTRY>
          <CFFILE ACTION="UPLOAD" FILEFIELD="New_FieldName" DESTINATION="#FilePath#" NAMECONFLICT="MakeUnique" ACCEPT="image/jpg">
          
            <CFSET NewFileName = "anand"><!--- renaming the uploaded file to anand - change this to whatever in ur code --->
        <CFSET FILEEXT = LISTLAST(FILE.SERVERFILE,'.')><!--- RENAME HERE --->
        <CFFILE ACTION="RENAME" SOURCE="#FilePath##FILE.SERVERFILE#" DESTINATION="#FilePath##NewFileName#.#FILEEXT#">
        <CFSET NEW_IMAGENAME = "#NewFileName#.#FILEEXT#">                      
             
          <CFCATCH>
            <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
            <!--
            alert('image not selected properly pls select the image again')
            history.back(-1)
            //-->
            </SCRIPT>        
        </CFCATCH>
    </CFTRY>
      <CFQUERY NAME="Upd_Table">
            Update Table set ImageName = '#New_ImageName#'
      </CFQUERY>
<CFELSE>
    <!--- Keep the Image As It Is ! --->
    <!--- Dont do ne-thing here --->
</CFIF>

HTH

K'Rgds
Anand
0
 

Author Comment

by:saffcossie
ID: 9776852
Thanks anandkp. Great response. I had something similar but much more basic that wasnt doing it so that has virtually solved my problem so have awarded you the points. I have another question in relation to it so will post it on a new thread so I can award the points.

Cheers
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now