How can I upload a file outside public_html?
-
14-07-2021 - |
Question
I have a folder structure like this:
etc
files
public_html
tmp
I want users of my website to be able to upload files, and have those files stored inside the files
folder (as opposed to storing them inside some folder within public_html
). This is for security reasons. I'm a bit of a newbie, and I'm having trouble figuring out how to store the uploaded file to the files
folder. Here is my code (the relevant part?):
$path = dirname( __FILE__ ) ."/" . 'files/my_file.pdf';
$success = move_uploaded_file( $tmp_name, $path );
How should I change $path
to get this to work?
La solution
It is hard to say where you files are being uploaded to with knowing what the value for FRAMEWORK_PATH
is. If you specifically want to put a file to the location of your choosing just specify it explicitly like:
$path = '/path/to/files/';
$filename = 'name_you_want_to_give_upload';
$success = move_uploaded_file($tmp_name, $path . $filename);
Autres conseils
If you want to save a file outside where your upload.php files is, then you need to add ../
at the beaning of the file path where you wanna save it, eg:
if your upload.php files is in this root:
public_html/files/php/upload.php
but, your want to save your uploaded files on a folder which name is upFiles and it is outside the php folder (which is this case), you may write the path like this:
../upFiles/
You can use this method if you want to save it on a sub-folder too:
../files/anotherFolder/upFiles