Skip to main content
Inspiring
March 8, 2012
Answered

How to cffileupload and store file paths in a database?

  • March 8, 2012
  • 2 replies
  • 3954 views

I'm using CF 9 and have multi-file uploading working. What I need help with is a help desk type page that will display the filenames with links to the files that are uploaded with the form that a user submits. When I view an admin page, I need to see the email form results with a list of the files uploaded and links to where they are.

Any ideas on how I can get an admin page to display only the files associated with each form submission? I'm assuming that I need to find a method of stroring each uploaded file path to a database but I don't know how to do that. Any help would be appreciated. Thanks

    This topic has been closed for replies.
    Correct answer BKBK

    You could proceed as follows:

    1) Create a database table named uploadedFile. The columns could be, for example,

    upload_id: INT, autoincrement, primary key

    datetimecreated: DATETIME

    serverfile: VARCHAR(50)

    serverdirectory: VARCHAR(30)

    2) Do your multi-upload, for example,

    myMultiUploader.cfm

    <cffileupload url="myUploadFiles.cfm" width="600" ... etc>

    myUploadFiles.cfm

    <cffile action = "uploadAll" destination = "c:\myUploads" nameConflict = "overwrite">

    <!--- This is where your question gets answered --->   

    <!--- All the information about each uploaded file is stored in the cffile structure. ColdFusion creates one cffile struct for each and every file you upload in a single upload round --->

    <!--- Save the information about each file to the database. The back slash \ often serves the purpose of an escape character. So I've replaced it with the forward slash / in the file path. --->

    <cfquery name="saveUploadInfo" datasource="myDSN">

        insert into uploadedfile (datetimecreated, serverdirectory, serverfile)

        values (#cffile.timecreated#, '#replace(cffile.serverdirectory,"\","/","all")#', '#cffile.serverfile#')

    </cfquery>

    <!--- You could in fact log (and review) all the upload data by means of the following --->

    <!--- <cflog file="uploadData" text="#serializeJson(cffile)#"> --->

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    March 10, 2012

    You could proceed as follows:

    1) Create a database table named uploadedFile. The columns could be, for example,

    upload_id: INT, autoincrement, primary key

    datetimecreated: DATETIME

    serverfile: VARCHAR(50)

    serverdirectory: VARCHAR(30)

    2) Do your multi-upload, for example,

    myMultiUploader.cfm

    <cffileupload url="myUploadFiles.cfm" width="600" ... etc>

    myUploadFiles.cfm

    <cffile action = "uploadAll" destination = "c:\myUploads" nameConflict = "overwrite">

    <!--- This is where your question gets answered --->   

    <!--- All the information about each uploaded file is stored in the cffile structure. ColdFusion creates one cffile struct for each and every file you upload in a single upload round --->

    <!--- Save the information about each file to the database. The back slash \ often serves the purpose of an escape character. So I've replaced it with the forward slash / in the file path. --->

    <cfquery name="saveUploadInfo" datasource="myDSN">

        insert into uploadedfile (datetimecreated, serverdirectory, serverfile)

        values (#cffile.timecreated#, '#replace(cffile.serverdirectory,"\","/","all")#', '#cffile.serverfile#')

    </cfquery>

    <!--- You could in fact log (and review) all the upload data by means of the following --->

    <!--- <cflog file="uploadData" text="#serializeJson(cffile)#"> --->

    btvbillbAuthor
    Inspiring
    March 12, 2012

    Thanks! I've got that working.

    To display my uploaded files on a page where the files can appear as links or be deleted, how would I get only the uploaded files to display for each record in the database?

    For instance, one table has a "task" field which auto increments when a new task is added. I'm trying to figure out how to extract the proper records from each of my two tables in order to display only the uploaded files that go along with each "task" I add.

    Also, where would the log file "uploadData" log file be created/generated? Thanks again.

    BKBK
    Community Expert
    Community Expert
    March 13, 2012

    btvbillb wrote:

    To display my uploaded files on a page where the files can appear as links or be deleted, how would I get only the uploaded files to display for each record in the database?

    For example, make sure the upload directory is under the web root, something like: c:\ColdFusion9\wwwroot\myUploads. then you've already solved half the problem. The rest can proceed as follows (using my query above as example)

    <cfquery name="getUploadFiles" datasource="myDSN">

        select serverfile

        from uploadedfile

        where datetimecreated > #some_datetime_value#

    </cfquery>

    List of links: <br>

    <cfif getUploadFiles.recordcount GT 0>

    <cfoutput query="getUploadFiles" >

    http://127.0.0.1:8500/myUploads/#serverfile#<br>

    </cfoutput>

    </cfif>

    Also, where would the log file "uploadData" log file be created/generated?

    c:\ColdFusion9\logs

    Inspiring
    March 8, 2012

    To store a file path in a database table treat the path as a string, normally a VARCHAR column in a database.  You can associate these database records with a tech support incident.  Just INSERT a record for each file to be stored.

    To download the file supply the file's path to CFCONTENT.

    References:

    CFCONTENT

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c82.html

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec1553c-7feb.html