Highlighted

cffile

Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

Hi,

Sorry if this is a dumb question, I'm new to this.

I have a form where i want the user to be able to upload an image from a form.  for the cffile i'm using

<cfif len(trim(form.ImageData))>

  <cffile action="upload"

     fileField="ImageData"

     destination="\uploads"

     nameConflict = "MakeUnique">

</cfif>

it works locally when i input the specific directory on my hard drive but when i use the above online i get an access denied error and it looks like it's trying to save it locally on c:\uploads

on my ftp i created an upload folder but I don't know what the 'destination' field typically looks like when you are trying to use cffile on a live site.  What information do i need from my web host to make this work? Like, does it involve an ip address?  I'm thinking some kind of username and password will be required.  does cffile have username password field too?  Basically, what shoudl cffile look like when you're using it ion a  live site

Thanks,

MIke

Here.. let's make this foolproof.  In your sites root folder, open the application.cfc, find the onRequestStart section and add the following:

<cfset rootPath = "#REreplaceNoCase(ExpandPath('./'),'(.+[\\|\/]mymindsnotright.com\www)(.)+','\1','all')#" />

This line will create a variable called rootPath, and no matter where in the folder structure the user goes, rootPath will always point to the root of the site.

Then, depending upon where in your site the uploads folder is, you append that flow to the variable rootPath.

If it's /uploads, then the destination will be "#variables.rootPath#\uploads".

If it's /another/folder/uploads, then destination will be "#variables.rootPath#\another\folder\uploads".

HTH,

^ _ ^

Views

1.3K

Likes

Translate

Translate

Report

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

cffile

Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

Hi,

Sorry if this is a dumb question, I'm new to this.

I have a form where i want the user to be able to upload an image from a form.  for the cffile i'm using

<cfif len(trim(form.ImageData))>

  <cffile action="upload"

     fileField="ImageData"

     destination="\uploads"

     nameConflict = "MakeUnique">

</cfif>

it works locally when i input the specific directory on my hard drive but when i use the above online i get an access denied error and it looks like it's trying to save it locally on c:\uploads

on my ftp i created an upload folder but I don't know what the 'destination' field typically looks like when you are trying to use cffile on a live site.  What information do i need from my web host to make this work? Like, does it involve an ip address?  I'm thinking some kind of username and password will be required.  does cffile have username password field too?  Basically, what shoudl cffile look like when you're using it ion a  live site

Thanks,

MIke

Here.. let's make this foolproof.  In your sites root folder, open the application.cfc, find the onRequestStart section and add the following:

<cfset rootPath = "#REreplaceNoCase(ExpandPath('./'),'(.+[\\|\/]mymindsnotright.com\www)(.)+','\1','all')#" />

This line will create a variable called rootPath, and no matter where in the folder structure the user goes, rootPath will always point to the root of the site.

Then, depending upon where in your site the uploads folder is, you append that flow to the variable rootPath.

If it's /uploads, then the destination will be "#variables.rootPath#\uploads".

If it's /another/folder/uploads, then destination will be "#variables.rootPath#\another\folder\uploads".

HTH,

^ _ ^

Views

1.3K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 02, 2018 0
Adobe Community Professional ,
Aug 02, 2018

Copy link to clipboard

Copied

I think it has to be a fully-qualified filesystem path:

c:\directory\subdirectory\

Dave Watts, Fig Leaf Software

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

Indeed, it does.  You need to put the full path from either the drive letter (C:\path\to\upload) on a Windows server, or /path/to/upload for a *nix server.  No relative paths allowed.

So, you could use ExpandPath('/') to find the current folder, and go from there.

V/r,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

sorry, how exactly would that look in the destination part of the coldfusion code?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

<cfif len(trim(form.ImageData))>

  <cffile action="upload"

    fileField="ImageData"

    destination="#ExpandPath('/')#\uploads"

    nameConflict = "MakeUnique">

</cfif>

This should work.

HTH,

^ _ ^

UPDATE: This will work IF the uploads folder is in the folder that the action page is in.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

like, would it look like

<cfset thisPath=ExpandPath("*.*")>

destination="\uploads(thisPath)"

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

No.

<cfset thisPath = ExpandPath('.') />

destination = "#thisPath#\uploads"

HTH,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 1
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

my web server gives me this error

java.io.FileNotFoundException: D:\WWWRoot\mymindsnotright.com\www\uploads\madness2.jpg (Access is denied)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

is there a way to add a username and password into cffile?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

or what information do i need from my web host in able to do this?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

You don't need a username and password.  You need to know how the file structure is on the host server.

From the root of your site, where is the uploads folder?  Is it www.yoursite.com/uploads?  Is it www.yoursite.com/another/folder/then/uploads?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

yes

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

Here.. let's make this foolproof.  In your sites root folder, open the application.cfc, find the onRequestStart section and add the following:

<cfset rootPath = "#REreplaceNoCase(ExpandPath('./'),'(.+[\\|\/]mymindsnotright.com\www)(.)+','\1','all')#" />

This line will create a variable called rootPath, and no matter where in the folder structure the user goes, rootPath will always point to the root of the site.

Then, depending upon where in your site the uploads folder is, you append that flow to the variable rootPath.

If it's /uploads, then the destination will be "#variables.rootPath#\uploads".

If it's /another/folder/uploads, then destination will be "#variables.rootPath#\another\folder\uploads".

HTH,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

so after i added that to applicaiton.cfc i get this error

ColdFusion was looking at the following text:

&lt;

The CFML compiler was processing:

    &lt; marks the beginning of a ColdFusion tag.Did you mean LT or LTE?
The error occurred in D:/WWWRoot/mymindsnotright.com/www/Application.cfc: line 8
6 : </cfcomponent> 7 : 8 : <cfset rootPath = "#REreplaceNoCase(ExpandPath('./'),'(.+[\\|\/]mymindsnotright.com\www)(.)+','\1','all')#" />

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

actually that's because there is no onrequeststart section

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

That means that something replaced my < with &lt; in the process, somewhere.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

Did you place that at the bottom of the application.cfc, or between the opening and closing tags for onRequestStart?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

Are you using application.cfc, or application.cfm?

Remove that line from the application.cfc/cfm, place it at the top of the action page for the file upload.  It just won't be available to your whole site.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

this is my whole application.cfc file

<cfcomponent>

<cfscript>

this.name = "MyMindsNotRight";

this.sessionmanagement = true;

</cfscript>

</cfcomponent>

<cffunction name="onRequestStart">

<cfset rootPath = "#REreplaceNoCase(ExpandPath('./'),'(.+[\\|\/]mymindsnotright.com\www)(.)+','\1','all')#" />

</cffunction>

i'm really sorry I'm very new at all this

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Adobe Community Professional ,
Aug 02, 2018

Copy link to clipboard

Copied

That looks like you've messed up the structure for Application.cfc. You can't have anything after the closing cfapplication tag.

Dave Watts, Fig Leaf Software

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

i don't have an onrequestart part of my application.cfc   where do i learn how to add it?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
LEGEND ,
Aug 02, 2018

Copy link to clipboard

Copied

<cffunction name="onRequestStart">

<cfset rootPath = "#REreplaceNoCase(ExpandPath('./'),'(.+[\\|\/]mymindsnotright.com\www)(.)+','\1','all')#" />

</cffunction>

Goes between the </cfscript> and </cfcomponent> tags.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Adobe Community Professional ,
Aug 02, 2018

Copy link to clipboard

Copied

Dave meant "closing cfcomponent", of course. But I see that Wolf has also sorted that out with you in a later note.

If all this doesn't work (you point to the right path and STILL can't write to it), that will be yet another problem, about permissions. I'll share a separate note about that in a moment.

/Charlie (server troubleshooter, carehart.org)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

ok i fixed that in the applicatino.cfc but I still get this error when i make  destination="#variables.rootPath#\uploads" 

java.io.FileNotFoundException: D:\WWWRoot\mymindsnotright.com\www\uploads\manic2.jpg (Access is denied)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Explorer ,
Aug 02, 2018

Copy link to clipboard

Copied

do i need to get some kind of information from my web host or am i out of luck here?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 0
Adobe Community Professional ,
Aug 02, 2018

Copy link to clipboard

Copied

Aha. You wrote your replies (confirming that it still did not work) as I was writing and posting this. But yours showed up before mine, so I am tweaking this (after posting it) so it makes sense as you or others come across it.

So you said earlier that you're running this code on a hosted server. I suspect the problem is that CF is setup (on the host) to run as a user (the user running the CF service) which DOES NOT HAVE PERMISSION TO WRITE TO THE DIRECTORY you want to write to.

If you think about it, it’s not unusual for a host to want to limit what can be written to a folder that would then be accessible via the web (as you show in your desired url of  http:///www.mysite.com/uploads). There are good security reasons to not want to allow that. Or to require that some precautions be taken (such as scanning the file for viruses, etc.) before letting such a file be accessible.

And here is where often the desires of a hosting company clash with the desires of their clients. YOU may want to be able to allow uploads of files to a web-accessible directory, but THEY may not. And in that case, you’re in a pickle.

But CF has a solution for that: you can use the CFFILE action=”upload” to save the file somewhere that they tell you they ARE OK to write to, and then you could serve that to users using a tag like CFCONTENT, which can serve up a file as content back to the user. But since you are new to CF, I won’t elaborate on this here. There are lots of potential pitfalls and challenges in using CFCONTENT (to solve various desires for its use).

So first, do you really need to write to that web-accessible folder, after all? If not, the web host may be willing to set some other non-web-accessible folder that they'd be willing to let CF write to.

If you were in control of the server, you could also perhaps change the folder (you want to write to) so that it had permission to be written to by CF (by the CF page doing the file upload to that directory). But again you don't, and the host may not be willing to do that.

In either case, again, you need to consider taking precautions about what you are letting people upload and then "download". (Some also want to let person A upload a file that only person A can see. That also is something that can be better managed/controlled via code using CFCONTENT to serve up the file.)

So just beware: what you want to do is something to be cautious about, even if you can get it working.

/Charlie (server troubleshooter, carehart.org)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 02, 2018 1