I am trying to use a file command to read the contents of a file (which I will use to create a form). I want to do something like:

$path = 'http://localhost/asras1sites/default/files/images/AboutUs/MyFile.txt'
$text = file($path);
or
$text = file_get_contents($path);

However, when I try this I get a timeout error 10060. Using file_exists($path) indicates the system cannot find the file.

So, what am I doing wrong? Yes, the file does exist in the specified directory.

I also tried using
$path = '/asras1sites/default/files/images/AboutUs/MyFile.txt', with no better results.

I also placed the file in the same directory as the module, and using
$path='MyFile.txt'
still no luck:(

Do I need to enable something somewhere to access files or use these file commands?

I am running on a standalone test Windows laptop, no actual internet happening yet.

Thanks in advance,
brew

Comments

dman’s picture

I think you have a misunderstanding of file paths.

As a quick win, I'd suggest trying
$path = 'sites/default/files/images/AboutUs/MyFile.txt'
as relative file paths are much more portable, and the PHP code runs relative to the top dir of your drupal installation.

I don't recognise why you'd be using 'asras1sites' not 'sites', and trying to run with '/' at the top of your filepaths will work inconsistantly on windows which expects 'c:\' or something screwy. And will get confused if your Apache/PHP/Drupal-webroot is on a different drive. It can be worked around, but don't.

Your first attempt with an http filepath can also theoretically work in some settings (with php.ini settings allow_url_fopen that allow it - and proper network resolution etc) but it's the wrong direction to go. Was the URL REALLY correct? Doesn't look like a Drupal path.

There are BETTER ways within Drupal anyway. Look at the file API that will help you access drupal files in the drupal files directory - in a portable way.
http://api.drupal.org/api/function/file_create_path/6
- that may be a bit much to take in to start with, but it will help a LOT when you come to move your site between hosts.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

eridanibrew’s picture

Yes, the relative path works even better for what I want; I guess I forgot to try that combination (especially since I would love to use this syntax in my content pages to include images, and I can't seem to get it to work there http://drupal.org/node/352971). The relative path certainly makes more sense to me.

Oh, yeah, I did mistype the path in my post, although it was correct in the program - it should have been asras1/sites with the slash before sites. asras1 is my drupal installation directory. So, the path /asras1/sites/... did not work.

So, I tried with the relative path and it works great.

Thanks!