我有一个文件上传脚本,可以[终于]工作,但希望让用户将图片提交到一个文件夹中,我希望将其创建为用户名。我以为我可能只是提交文件然后它会创建它,但它没有,所以我怎么能这样做?这是我的脚本: PHP代码:
<?php
// Configuration part
$dbhost = "localhost"; // Database host
$dbname = "users"; // Database name
$dbuser = "dbuser"; // Database username
$dbpass = ""; // Database password
// Connect to database
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Error: Couldn't connect to database");
mysql_select_db($dbname, $db) or die("Error: Couldn't select database.");
if($IsLoggedIn)
{ $userid = $_GET["userid"];
if($_POST['Submit'])
{
if($userid)
{ $uploadName = mysql_query("SELECT * FROM users WHERE userId ='"
. $userid . "'");
while ($upName = mysql_fetch_row($uploadName))
{ $userName2 = $upName[1];
$userPic1 = $upName[11];
echo "$userName2"; } }
//If the Submitbutton was pressed do:
if ($_FILES['imagefile']['type'] == 'image/pjpeg')
{
//this is where the pic is put into a folder by the username
copy ($_FILES['imagefile']['tmp_name'],
"pics/profiles/" . $userName2 . "/" . $_FILES['imagefile']['name'])
or die ("Could not copy");
echo "";
echo "Name: ".$_FILES['imagefile']['name']."";
echo "Size: ".$_FILES['imagefile']['size']."";
echo "Type: ".$_FILES['imagefile']['type']."";
echo "Copy Done....";
}
else {
echo "";
echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")";
}
}
?>
<form name="form1" method="post" action="index.php?page=classupload&userid=<?php echo "$userid"; ?>" enctype="multipart/form-data">
<input type="file" name="imagefile">
<input type="submit" name="Submit" value="Submit">
</form>
<?php } else { ?>How did you get here? You aren't <a href="index.php?page=login">Logged In</a>.
<?php } ?>
有没有办法做到这一点?也许即使用户制作?谢谢!
答案 0 :(得分:3)
$directoryPath = "/home/domain/public_html/site_name/pics/profiles/".$userName2;
mkdir($directoryPath, 0644);
答案 1 :(得分:3)
您可以进一步检查您所在的目录是否允许您创建新目录。 以下是一些验证示例,可帮助您提高应用的稳健性。
$parentDir = '/home/domain/public_html/site_name/pics/profiles/';
if(!is_dir($parentDir)) { // Check if the parent directory is a directory
die('Invalid path specified');
}
if(!is_writable($parentDir)) { // Check if the parent directory is writeable
die('Unable to create directory, permissions denied.');
}
if(mkdir($parentDir . $userName2) === false) { // Create the directory
die('Problems creating directory.');
}
die('Created directory successfully'); // Success point
答案 2 :(得分:2)
“如何使用PHP创建文件夹”的答案是mkdir()。我无法理解所有代码与主题有什么关系。
答案 3 :(得分:1)
也许你应该看一下mkdir命令。
答案 4 :(得分:0)
这应该if(!file_exists($targetPath)) mkdir($targetPath);
,但你需要拥有正确的权限