首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP,在目录中创建文件

PHP,在目录中创建文件
EN

Stack Overflow用户
提问于 2013-11-30 08:53:06
回答 1查看 115关注 0票数 0

帮助!

因此,我已经在堆栈溢出的情况下看到了许多解决方案,但似乎没有奏效。所以我要做的是创建一个随机数的目录,然后在其中创建一个文本文件。

我的目录是这样的:

代码语言:javascript
复制
localhost
--/tdir/
---/(random number directory) < this is were i want to save to the text file

这是我的代码:

代码语言:javascript
复制
    <?php
            $dir = rand(1, 1999999);
            if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
            if ($dir == true) {
                echo "Created directory! :D";
            } else {
                echo "Failed to create directory! ~ :(";
            }

        chmod("/tdir/$dir/", 0777);
        echo "</br><a href='/tdir/'>Go Back</a>";
        echo "</br><a href='/tdir/$dir'>Go to page!</a></br>";
        $my_file = '../'.$dir.'/file.txt';
        $handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
        $data = 'This is the data';
        fwrite($handle, $data);
    ?>

我一直在犯这个错误:

代码语言:javascript
复制
Created directory! :D
Go Back
Go to page!
Cannot open file: ../1320710 *random number directory* /file.txt

我需要能够保存到file.txt目录下的$dir目录!

EN

回答 1

Stack Overflow用户

发布于 2013-11-30 09:00:17

您必须计算mkdir函数的响应,只需这样编写它即可。

代码语言:javascript
复制
if (mkdir($dir, 0777, true) === TRUE){
....

此外,在写入/读取文件夹时,请确保使用的是系统路径,而不是URL路径。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20298355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档