使用随机函数创建目录时出错

时间:2015-09-11 05:40:46

标签: php css

我正在尝试使用以下代码创建一个用于保存上传照片的随机目录,但它不起作用。任何人都可以提供帮助。

//Photo upload script
if(isset($_FILES['profilepic']))
{

    if(((@$_FILES["profilepic"]["type"]=="image/jpeg")||(@$_FILES["profilepic"]["type"]=="image/png")||(@$_FILES["profilepic"]["type"]=="image/gif")) && (@$_FILES["profilepic"]["size"]<2048576))
    {
        $chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456789";
        $rand_dir_name=substr(str_shuffle($chars),0,15);
        mkdir("./userdata/images/$rand_dir_name");
        //mkdir("\\userdata\\images\\".$rand_dir_name,077,true); //tried this but no luck

        if(file_exists("userdata/images/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
        {
            echo @$_FILES["profilepic"]["name"]."Already exists";
        }
        else
        {
            move_uploaded_file(@$_FILES["profilepic"]["temp_name"],"userdata/images/$rand_dir_name/".$_FILES[profilepic][name]);
            echo "Uploaded and Stored in userdata/images/$rand_dir_name/".@$_FILES["profilepic"]["name"];
        }

    }
    else
    {
        echo "error";

    }
}

1 个答案:

答案 0 :(得分:0)

mkdir("./userdata/images/$rand_dir_name");

这一行应该是:

mkdir("../userdata/images/$rand_dir_name"); //in this format if you are using one directory back to the relative path

mkdir("/userdata/images/$rand_dir_name"); //in this format if you are trying to create from projects root path

mkdir("userdata/images/$rand_dir_name"); //in this format if you are trying to create a directory from the relative path

它不能有单点(。)后跟正斜杠