Search code examples
phprelative-pathabsolute-pathopendir

PHP opendir works fine with relative path and not with absolute one?


Here is the problem: In one place I'm using relative path to load names of all files in certain folder:

if ($handle = opendir('images/uploads/form_id_1103/1'))

This is working fine, but if I change it to:

if ($handle = opendir('/images/uploads/form_id_1103/1'))

I get an error: No such file or directory in - just to mention, images folder is in the root, so /images should be valid

In the meantime, if I show an image from that ("non existing") folder with

<img src="/images/uploads/form_id_1103/1/test.jpg">

it works fine and shows the image.

I cannot use relative path, as I'm using Apache's mod_rewrite to transform URLs to SEO friendly ones.


Solution

  • You're confusing web URLs with file system paths. PHP's file-based functions dont' work with URLs, unless they're full+absolute http://blah/blah/blah/blah). You need to figure out the real path for your image on the server if you want to use an absolute one. It'd be something like

    /home/site/example.com/docroot/images/etc...
                                   ^---url starts here.