带有随机名称的文件夹,并使用PHP将文件保存到它

时间:2011-10-10 20:29:07

标签: php javascript jquery post

所以我正在尝试创建一个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
?>

1 个答案:

答案 0 :(得分:2)

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

但为什么不在案件中使用tempnam()tmpfile()