How to create a folder and copy files into the folder?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

How to create a folder and copy files into the folder?

Post by Tassadduq »

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 just create it, but it didn't, so how can I do this? This is my script:

Code: Select all

<?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 } ?>
please some one help me???
User avatar
rlg0613
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 10:15 am

Re: How to create a folder and copy files into the folder?

Post by rlg0613 »

How about...

Code: Select all

 
 
//this is where the pic is put into a folder by the username
  $dirPath = "pics/profiles/" . $userName2 . "/";
  $response = @makeDirectory($new_directory);
  if ($response != TRUE) {
    $msg = "Folder ($dirPath) NOT created";
  }
  else {
  //  Your copy code...
  }
//  Functions
 
function makeDirectory($dirPath){
  if( !is_dir( $dirPath ) ){
    if ( mkdir( $dirPath, 0700 ) ){
      return true;
    }else{
      return false;
    }
  }
}
 
 
Also make sure you have write privileges to the site.
Post Reply