tags:

views:

108

answers:

4

I'm having trouble specifically with the getimagesize function. I'm making the function call from /item/ajax/image.php relative to the domain's HTTP root. I'm trying to get the dimensions of an image stored at /portfolio/15/image.jpg. From what I understand, the function takes a filename as an argument, so I tried the following:

getimagesize('/portfolio/15/image.jpg')

And

getimagesize('../../portfolio/15/image.jpg')

But both of them just threw PHP errors.

+2  A: 

try prefixing below to path:

 $_SERVER['DOCUMENT_ROOT']
Sarfraz
A: 

The filename you enter is not related to the http root but should be an existing path in the file system of your web server.

To see what goes wrong you could enter:

realpath('../../portfolio/15/image.jpg')

To see what directory you end up at.

Or use:

imagesize(dirname(__FILE__) . '/../../portfolio/15/image.jpg')

to get the full directory qualification.

As an alternative you can use the web address, but you should specify the full url:

getimagesize('http://yoursite.com/portfolio/15/image.jpg')

However, this is a slower option.

Matijs
A: 

In PHP "/" is not the same as the Apache "/" (web root). In PHP "/" refers to the system root. You should use paths relative to your PHP script location ('portfolio/15/image.jpg' if your script and the 'portfolio' folder are in the same location)

Brayn
+3  A: 

Relative paths always start from the file that is executed, which is most likely index.php. This is true for included files as well. This means in any file within you project relative paths start from your index.php. (Except a chdir() is done before)

I think it is really bad code to have paths like "../../file.ext" or the like. Define a Constant that has the full path to your application (eg: $_SERVER['DOCUMENT_ROOT']) and prepend it to any path you're using.

Example:

# somewhere in your index.php
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);

# in any included file
$my_path = ROOT_PATH."/portfolio/14/image.jpg"

This is imho the cleanest and most readable way to define paths.

Nils Riedemann