具有随机名称的文件夹并使用 PHP 将文件保存到其中

发布于 2024-12-08 20:49:49 字数 1468 浏览 0 评论 0原文

因此,我正在尝试创建一个 PHP 脚本,客户端可以在其中创建一个具有 10 位随机字母和数字名称的文件夹,然后将他们当前正在处理的文档保存到该文件夹​​中。它就像一个 JSfiddle,您可以在其中保存当前正在处理的内容,并且它会创建一个随机文件夹。我的问题是它不会创建我的目录,这个想法是正确的,它应该可以工作。但是,PHP 不保存错误日志,因此我无法识别问题。这是我到目前为止所得到的。

PHP

save_functions.php

<?php
function genRandomString() {
    $length = 10;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;

}
<?php
function createFolder() {
    $folderName = genRandomString(); //Make a random name for the folder
    $goTo = '../$folderName';//Path to folder
    while(is_dir($goTo)==true){ //Check if a folder with that name exists
        $folderName = genRandomString();
        $goTo = '../$folderName';
    }
    mkdir($goTo,7777); //Make a directory with that name at $goTo
    return $goTo; //Return the path to the folder
}
?>   

create_files.php

<?php
include('save_functions.php');//Include those functions
    $doc = $_POST['doc'];//Get contents of the file
    $folder = createFolder();//Make the folder with that random name
    $docName = '$folder/style.css';//Create the css file
    $dh = fopen($docName, 'w+') or die("can't open file");//Open or create the file
    fwrite($dh, $doc);//Overwrite contents of the file
   fclose($dh);//Close handler
?>

So I am creating trying to create a PHP script where the client can create a folder with a 10 digit name of random letters and numbers, and than save the document they are currently working on into that folder. Its like a JSfiddle where you can save what you are currently working on and it makes a random folder. My issue is that it wont create my directory, and the idea is correct, and it should work. However, PHP isn't saving an Error Log so I cannot identify the issue. Here's what I got so far.

PHP

save_functions.php

<?php
function genRandomString() {
    $length = 10;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;

}
<?php
function createFolder() {
    $folderName = genRandomString(); //Make a random name for the folder
    $goTo = '../$folderName';//Path to folder
    while(is_dir($goTo)==true){ //Check if a folder with that name exists
        $folderName = genRandomString();
        $goTo = '../$folderName';
    }
    mkdir($goTo,7777); //Make a directory with that name at $goTo
    return $goTo; //Return the path to the folder
}
?>   

create_files.php

<?php
include('save_functions.php');//Include those functions
    $doc = $_POST['doc'];//Get contents of the file
    $folder = createFolder();//Make the folder with that random name
    $docName = '$folder/style.css';//Create the css file
    $dh = fopen($docName, 'w+') or die("can't open file");//Open or create the file
    fwrite($dh, $doc);//Overwrite contents of the file
   fclose($dh);//Close handler
?>

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

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

发布评论

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

评论(1

请叫√我孤独 2024-12-15 20:49:49

mkdir($goTo,7777) 的调用有错误的模式,这通常是八进制,而不是十进制或十六进制。 7777 是八进制的 017141,因此尝试设置不存在的位。尝试通常的 0777

但为什么在你的情况下不直接使用 tempnam()tmpfile() 呢?

The call to mkdir($goTo,7777) has wrong mode, this is usually octal and not decimal or hex. 7777 is 017141 in octal and thus tries to set non-existent bits. Try the usual 0777.

But why don't you just use tempnam() or tmpfile() in your case?

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