Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

using HTML input type = "file"

Guest
Jun 19, 2009 Jun 19, 2009

I'm creating an input form for users so that they can input various bits of info into a db. One of the fields is so that they can upload a file, which puts the file onto the web server and the path to it into the db. The problem is if anyone edits the data after a file has been uploaded, the path to the file gets overwritten in the db with an empty string. I get round this problem in all other entry fields by pulling the data out the db and putting in the form entry box, but can't find a way to do this using the input type = "file" tag. Does anyone know how to do this?

588
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 19, 2009 Jun 19, 2009

Use if/else logic on the form page to only display the file input if the intent is to insert new records.

On your action page, if you are updating, don't update the field with the path to the file.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 19, 2009 Jun 19, 2009

The HTML standard makes it illeagal for you to populate a file field with any value.  This is for security reasons.  If developers could do this, it would be even easier for not nice developers to create web sites that could populate forms with file names for well known, sensitive files and steal user data.

You have to account for this in your action page.  Simply check the file field for a value before updating the database.  If it does not contain a value, then don't include that field in the SQL update statement.

<cfquery...>

...

<cfif len(trim(form.fileNameField)) GT 0>

databaseFileField = #fileNameValue#

</cfif>

...

</cfquery>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 19, 2009 Jun 19, 2009
LATEST

Ah, thanks guys, that would explain why I can't find a way of doing it. Cool, I'll do the workarounds you suggest.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources