当用户注册到我的网站时,我想创建一个文件夹,其中包含用户名和默认配置文件pic图像。我知道如何创建一个文件夹,但是如何创建一个包含文件的文件夹。
该文件夹应如下所示:
/users/pcoulson/
(pcoulson将是用户的用户名)
和../pcoulson/
应该包含默认的个人资料照片:
/users/pcoulson/default-profile_pic.png
如何使用 PHP
执行此操作答案 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)
假设您已将用户数据退出到$userdata
变量。你可以制作这样的文件夹
我假设您的default_pic
与users
位于同一目录中。
$new_directory = 'users/'.$userdata['username'];
mkdir($new_directory,0777);
copy('default-profile_pic.png',$new_directory.'/default-profile_pic.png');