PHP mkdir(): 权限被拒绝
PHP mkdir():Permission denied
我有一个简单的 PHP/HTML/CSS 应用程序,可以为新注册的用户创建一个文件夹。它在我的测试站点上运行良好,并不是说我准备好 "go live" 我收到 "mkdir(): Permission denied" 错误。据我所知,两个站点上的所有设置都相同,根文件夹和上传文件夹的文件权限设置为 755。其他一切都按预期工作接受下面的代码...
if (count($errors) == 0) {
$pword = md5($pword_1);//encrypt the pword before saving in the database
$rand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
$rand = str_shuffle($rand);
$folder = substr($rand, 0, 10);
$regDate = date("Y-m-d");
$token = 0;
$tokenExp = 0;
$curDir = getcwd();
if(mkdir($curDir . "/uploads/" . $folder, 0755)){
$query = "INSERT INTO users (uname, email, pword, folder, regDate, token, tokenExp) VALUES ('$uname', '$email', '$pword', '$folder', '$regDate', '$token', '$tokenExp')";
mysqli_query($db, $query);
$_SESSION['uname'] = $uname;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else{
array_push($errors, "An error occurred creating your account!!!");
}
}
据我所知无法创建用户的文件夹,我无法上传文件。但是,在排除故障时,当我手动将文件夹添加到服务器时,我仍然收到 "path not found" 错误。这是上传文件代码...
if(isset($_POST['uploads'])){
$uname = mysqli_real_escape_string($db, $_POST['uname']);
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$extension = substr($name, strpos($name, '.') + 1);
$max_size = 2500000; //bytes
if(empty($name)) {
echo "<p class='error'>Please Select a File</p>";
}else{
if($extension == "jpg" || $extension == "jpeg" || $extension == "gif" || $extension == "tif" || $extension == "png" || $extension == "pdf"){
if($extension == $size<=$max_size){
$getFold = "SELECT * FROM users WHERE uname='$uname'";
$getFold = mysqli_query($db, $getFold);
while($for = mysqli_fetch_assoc($getFold)){
$folder = $for['folder'];
}
$location = "uploads/" . $folder . "/";
if(move_uploaded_file($tmp_name, $location . $name)){
$query = "INSERT INTO `upload` (name, size, type, location, uname, folder) VALUES ('$name', '$size', '$type', '$location', '$uname', '$folder')";
$result = mysqli_query($db, $query);
if($result){
echo "<p class='success'>File has been uploaded successfully!!!</p>";
}else{
echo "<p class='error'>Failed to upload file information to database!!! Filename already exist!</p>";
}
}else{
echo "<p class='error'>Failed to Upload File</p>";
}
}else{
echo "<p class='error'>File size should be 3MB or less</p>";
}
}else{
echo "<p class='error'>The selected file is not a JPG, JPEG, GIF, TIF, PNG, or PDF file type!!!</p>";
}
}
}
您使用 0755
权限创建目录,这意味着所有者拥有完全访问权限,其他人只能读取和执行。改成0777
或0766
;对任何人的完全访问权限或对所有者的完全访问权限以及对任何人的读写权限。
这也适用于父文件夹。
您需要将尝试为您的用户创建子文件夹的文件夹的所有者更改为:
apache
- 如果是 CentOS 服务器
www-data
- 如果是 Ubuntu 服务器
你可以用下面的命令来完成
sudo chown -R www-data /folder
-R
标志表示它是递归的,因此 apache/httpd 进程即 运行 php 也将拥有您可能创建的所有子文件夹。
有关此命令的更多信息,请查看此 SO post
感谢您的帮助,我听从了您的建议,结果发现我的主机提供商阻止了可通过 cPanel 访问的模糊 "setting" 位置中的功能。以前(即在我的旧测试 server/account 中),帐户自动设置为允许 read/write 访问,较新的帐户设置为只读并要求帐户所有者通过以下方式进行切换设置。
我有一个简单的 PHP/HTML/CSS 应用程序,可以为新注册的用户创建一个文件夹。它在我的测试站点上运行良好,并不是说我准备好 "go live" 我收到 "mkdir(): Permission denied" 错误。据我所知,两个站点上的所有设置都相同,根文件夹和上传文件夹的文件权限设置为 755。其他一切都按预期工作接受下面的代码...
if (count($errors) == 0) {
$pword = md5($pword_1);//encrypt the pword before saving in the database
$rand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
$rand = str_shuffle($rand);
$folder = substr($rand, 0, 10);
$regDate = date("Y-m-d");
$token = 0;
$tokenExp = 0;
$curDir = getcwd();
if(mkdir($curDir . "/uploads/" . $folder, 0755)){
$query = "INSERT INTO users (uname, email, pword, folder, regDate, token, tokenExp) VALUES ('$uname', '$email', '$pword', '$folder', '$regDate', '$token', '$tokenExp')";
mysqli_query($db, $query);
$_SESSION['uname'] = $uname;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else{
array_push($errors, "An error occurred creating your account!!!");
}
}
据我所知无法创建用户的文件夹,我无法上传文件。但是,在排除故障时,当我手动将文件夹添加到服务器时,我仍然收到 "path not found" 错误。这是上传文件代码...
if(isset($_POST['uploads'])){
$uname = mysqli_real_escape_string($db, $_POST['uname']);
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$extension = substr($name, strpos($name, '.') + 1);
$max_size = 2500000; //bytes
if(empty($name)) {
echo "<p class='error'>Please Select a File</p>";
}else{
if($extension == "jpg" || $extension == "jpeg" || $extension == "gif" || $extension == "tif" || $extension == "png" || $extension == "pdf"){
if($extension == $size<=$max_size){
$getFold = "SELECT * FROM users WHERE uname='$uname'";
$getFold = mysqli_query($db, $getFold);
while($for = mysqli_fetch_assoc($getFold)){
$folder = $for['folder'];
}
$location = "uploads/" . $folder . "/";
if(move_uploaded_file($tmp_name, $location . $name)){
$query = "INSERT INTO `upload` (name, size, type, location, uname, folder) VALUES ('$name', '$size', '$type', '$location', '$uname', '$folder')";
$result = mysqli_query($db, $query);
if($result){
echo "<p class='success'>File has been uploaded successfully!!!</p>";
}else{
echo "<p class='error'>Failed to upload file information to database!!! Filename already exist!</p>";
}
}else{
echo "<p class='error'>Failed to Upload File</p>";
}
}else{
echo "<p class='error'>File size should be 3MB or less</p>";
}
}else{
echo "<p class='error'>The selected file is not a JPG, JPEG, GIF, TIF, PNG, or PDF file type!!!</p>";
}
}
}
您使用 0755
权限创建目录,这意味着所有者拥有完全访问权限,其他人只能读取和执行。改成0777
或0766
;对任何人的完全访问权限或对所有者的完全访问权限以及对任何人的读写权限。
这也适用于父文件夹。
您需要将尝试为您的用户创建子文件夹的文件夹的所有者更改为:
apache
- 如果是 CentOS 服务器www-data
- 如果是 Ubuntu 服务器
你可以用下面的命令来完成
sudo chown -R www-data /folder
-R
标志表示它是递归的,因此 apache/httpd 进程即 运行 php 也将拥有您可能创建的所有子文件夹。
有关此命令的更多信息,请查看此 SO post
感谢您的帮助,我听从了您的建议,结果发现我的主机提供商阻止了可通过 cPanel 访问的模糊 "setting" 位置中的功能。以前(即在我的旧测试 server/account 中),帐户自动设置为允许 read/write 访问,较新的帐户设置为只读并要求帐户所有者通过以下方式进行切换设置。