I want to create a class in php to upload files and validate file information before doing it. My class is not in the root directory. The structure is something like this:
-project
-root
index.php
-src
-classes
class.file.php
-files
myFile.txt
My file class is like this:
<?php
Class File {
public function uploadFile($file) {
$target = "../files/" . basename($file['name']);
//some additional validation
if(move_uploaded_file($file['tmp_name']) {
return true;
} else {
return false;
}
}
}
And finally my index file is:
<?php
include '/../../classes/class.file.php';
$objFile = new File();
if(isset($_POST['uploadFile']) && isset($_FILES['txtFile'])) {
if($objFile->uploadFile($_FILES['txtFile')) {
echo "file uploaded";
} else {
echo "file not uploaded";
}
}
?>
The problem I have is that this will only work if the relative target path is from the php file where the method is called. I can't use absolute path. How can I set my uploadFile method to work with the proper path no matter where it is called? Please be nice is one of my first projects in php.