我正在尝试根据用户的用户名上传图片 我的意思是你可以在图片中看到有一个叫做kunz的用户 因此,当用户kunz上传图片时,我希望它在kunz文件夹中
$username =getfield('username'); //function to get fields
$location = 'user/$username; //this is the part that doesnt work
if (move_uploaded_file($tmp_name,$location.$name))
{
echo 'uploaded';
}else
{
echo 'there was an error';
}
答案 0 :(得分:0)
您的代码中似乎缺少某些内容。试试这个。
$username =getfield('username'); //function to get fields
$location = 'user/'.$username.'/'; //this is the part that doesnt work
if (move_uploaded_file($tmp_name,$location.$name))
{
echo 'uploaded';
}else
{
echo 'there was an error';
}
答案 1 :(得分:0)
首先确认$username
不为空。然后修改您的代码,如下所示。
<?php
$username = getfield('username'); //function to get fields
if(!empty(trim($username)))
{
$location = './user/'.$username;
if ( ! is_dir($location)) // check if directory present or not
{
mkdir($location, 0755); // if director not present, create directory with proper permissions
}
// then upload the file to the folder
if (move_uploaded_file($tmp_name,$location.'/'.$name))
{
echo 'uploaded';
}
else
{
echo 'there was an error';
}
}
else
{
echo "Error: Username empty.";
}
?>
答案 2 :(得分:0)
每当你给目的地然后检查它是否可写和第二件事的位置,给出路径如下: -
$location='./user/'.$username.'/';
这样它应该从根文件夹中获取路径....
和
上传
move_uploaded_file($tmp_name,$location)
使用此声明。