如何用PHP里面的图像创建一个文件夹

时间:2014-02-13 22:50:09

标签: php file-upload mkdir

当用户注册到我的网站时,我想创建一个文件夹,其中包含用户名和默认配置文件pic图像。我知道如何创建一个文件夹,但是如何创建一个包含文件的文件夹。

该文件夹应如下所示:

/users/pcoulson/

(pcoulson将是用户的用户名)

../pcoulson/应该包含默认的个人资料照片:

/users/pcoulson/default-profile_pic.png

如何使用 PHP

执行此操作

4 个答案:

答案 0 :(得分:2)

$dir='users/'.$username;
mkdir($dir);
copy('default-profile_pic.png',$dir.'/default-profile_pic.png'

答案 1 :(得分:0)

if(isset($_POST['add-user-submit']) && isset($_FILES['image']['name']))
{
    #label the form inputs
    $username = $_POST['username'];
    $image = $_FILES["image"]["name"]; // The file name
    $fileTmpLoc = $_FILES["image"]["tmp_name"]; // File in the PHP tmp folder
    $fileType = $_FILES["image"]["type"]; // The type of file it is
    $fileSize = $_FILES["image"]["size"]; // File size in bytes
    $fileErrorMsg = $_FILES["image"]["error"]; // 0 = false | 1 = true
    $kaboom = explode(".", $eventFlyer); // Split file name into an array using the dot
    $fileExt = end($kaboom); // Now target the last array element to get the file extension

    if(!$fileTmpLoc)
    {
        $error =  "Please insert ALL fields";
    }
    elseif($fileSize > 2097152 )
    {
        $error =  "ERROR: Your file was larger than 2 Megabytes in size.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    }
    elseif(!preg_match("/.(gif|jpg|png)$/i", $image))
    {
        $error =  "ERROR: Your image was not .gif, .jpg, or .png.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    }
    else if ($fileErrorMsg == 1)
    {
        $error =  "ERROR: An error occured while processing the file. Try  again.";
    }
    else
    {

        # move the file to a folder
        $moveResult = move_uploaded_file($fileTmpLoc,  "you file directory ex(../img/users/$username)");

        if($moveResult != true) // there was an error uploading the file to the folder
        {
            $error =  "ERROR: File not uploaded. Try again.";
            unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
        }

答案 2 :(得分:0)

我建议将图像保存在后备存储中,即数据库。

这样做,图像与用户“密切相关”。一旦用户名更改,它就不会变得不相关。

答案 3 :(得分:0)

  1. 首先必须创建文件夹
  2. 将图片复制到新生成的文件夹。
  3. 假设您已将用户数据退出到$userdata变量。你可以制作这样的文件夹 我假设您的default_picusers位于同一目录中。

     $new_directory = 'users/'.$userdata['username'];
     mkdir($new_directory,0777);
     copy('default-profile_pic.png',$new_directory.'/default-profile_pic.png');