一个 PHP 文件未执行,其他文件工作正常(使用 php 函数“mkdir”)

发布于 2024-12-29 19:22:19 字数 848 浏览 0 评论 0原文

最终编辑:已修复但未解决。不知道发生了什么,但我从头开始创建了一个新的 php 文件,并且该文件有效。很奇怪。

编辑:我对所有相关文件夹的权限设置为 777

编辑 2:向字符串添加引号

编辑 3:删除 字母 之前的 /

编辑 4:

<?php mkdir("letter/testfolder",0777); ?>

工作完美,并在 letter 中创建了文件夹 testfolder


我不太确定我在这里做错了什么。我有许多单独工作的 php 文件,但是当我尝试执行这个特定的文件(唯一一个使用 mkdir 的文件)时,它不起作用,而是加载 index.php 文件。我认为这是由于编码错误造成的?

代码是

<?php
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

foreach($letters as $letter) {
    mkdir("letter/$letter",0755);
    echo $letter;
    echo " created";
}
?>

想法是在文件夹 letter 内创建 26 个文件夹,每个文件夹对应字母表中的一个字母。

我觉得答案是非常明显的,但我发现最难发现的往往是我自己的明显错误。

FINAL EDIT: FIXED but not solved. Not sure what was going on, but I created a new php file from scratch and that one worked. Very strange.

Edit: I have permissions set to 777 on all related folders

Edit 2: Added quotes to strings

Edit 3: Removed / before letter

Edit 4:

<?php mkdir("letter/testfolder",0777); ?>

worked perfectly and created the folder testfolder within letter.


I'm not really sure what I'm doing wrong here. I have many php files that work on their own, but when I try to execute this particular one, the only one that uses mkdir, it does not work and it loads the index.php files instead. I assume it is due to bad coding?

The code is

<?php
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

foreach($letters as $letter) {
    mkdir("letter/$letter",0755);
    echo $letter;
    echo " created";
}
?>

The idea is to create 26 folders, one for each letter of the alphabet, inside the folder letter.

I feel like the answer is very obvious, but it is often my own obvious errors that I find the hardest to see.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

丶情人眼里出诗心の 2025-01-05 19:22:19
mkdir("/letter/$letter",0755);

尝试在位于文件系统根目录的 /letter/ 文件夹中创建文件夹。你那里有吗?如果您尝试使用当前工作目录的子文件夹,请使用

mkdir("letter/$letter",0755);

并确保文件夹 letter 已存在。

ps:php 文件位于磁盘的根目录,文件夹字母也是如此 - 非常奇怪的配置。您确定您谈论的是磁盘的根目录而不是网站的根目录吗?启用 php.ini 中所有错误的输出。

如果您想在站点的根目录下创建文件夹,请使用

 mkdir($_SERVER['DOCUMENT_ROOT'] . "/letter/$letter",0755);

但这仅在您能够运行脚本时才是正确的。您所说的“它加载index.php”意味着您的服务器的httpd.conf(假设您的服务器是Apache)或.htaccess中还有其他内容,将所有请求重定向到索引。 php 文件。如果您在网站上使用任何 CMS,这是可能的。

mkdir("/letter/$letter",0755);

Tries to create folder in the /letter/ folder located at the root of the file system. Do you have it there? If you are trying to work with subfolders of the current working directory then use

mkdir("letter/$letter",0755);

and be sure that folder letter already exists.

ps: The php file is at the root of the disk and so is the folder letter - very weird configuration. Are sure you talking about the root of the disk and not about the root of the website? Enable output of all errors in php.

If you want to create folder at the root of the site use

 mkdir($_SERVER['DOCUMENT_ROOT'] . "/letter/$letter",0755);

But this is true only if you are able to run the script. You phrase about it loads the index.php means that you have something else in the httpd.conf (lets say that your server is Apache) of your server or in .htaccess redirecting all request to the index.php file. If you are using any CMS on the website it could be possible.

岁吢 2025-01-05 19:22:19

您需要在路径前添加 .

<?php
error_reporting(E_ALL); //<<Always a good idea
foreach(range('a','z') as $letter){

mkdir("./letter/$letter",0755);
echo $letter.' created';
}
?>

You need to put a . before the path.

<?php
error_reporting(E_ALL); //<<Always a good idea
foreach(range('a','z') as $letter){

mkdir("./letter/$letter",0755);
echo $letter.' created';
}
?>
_畞蕅 2025-01-05 19:22:19

您应该执行以下操作:

  1. 启用错误报告
  2. 测试 mkdir 调用的结果。

一旦你做了这两件事,我猜你就会明白为什么事情不适合你:

// Useful in debug / development environments (disable in production)
error_reporting(E_ALL);

foreach ($letters as $letter)
{
    if(mkdir("/letter/$letter",0755))
        echo "$letter created\n";
    else
        echo "Failed to create $letter!\n";
}

There are a few things you should do:

  1. Enable error reporting
  2. Test the result of your mkdir calls.

Once you do these two things, I'm guessing you'll see exactly why things aren't working for you:

// Useful in debug / development environments (disable in production)
error_reporting(E_ALL);

foreach ($letters as $letter)
{
    if(mkdir("/letter/$letter",0755))
        echo "$letter created\n";
    else
        echo "Failed to create $letter!\n";
}
许一世地老天荒 2025-01-05 19:22:19

我被这个问题困扰了很长时间,也许这是你的问题。如果您使用的是 SELinux,则有一个名为“安全上下文”的额外权限层,您可以使用 ls -alZ 来查看它。使用诸如 chcon system_u:object_r:httpd_sys_content_t:s0 public_HTML 之类的东西

,不同目录中的相同文件会起作用或失败,这取决于这个额外的层。哈

i was stuck with this for a long time, perhaps this was your issue. if you are on SELinux there is an extra layer of permissions called "Security Context" which you can see with ls -alZ. use something like chcon system_u:object_r:httpd_sys_content_t:s0 public_HTML

it would make sense that the same file in different directories would work or fail, depending on this extra layer. hth

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