I’ve got the basics of how file uploads in PHP work. Once the submit button has been clicked, I grab the important stuff like the file size and file extension and check to make sure the user isn’t uploading anything too big or weird. After that, you give the file a new name, then move it into a folder called “uploads” located in your root folder.
All resources I’ve found online say to use the move_uploaded_file()
command to move the uploaded file from its temporary location ($_FILES['file']['tmp_name']
) to that uploads folder. The problem is, all those resources give examples of rather small projects with all files located right in the root folder. In my case, the scripting file is not located directly in the root folder but within a folder called scripts
in the root folder.
- MyProject
- pages
- scripts
- function.php
- style
- uploads
I’ve tried calling that move_uploaded_file()
function with the destination as "uploads/
and as "../uploads/"
(with the two dots in front) but neither work.
$fileNameNew = uniqid('', true).".".$fileExt;
$fileDestination = "../uploads/".$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
(Note: In the snippet above, $fileExt
is the file’s extension such as “jpg” and $fileTmpName
is the temporary location of the file.)
Is there something special I have to do to get the right relative location of the uploads folder?