我有一个简单的PHP / HTML / CSS应用程序,为新注册的用户创建一个文件夹。它在我的测试网站上工作得很好,而不是我准备“上线”我得到“mkdir():Permission denied”错误。据我所知,两个站点上的所有设置都是相同的,root和uploads文件夹的文件权限设置为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!!!");
}
}
据我所知,无法创建用户的文件夹,我无法上传文件。但是,在排除故障时,我手动将文件夹添加到服务器时,仍然会收到“找不到路径”错误。这是上传文件代码......
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
创建目录,这意味着对所有者的完全访问权限,并且仅对其他人执行read + execute。将其更改为0777
或0766
;完全访问任何人或完全访问所有者并读取+写入任何人。
这也适用于父文件夹。
您需要更改要为用户创建子文件夹的文件夹的所有者:
apache
- 在CentOS服务器的情况下www-data
- 在Ubuntu服务器的情况下您可以使用以下命令执行此操作
sudo chown -R www-data /folder
-R
标志意味着它是递归的,因此运行php的apache / httpd进程将拥有您可能已创建的所有子文件夹。
有关此命令的更多信息,请查看此SO post
感谢您的帮助,我听从了您的建议,结果发现我的主机提供商在通过cPanel访问的模糊“设置”位置阻止了该功能。以前(即在我较旧的测试服务器/帐户上),帐户被自动设置为允许读/写访问,较新的帐户设置为只读,并要求帐户所有者通过设置进行切换。