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

Can I test to see if a remote url file exists?

Explorer ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

I need to check to see if an mp3 file exists on a remote server. Can someone help me with this code?

thanks,

Richie

TOPICS
Database access

Views

14.0K

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
community guidelines

correct answers 1 Correct answer

Valorous Hero , Dec 14, 2010 Dec 14, 2010

It is not going to return a yes or no.  It is going to return a structure.  YOu then examin that structure to determine if it was a yes or no.

As BKBK showed

<cfhttp method="head" url="http://www.myurl.com/mymp3.mp3">

...

<cfdump var="#cfhttp#">

You would then be looking for a "200 OK" status.  200 OK is the HTTP status that the URL was found and returned what was requested.

<cfhttp...> documentation.

http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_g-h_09.html

Votes

Translate

Translate
Valorous Hero ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

<cfhttp....> to the url of the MP3 on the server.  Using the head option if you don't want to download the actual MP3 file.

A status 200 ok in the returned structer of the <cfhttp...> call would indicate that it exists, or at least something exists, at that url.

You may want to investigate other information in the returned structure to confirm that it is the specific desired content.

Votes

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
community guidelines
Explorer ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

Say the url is http://www.myurl.com/mymp3.mp3

How would I get <cfhttp to return a yes or a no?

Votes

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
community guidelines
Valorous Hero ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

It is not going to return a yes or no.  It is going to return a structure.  YOu then examin that structure to determine if it was a yes or no.

As BKBK showed

<cfhttp method="head" url="http://www.myurl.com/mymp3.mp3">

...

<cfdump var="#cfhttp#">

You would then be looking for a "200 OK" status.  200 OK is the HTTP status that the URL was found and returned what was requested.

<cfhttp...> documentation.

http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_g-h_09.html

Votes

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
community guidelines
Explorer ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

Thanks guys.

I got it to work using

<cfhttp method="head" url="http//www.mywebsite.com/test.htm" result="sc">

<cfoutput>#sc.statuscode#</cfoutput>

Votes

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
community guidelines
Community Expert ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

RichieWhite wrote:

How would I get ... a yes or a no?

You might decide to push the envelope, and do something like this instead:

<cfoutput>#fileExists("http://www.myurl.com/mymp3.mp3")#</cfoutput>

Votes

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
community guidelines
Valorous Hero ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

That seems to push the envelope beyond the limit of possibility.

CF8 Documentation

fileExists Parameters

Parameter

Description

absolute_path

An absolute path

CF9 Documentation

fileExists Parameters

Parameter

Description

absolute_path

The absolute path of the on-disk or in-memory file.

They seem to be pretty adamant that a URL is not an allowed value for the parameter.

Votes

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
community guidelines
Community Expert ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

While it doesn't work, it also doesn't error - it just always returns false!

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Read this before you post:

http://forums.adobe.com/thread/607238

Votes

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
community guidelines
Community Expert ,
Dec 15, 2010 Dec 15, 2010

Copy link to clipboard

Copied

Dave Watts wrote:

While it doesn't work, it also doesn't error - it just always returns false!

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/


OK. So, let's take your own web site. Put this in your pipe and light up:

fileExists("http://www.figleaf.com/"): <cfoutput>#fileExists("http://www.figleaf.com/")#</cfoutput><br>
fileExists("http://www.figleaf.com/images/topbanner_products.jpg"): <cfoutput>#fileExists("http://www.figleaf.com/images/topbanner_products.jpg")#</cfoutput><br>
fileExists("http://www.figleaf.com/images/bkbk_test.jpg"): <cfoutput>#fileExists("http://www.figleaf.com/images/bkbk_test.jpg")#</cfoutput>

You should get (on CF9 at any rate):

yes

yes

no

Votes

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
community guidelines
Community Expert ,
Dec 15, 2010 Dec 15, 2010

Copy link to clipboard

Copied

I don't smoke a pipe. But more importantly, I got NO NO NO. That's basically the test I tried before posting my response - I didn't know whether it would work or not. I'm running CF 9 on Windows x64, full JRun install.

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Read this before you post:

http://forums.adobe.com/thread/607238

Votes

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
community guidelines
Community Expert ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

Dave Watts wrote:
> I got NO NO NO.

I am curious what others got!

Votes

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
community guidelines
Contributor ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

Even I got NO NO NO; I m working in CF 9


Votes

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
community guidelines
Contributor ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

I got the same results you did, BKBK: Yes | Yes | No

Votes

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
community guidelines
Community Expert ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

JMF wrote:

I got the same results you did, BKBK:  Yes | Yes | No

Yey!

Votes

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
community guidelines
Community Expert ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

Perhaps this is a 9.0.1 thing - I tested with 9.0.0 as I haven't upgraded this machine yet.

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Read this before you post:

http://forums.adobe.com/thread/607238

Votes

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
community guidelines
Community Expert ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

Dave Watts wrote:

Perhaps this is a 9.0.1 thing - I tested with 9.0.0 as I haven't upgraded this machine yet.

Indeed. I am on build 9.0.1.274703

Votes

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
community guidelines
Valorous Hero ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

BKBK wrote:

Indeed. I am on build 9.0.1.274703

So now the question is - Is this documented anywhere as an enhancement|change in 9.0.1?

Votes

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
community guidelines
Contributor ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

Yeah, that could be it. I tested on a 9.0.1 installation.

Votes

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
community guidelines
Enthusiast ,
Dec 17, 2010 Dec 17, 2010

Copy link to clipboard

Copied

I tested with 9.0.1 and I get YES YES NO and there are a couple of

interesting things:

- cookies persist between function calls and page requests (in other

words if www.google.com sets a cookie in one request it will be sent

to google in subsequent page requests);

- it caches the connection so if you have multiple fileExists checks

it avoid opening and closing TCP connections;

- the connection to the server is not closed when the page requests is

completed (I think it stays open until the TCP timeout is reached).

- seems to do an extra HEAD request (for example if you have a single

fileExists the first HEAD request is done with "User-Agent: Jakarta

Commons-HttpClient/3.1" and the "real" request is done with

"User-Agent: Jakarta-Commons-VFS")

Interesting.

--

Mack

Votes

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
community guidelines
LEGEND ,
Dec 17, 2010 Dec 17, 2010

Copy link to clipboard

Copied

Yeah, URLs seem to work for me on 9.0.1, too.

I'm not sure I'm convinced this is a sensible feature of fileExists(). Well it mostly seems OK, but something about it doesn't quite sit right for me. I guess it's because the operations to implement the "file system" side of things is completely different to that for checking URLs. And if I found myself writing a function which had two completely separate bits either side of an "else" statement, I'd start wondering if I was putting too much into the function.

What does it do with 30x responses? Also, does it do a HEAD request, or a full GET? I presume it's the latter.

Wouldn't it be nice if Adobe kept their docs up to date? Surely if someone had a ticket raised to "add this functionality to fileExists()", there should have also been a ticket to "update the documentation for fileExists()".

Personally I think I would have preferred a urlExists() function, if they were going to do this.

Still: bottom line is that it'll be handy for some people, so it's a good thing.

--

Adam

Votes

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
community guidelines
Enthusiast ,
Dec 17, 2010 Dec 17, 2010

Copy link to clipboard

Copied

What does it do with 30x responses?

Follows them.

Also, does it do a HEAD request, or a full GET? I presume it's the latter.

HEAD, although some servers treat that as a GET.

Wouldn't it be nice if Adobe kept their docs up to date? Surely if someone had a ticket raised to "add this functionality to fileExists()", there should have also been a ticket to "update the documentation for fileExists()".

I think this might have slipped in 9.0.1 with the support for Amazon

S3 (for example fileOpen("http://www.google.com/") does a couple of

head requests and then a GET request and then errors out with "Complex

object types cannot be converted to simple values.").

--

Mack

Votes

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
community guidelines
LEGEND ,
Dec 17, 2010 Dec 17, 2010

Copy link to clipboard

Copied

I think this might have slipped in 9.0.1 with the support for Amazon

S3

Yeah, fair enough: that would make sense.

(for example fileOpen("http://www.google.com/") does a couple of

head requests and then a GET request and then errors out with "Complex

object types cannot be converted to simple values.").

Nice. Great feature that one.

--

Adam

Votes

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
community guidelines
Valorous Hero ,
Dec 16, 2010 Dec 16, 2010

Copy link to clipboard

Copied

Add me to the tripple NO club.

fileExists("http://www.figleaf.com/"): NO
fileExists("http://www.figleaf.com/images/topbanner_products.jpg"): NO
fileExists("http://www.figleaf.com/images/bkbk_test.jpg"): NO

productversion 9,0,0,251028

Votes

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
community guidelines
Community Expert ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

<!--- Head method is same as Get method, but here, server doesn't send back any response. We only need to check whether file exists --->
<cfhttp method="head" url="url_of_the_mp3_file">

Votes

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
community guidelines
Community Expert ,
Dec 14, 2010 Dec 14, 2010

Copy link to clipboard

Copied

I reacted before seeing the last post.

Votes

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
community guidelines
Resources
Documentation