简体   繁体   English

PHP-在资料夹中建立资料夹

[英]PHP - Make folder in folder

Im pretty sure I simply dont have permission. 我很确定我只是没有权限。 Thank you for your answers anyway! 无论如何,谢谢您的回答! I will switch to self hosting so I KNOW i have permission! 我将切换到自托管,所以我知道我已获得许可!

(I would delete this but it says I cannot b/c there are answers) (我会删除它,但它说我无法b / c有答案)

First of all, what is the actual value of $username ? 首先, $username的实际值是多少? Have you verified that it's not empty? 您是否已验证不为空?

Dealing with the filesystem like this can result in several different problems. 像这样处理文件系统可能会导致几个不同的问题。 I like to put in a lot of extra checks so if something fails I have an easier time knowing why. 我喜欢进行很多额外的检查,因此如果出现故障,我会更容易知道原因。 I also like to deal in absolute directory names where possible, so I don't run into problems with relative paths. 我还喜欢在可能的情况下处理绝对目录名称,因此我不会遇到相对路径问题。

$filesDir = '/path/to/files';
if (!file_exists($filesDir) || !is_dir($filesDir)) {
    throw new Exception("Files directory $filesDir does not exist or is not a directory");

} else if (!is_writable($filesDir)) {
    throw new Exception("Files directory $filesDir is not writable");
}

if (empty($username)) {
    throw new Exception("Username is empty!");
}

$userDir = $filesDir . '/' . $username;

if (file_exists($userDir)) {
    // $userDir should be all ready to go; nothing to do.
    // You could put in checks here to make sure it's 
    // really a directory and it's writable, though.

} else if (!mkdir($userDir)) {
    throw new Exception("Creating user dir $userDir failed for unknown reasons.");
}

mkdir() has some really useful options for setting permissions and making folders multiple levels deep. mkdir()有一些非常有用的选项,用于设置权限并使文件夹更深。 Check out PHP's mkdir page if you haven't yet. 如果还没有,请查看PHP的mkdir页面

For security, make sure your exceptions aren't revealing system paths to the end user. 为了安全起见,请确保您的例外情况不会泄露最终用户的系统路径。 You may want to remove the folder paths from your error messages when your code goes onto a public server. 当代码进入公共服务器时,您可能希望从错误消息中删除文件夹路径。 Or configure things so your exceptions get logged but not displayed on the web page. 或进行配置,以便您的异常得到记录,但不会显示在网页上。

This is the algorithm for this kind of scenario. 这是用于这种情况的算法。

  1. Check if the folder exists. 检查文件夹是否存在。
  2. If the folder exists name the folder to something else or add a random number to it. 如果该文件夹存在,则将该文件夹命名为其他名称或向其中添加一个随机数。
  3. Create the new folder. 创建新文件夹。

    http://pastebin.com/VefbrzRS http://pastebin.com/VefbrzRS

尝试这个。

mkdir ("./files/$username");

Aren't those the same folder? 这些文件夹不是吗? files/Alex and files/Alex/ are the same. files / Alex和files / Alex /相同。 Do you mean files/$username and files/$username/files ? 您是指文件/ $用户名和文件/ $用户名/文件吗? you are doing the same dir twice so that's the error 你做两次相同的目录,这就是错误

If you're on Linux or MacOs, there is also another scenario which would consist in calling the mkdir function of your shell. 如果您使用的是Linux或MacO,则还有另一种情况,那就是调用Shell的mkdir函数。

It'll look like : 它看起来像:

system('mkdir -p yourdir/files/$username')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM