Create New Folder With PHP

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
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Create New Folder With PHP

Post by JustinMs66 »

ok so i have an existing and working file upload script, but i want to be able to, in the HTML doc where you select the file to upload, be able to either select an existing folder to upload to, OR create a new folder, and then be able to upload to the newly created folder.

here is my existing PHP:

Code: Select all

<?php
$bad_types = array('application/octet-stream','application/x-msdos-program','application/x');
if( in_array( $_FILES['uploadedfile']['type'], $bad_types ) )
{
    echo "That File type is not supported.";
}
else
{
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']).
        " has been uploaded. here is the link to your file: <a href=uploads/".  basename( $_FILES['uploadedfile']['name']). ">".  basename( $_FILES['uploadedfile']['name'])."</a>";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
and here is my existing HTML:

Code: Select all

<body>
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='file' name='uploadedfile' /><br />
<input type='submit' value='Upload File' />
</form>
</body>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mkdir() is the general function to use, often in conjunction with chmod().
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok....so how to i put that to use with code....?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The linked documentation pages provide example usage. It may also be a good idea to read user comments from the pages as well.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

If you want the folder to be created while uploading file here is the customized code of yours :

Code: Select all

<?php
$bad_types = array('application/octet-stream','application/x-msdos-program','application/x');
if( in_array( $_FILES['uploadedfile']['type'], $bad_types ) )
{
    echo "That File type is not supported.";
}
else
{
$target_path = "uploads/";

if( !file_exists($target_path)){
    mkdir($target_path, 0700); //create directory with chmod 0700

}
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']).
        " has been uploaded. here is the link to your file: <a href=uploads/".  basename( $_FILES['uploadedfile']['name']). ">".  basename( $_FILES['uploadedfile']['name'])."</a>";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

hm...i kind of get it. i actually want to have a "name" box and a "create" button and have the folder name be the text that is in the "name" box.

i also want a dropdown box that displays all of the folders that people have created.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

So you want to create a folder whatever user creates and what is that list for ? Do you want to upload the files on the selected folder in the list ?
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

dibyendrah wrote:So you want to create a folder whatever user creates and what is that list for ?
yes...and it's for so that you can upload to a specific folder.
dibyendrah wrote: Do you want to upload the files on the selected folder in the list ?
yes exactly.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

...plz help?? :( :( :(
Post Reply