Highlighted

## cffile

Explorer ,
Aug 02, 2018

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))>

fileField="ImageData"

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.

HTH,

^ _ ^

Views

1.3K

Likes

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

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))>

fileField="ImageData"

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.

HTH,

^ _ ^

Views

1.3K

Likes

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
39 Replies 39
Aug 02, 2018

Copied

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

c:\directory\subdirectory\

Dave Watts, Fig Leaf Software

Likes

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
LEGEND ,
Aug 02, 2018

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

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
Explorer ,
Aug 02, 2018

Copied

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

Likes

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
LEGEND ,
Aug 02, 2018

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

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
Explorer ,
Aug 02, 2018

Copied

like, would it look like

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

Likes

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
LEGEND ,
Aug 02, 2018

Copied

No.

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

HTH,

^ _ ^

Likes

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 1
Explorer ,
Aug 02, 2018

Copied

my web server gives me this error

Likes

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
Explorer ,
Aug 02, 2018

Copied

Likes

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
Explorer ,
Aug 02, 2018

Copied

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

Likes

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
LEGEND ,
Aug 02, 2018

Copied

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

Likes

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
Explorer ,
Aug 02, 2018

Copied

yes

Likes

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
Explorer ,
Aug 02, 2018

Copied

Likes

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
Explorer ,
Aug 02, 2018

Copied

Likes

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
LEGEND ,
Aug 02, 2018

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.

HTH,

^ _ ^

Likes

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
Explorer ,
Aug 02, 2018

Copied

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

 ColdFusion was looking at the following text:<The CFML compiler was processing:< 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 : 7 : 8 :

Likes

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
Explorer ,
Aug 02, 2018

Copied

actually that's because there is no onrequeststart section

Likes

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
LEGEND ,
Aug 02, 2018

Copied

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

Likes

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
LEGEND ,
Aug 02, 2018

Copied

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

Likes

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
LEGEND ,
Aug 02, 2018

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

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
Explorer ,
Aug 02, 2018

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

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
Aug 02, 2018

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

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
Explorer ,
Aug 02, 2018

Copied

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

Likes

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
LEGEND ,
Aug 02, 2018

Copied

<cffunction name="onRequestStart">

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

</cffunction>

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

Likes

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
Aug 02, 2018

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

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
Explorer ,
Aug 02, 2018

Copied

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

Likes

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
Explorer ,
Aug 02, 2018

Copied

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

Likes

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
Aug 02, 2018

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

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 1