使用php创建新文件夹

问题描述:

我有一个文件上传脚本,[终于],但想让用户提交图片到一个文件夹中,我想创建用户名。我想我可能只是提交文件,然后它会创建它,但它没有,所以我怎么能做到这一点?这是我的脚本: PHP代码:使用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 } ?> 

有没有办法做到这一点?甚至当用户被制造时谢谢!

+5

看着http://forums.devarticles.com/php-development-48/create-new-folder-with-php-4113.html,这个问题真的是什么? – Gumbo 2010-07-28 09:52:42

$directoryPath = "/home/domain/public_html/site_name/pics/profiles/".$userName2; 
mkdir($directoryPath, 0644); 
+0

如果它不起作用尝试将文件权限设置为0755 – Martin 2010-07-28 09:56:17

+0

谢谢 我试过这个: $ structure ='./users/'.$usercheck;如果(!mkdir($ structure,0,true)){ die('无法创建文件夹...'); } – 2010-07-28 10:13:25

也许你应该看看mkdir命令。

“如何用PHP创建文件夹”的答案是mkdir()。我无法理解所有的代码与这个主题有什么关系。

这应该这样做if(!file_exists($targetPath)) mkdir($targetPath);但你需要有正确的权限

您可以进一步检查,如果你允许范围内的目录你做出新的目录。 以下是一些验证示例,可以帮助您提高应用程序的稳健性。

$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