I want to upload to directory ‘Uploads’ which is one step above the root (using Plesk).
Could anyone tell me why the files won’t upload there? Permissions have been give to Uploads as 777.
ob_start();
session_start();
$extensions = array("jpg", "png","jpeg", "gif", "zip", "rar", "swf", "tiff", "bmp", "txt", "fla", "7z", "tar", "gz", "iso",
"dmg", "mp3", "wav", "m4a", "aac", "doc", "docx", "xls", "rtf", "ppt", "bsd", "exe", "psd", "c4d", "pdf", "dwg", "max", "ipa",
"vtf", "iam", "ipt", "flv", "cap", "scr");
$maxsize = 104288000;
$server = "http://www.andre.com";
$name = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size'];
$random = md5(uniqid(rand(), true));
$random = substr($random, 0, 20);
if (!$name || !$temp || !$size)
{
echo "Go back and select a file.";
exit();
}
foreach ($_FILES as $file)
{
if ($file['tmp_name'] != null)
{
$thisext1=explode(".", strtolower($file['name']));
$thisext=$thisext1[count($thisext1)-1];
if (!in_array($thisext, $extensions))
{
echo "That file type is not allowed.";
exit();
}
}
}
if ($size > $maxsize)
{
echo "File size too big.";
exit();
}
$destination = "../Uploads/" . $random ;
mkdir($destination);
move_uploaded_file($temp, $destination."/".$name);
$final = $server."/".$destination."/".$name;
Looking at
$destination = "../Uploads/" . $random ;
in particular which is found in /var/www/vhosts/example.com/Uploads
.
4
Answers
I had a open_basedir error as user973... suggested.
Solved by creating vhost.conf in:
/var/www/vhosts/site.com/conf
Added in the following:
and then rebuilt apache config with:
can be
open_basedir
orsafe_mode
restrictions, also check user privilegesIf you’re trying to upload from the root to Uploads, you don’t use ../Uploads/ to reference that directory. It’s either ./Uploads/, or just Uploads/.
../ references the directory above the current directory, which in your case would be /var/www/vhosts/.
Try anchoring your relative path by using
$_SERVER['DOCUMENT_ROOT']
:Alternatively, you can use a path relative to the current PHP script using
__DIR__
(ordirname(__FILE__)
if you are using PHP 5.2):