Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Uploading files

I want to be able to upload files to a directory that will be selected by the user in the first page:

<form action="upload.cfm" method="post">
<select name="site" size="1">
    <cfoutput query="getoptions">
      <option value="#tblName#">#description#</option>
      </cfoutput>
  </select>
 <input type="submit" value="Go"></form>

then the upload page:

<cfif IsDefined("Form.Submit")>
<cflock name="FileToUpload" type="exclusive" timeout="10">
     <cffile
          destination="media/#form.site#"
          action="upload"
          nameconflict="makeunique"
          filefield="FileToUpload">
</cflock>
<cfelse>
     <form method="post" action="done.html" enctype="multipart/form-data">
          <input type="file" name="FileToUpload">
          <br>
          <input type="submit" name="submit" value="Upload">
     </form>
</cfif>

but i keep gettting the error SITE undefined in FORM ??
0
bjv211
Asked:
bjv211
  • 7
  • 6
  • 4
2 Solutions
 
Tacobell777Commented:
try this

<cfif IsDefined("Form.Submit")>
<cflock name="FileToUpload" type="exclusive" timeout="10">
     <cffile
          destination="media/#form.site#"
          action="upload"
          nameconflict="makeunique"
          filefield="FileToUpload">
</cflock>
<cfelse>
     <form method="post" action="done.html" enctype="multipart/form-data">
          <input type="hidden" name="site" value="#form.site#">
          <input type="file" name="FileToUpload">
          <br>
          <input type="submit" name="submit" value="Upload">
     </form>
</cfif>
0
 
Tacobell777Commented:
I *believe* you are posting to the action page and display a form, then submit that form and expect the form field from the first submit to be present, correct?
If so, you would need to include it as a hidden variable, form fields are not automatically passed from form to form.
0
 
bjv211Author Commented:
i'm passing form value from selection page to action page... on the action page it would be nice if it could post to itself and add the file name to my central database...is this possible or do i need to post to a secondary page
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Tacobell777Commented:
did you try my code?
0
 
bjv211Author Commented:
yea but i cant figure out where the file is going
0
 
Tacobell777Commented:
yes its possible, try my code and it should work...
0
 
bjv211Author Commented:
i'm going to have to get some sleep and come back with a fresh mind.... i'll tty tomorrow
0
 
Tacobell777Commented:
> yea but i cant figure out where the file is going

thats a different question isn't it?
0
 
Tacobell777Commented:
media/#form.site# is where the file is going, however this should really be a full path like c:\temp\whatever
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi bjv211!

I agree with what Tacobell777 said that it should be a full path will be specified for the attribute "destination".

> I want to be able to upload files to a directory that will be selected by the user in the first page
   Of course, this is really possible.  You can do this.

> but i keep gettting the error SITE undefined in FORM ??
   Of course, that error is expected 'coz it's really not defined on your form.

To accomplish what you want to achieve, try to run this revised code :

:: First Page ::
<form action="upload.cfm" method="post">
  <select name="site" size="1">
    <cfoutput query="getoptions">
      <option value="#tblName#">#description#</option>
    </cfoutput>
  </select>
  <input type="submit" value="Go">
</form>

:: Second Page ::
<cfif IsDefined("Form.Submit")>
  <cfset CurrentPath = GetDirectoryFromPath(GetBaseTemplatePath())>
  <cfset FullPath = '#CurrentPath#media\#form.site#'>

  <!--- Creating a new directory within your folder "media" --->
  <cfdirectory action="create" directory="#FullPath#">
 
  <!--- Uploading the file to the newly-created directory --->
  <cffile destination="#FullPath#" action="upload" nameconflict="makeunique" filefield="FileToUpload">
<cfelse>
  <form method="post" action="done.html" enctype="multipart/form-data">
    <input type="hidden" name="site" value="#form.site#">
    <input type="file" name="FileToUpload"><br>
    <input type="submit" name="submit" value="Upload">
  </form>
</cfif>

:: Reminders ::
   1.  I am assuming that on your root directory you have folder "media" for your uploaded files.
   2.  You will create a new directory first before uploading basing from your requirements.
   3.  Take note that when you refresh the page you might encountered an error since you are creating an existing directory.
        To avoid it, you will use the combination of "<cftry>" and "<cfcatch>" tags.

Hope this helps you.  Just try it and let me know the result.


Goodluck!
eNTRANCE2002 :-)
0
 
Renante EnteraSenior PHP DeveloperCommented:
Oooopppssss!  Forgot something on the part of the value of hidden field "site".

Instead of : value="#form.site#"

Replace it with this : value="<cfoutput>#form.site#</cfoutput>"


Regards!
eNTRANCE2002 :-)
0
 
bjv211Author Commented:
ok, after tweaks with you code, i got it uploading..thanks for the direction.  Now, I would like to get the name of the file that was uploaded, copy this to my database for indexing.

I have tables that correspond to the folders in media  FOr example I have NCtrip as a table and in it I have fields "description", filename.  I would like the table to be updated with the new file.  I know how to do the query, i just don't know how to get the filename I just uploaded.

0
 
bjv211Author Commented:
I just had a thought...can i just pass the variable "filetoupload to the next page and use this to process thequery?  i'll try
0
 
bjv211Author Commented:
ok so here is what I need help on.  I need to update my access table with the recently uploaded filename.  I realize you can do this using ServerFile attribute but I can't figure out where to place this code. here is my code that uploads the file.

first page:
<cfquery name="getoptions" datasource="pics">
SELECT * FROM cat ORDER BY description ASC
</cfquery>
<body>
<p class="pagehead" align="center">Upload Destination</p>
<div align="center">
<form action="upload.cfm" method="post">
  <select name="folder" size="1">
    <cfoutput query="getoptions">
      <option value="#tblName#">#description#</option>
    </cfoutput>
  </select>
  <input type="submit" value="Go">
</form>

2nd page:
<cfif IsDefined("Form.Submit")>

  <cfset FullPath = 'D:\inetpub\trappedinnyc\logged_in\media\#form.folder#'>
  <!--- Uploading the file to the directory --->
  <cffile destination="#FullPath#" action="upload" nameconflict="makeunique" filefield="FileToUpload">
<cfelse>
  <form method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="folder" value="<cfoutput>#form.folder#</cfoutput>">
    <input type="file" name="FileToUpload"><br>
      <input type="submit" name="submit" value="Upload">
  </form>
</cfif>
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi bjv211!

I think this deserves to be another question since you're main question is this :
  "I want to be able to upload files to a directory that will be selected by the user in the first page"

And it has been answered already.  Am i right ???

By the way, just a reminder.  If you want to get the root directory of your templates do not do it in a hard-coded way.

Use this : <cfset FullPath = '#GetDirectoryFromPath(GetBaseTemplatePath())#media\#form.folder#'>

Hope this makes sense ...


Regards!
eNTRANCE2002 :-)

0
 
bjv211Author Commented:
well since we never really answered my question  "I want to be able to upload files to a directory that will be selected by the user in the first page"
i will award by default.
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi bjv211!

I think I have answered your question.  Haven't you tried my code ???

>> I want to be able to upload files to a directory that will be selected by the user in the first page <<
     The lines below answered it :

     <cfset CurrentPath = GetDirectoryFromPath(GetBaseTemplatePath())>
     <cfset FullPath = '#CurrentPath#media\#form.site#'>
     <!--- Creating a new directory within your folder "media" --->
     <cfdirectory action="create" directory="#FullPath#">
     <!--- Uploading the file to the newly-created directory --->
     <cffile destination="#FullPath#" action="upload" nameconflict="makeunique" filefield="FileToUpload">

And I know that Tacobell777 will agree with me.  Am I right Tacobell777 ?


Regards!
eNTRANCE2002 :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now