Here, give this a try. I tested it on my (hosted) server and it works. Yet, I tested it by placing the files in the root of it, and used images
instead of ../images
for the $unique_directory
variable.
I also used the chmod
command apart from the other function, because the other method did not work.
N.B.: If possible, try changing 0777
to 0755
, because using 0777
is not the safest setting.
<?php
$filename = $_POST['item_name'];
$unique_directory = "../images";
if (!is_dir($unique_directory . '/' . $filename)){
mkdir($unique_directory . "/" . $filename);
chmod("$unique_directory" . "/" .$filename, 0777);
}
if (is_dir($unique_directory))
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "1) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "2) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
?>