开发者

create new folder with php

开发者 https://www.devze.com 2023-01-09 09:13 出处:网络
I have a file upload script that works [finally] but want to have users submit pics into a folder, which I want to be created as the users name. I was thinking I could just maybe submit the file and t

I have a file upload script that works [finally] but want to have users submit pics into a folder, which I want to be created as the users name. I was thinking I could just maybe submit the file and then it would jsut create it, but it didn't, so how can I do this? This is my script: PHP Code:

<?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 "";  
       开发者_如何学Python 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 } ?>

is there any way to do this? Maybe even when the user is made? thanks!


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


You can further check if the directory you're within permits you to make new directories. Here are a few validation examples for you to improve the robustness of your app.

$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


The answer to "How to create a folder with PHP" is mkdir(). I can't understand what all that code has to do with the subject.


Maybe you should take a look at the mkdir command.


this should do it if(!file_exists($targetPath)) mkdir($targetPath); but you need to have the right permissions

0

精彩评论

暂无评论...
验证码 换一张
取 消