File Paths Query - Uploading

Soldato
Joined
18 Oct 2002
Posts
4,656
Location
The Darkside
I am having trouble uploading files to folders outside of the the folder I am running the php script from.

For example this folder layout works fine:
test (contains upload.php etc)
test/uploads (stores uploaded content)

I can't seem to upload files to a folder which is a level up from the one the script is in.

i.e. script is here

root/mysite/admin/upload.php

and the folder i want to upload files to is here:

root/mysite/uploads

If I had a html document stored in the admin folder in which it displays the images, I can display them using the path /uploads/image.jpg but I can't upload to it from a script running in a different folder.

Can anyone share some light as to how to get around this cause it should be straight forward but apparently its not.
I thought I could use the path /uploads but that doesn't work.

Cheers

PS- I am using 'move_uploaded_file' btw
 
Last edited:
Usually the quickest and easiest way to find this is to use the global server/environment variable DOCUMENT_ROOT i.e.

Code:
echo $_SERVER['DOCUMENT_ROOT'];
// returns /rootpath/mysite e.g. /home/scream/public_html

echo dirname($_SERVER['DOCUMENT_ROOT']);
// returns /rootpath e.g. /home/scream

DOCUMENT_ROOT returns the root path of your public directory wherever you are. dirname() will return the path of the directory holding your public website directory i.e. the root directory you're after.

You can also use realpath() on your returned path to ensure you're using the absolute pathname.
 
Cheers for that Sic.

For something thats simple to look at, working with file paths is very akward.
 
No problem - it still stumps me from time to time. Actually one got me this week - using dompdf, it has an autoload function and trying to get it not to conflict with my own nearly gave me a small heart attack.

Like I said, a bit more feedback would be so welcome, but I guess it's all part of the fun!
 
Back
Top Bottom