Reputation: 12955
I am trying to save a user uploaded file below web root, to prevent it from being retrieved without permission or direct linked to.
The upload works perfectly when saving to a folder which is below web root.
For instance, the page is served from D:\xampp\htdocs\foo\
, and the script runs at D:\xampp\htdocs\foo\library\
. When I save to ../../uploads/
the page resolves to http://my.local/uploads/image.jpg
.
When I save to $_SERVER['document_root'] . '../uploads/'
, however, the upload fails and no file is located at D:\xamp\htdocs\uploads
, although I have verified that the path is resolving to the correct location in the script, and that the folders exist and have read/write/execute.
My local dev environment is windows based, but my production server will be a linux server.
Upvotes: 0
Views: 620
Reputation: 12955
So, in actuality, the upload script that I found on the web had is not using the a variable which it purports to be using, which is why I received no error error message. Turns out the files were being saved, just to a different directory. I had to tweak the code in a few places but it runs now. Thanks.
Upvotes: 0
Reputation: 719
you say When I save to $_SERVER['document_root'] . '.../uploads/',
but I notice several things in there
1. you have three dots before /uploads and that won't resolve properly
2. you are using a mixture of windows ....\ notation and linux notation ../../ which might confuse xamp
3. if $_SERVER['document_root'] is D:\xamp\htdocs\
then $_SERVER['document_root'] . '..\uploads\' is D:\xamp\uploads
not D:\xamp\htdocs\uploads
since you don't say exactly what $_SERVER['document_root']
refers to I can't be sure.
Upvotes: 0
Reputation: 2424
A better solution might be to store it in a folder, withint your web root, that doesn't have world read permissions. This would prevent access in much the same way as you require.
Upvotes: -1
Reputation: 27478
Excellent news, Apache is saving you from a world of grief and pain!
Imagine if I renamed reallybad.exe
to svchost.exe
and uploaded it to C:\WINDOWS\system32\svchost.exe
You can achieve this by putting a symbolic link from a folder in your web directory to your desired directory.
Upvotes: 2
Reputation: 157872
It seems there are tons of typos in your code.
$_SERVER['DOCUMENT_ROOT']
not $_SERVER['document_root']
../uploads/
not .../uploads/
There are some rules to follow.
error reporting
at E_ALLdisplay_errors
on on the development serverand you'll be able yo solve most of the problems yourself
however, if you still can't - there are some other rules
Upvotes: 2