如何上传到根目录以上的文件夹
我想上传到目录“Uploads”,该目录比根目录高一级(使用 Plesk)。
谁能告诉我为什么文件不能上传到那里?上传权限已授予 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;
特别查看
$destination = "../Uploads/" . $random ;
/var/www/vhosts/example.com/Uploads
中的权限。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我按照 user973... 的建议出现了 open_basedir 错误。
通过在以下位置创建 vhost.conf 来解决:
/var/www/vhosts/site.com/conf
添加以下内容:
然后使用以下命令重建 apache 配置:
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:
可以是
open_basedir
或safe_mode
限制,同时检查用户权限can be
open_basedir
orsafe_mode
restrictions, also check user privileges如果您尝试从根目录上传到 Uploads,则不要使用 ../Uploads/ 来引用该目录。它可以是 ./Uploads/,也可以只是 Uploads/。
../ 引用当前目录之上的目录,在您的情况下为 /var/www/vhosts/。
If 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/.
尝试使用
$_SERVER['DOCUMENT_ROOT']
锚定您的相对路径:或者,您可以使用相对于当前 PHP 脚本的路径
__DIR__
(或dirname(__FILE__)
(如果您使用的是 PHP 5.2):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):