I want to save an uploaded image to a folder up in the directory. I have tested these destinations:
$destination_path = __DIR__."/../img/";
and $destination_path = "../img/";
and $destination_path = "/../img/";
I also tried the following and it does not work
chdir('../img/');
$destination_path = getcwd().DIRECTORY_SEPARATOR;
Only this worked for me only if the file is placed in the website main directory!!:
$destination_path = getcwd().DIRECTORY_SEPARATOR;
My code:-
$destination_path = "/../img/";
$name = rand (100,1000);
$result = 0;
$filename=$_FILES['myfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION) ;
// setting up the directory of the file uploaded
$target_path = $destination_path . $name .".". $ext;
//making sure the file has been uploaded in the specified directory
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))
{
$result = 1;
}