如何上传到根目录以上的文件夹

发布于 2024-12-11 16:13:22 字数 1406 浏览 0 评论 0原文

我想上传到目录“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

是伱的 2024-12-18 16:13:23

我按照 user973... 的建议出现了 open_basedir 错误。

通过在以下位置创建 vhost.conf 来解决:

/var/www/vhosts/site.com/conf

添加以下内容:

<Directory /var/www/vhosts/site.com/httpdocs/>
php_admin_value open_basedir    "/var/www/vhosts/uploadvillage.com/httpdocs:/tmp:/var/www/vhosts/site.com/Uploads"
</Directory> 

然后使用以下命令重建 apache 配置:

[root@server1 ~]# /usr/local/psa/admin/bin/httpdmng --reconfigure-all

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:

<Directory /var/www/vhosts/site.com/httpdocs/>
php_admin_value open_basedir    "/var/www/vhosts/uploadvillage.com/httpdocs:/tmp:/var/www/vhosts/site.com/Uploads"
</Directory> 

and then rebuilt apache config with:

[root@server1 ~]# /usr/local/psa/admin/bin/httpdmng --reconfigure-all
泛泛之交 2024-12-18 16:13:22

可以是open_basedirsafe_mode限制,同时检查用户权限

can be open_basedir or safe_mode restrictions, also check user privileges

时光与爱终年不遇 2024-12-18 16:13:22

如果您尝试从根目录上传到 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/.

音盲 2024-12-18 16:13:22

尝试使用 $_SERVER['DOCUMENT_ROOT'] 锚定您的相对路径:

$destination = $_SERVER["DOCUMENT_ROOT"] . "/../Uploads/" . $random;

或者,您可以使用相对于当前 PHP 脚本的路径 __DIR__ (或dirname(__FILE__)(如果您使用的是 PHP 5.2):

$destination = __DIR__ . "/../Uploads/" . $random;

Try anchoring your relative path by using $_SERVER['DOCUMENT_ROOT']:

$destination = $_SERVER["DOCUMENT_ROOT"] . "/../Uploads/" . $random;

Alternatively, you can use a path relative to the current PHP script using __DIR__ (or dirname(__FILE__) if you are using PHP 5.2):

$destination = __DIR__ . "/../Uploads/" . $random;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文