Page 1 of 1

Create New Folder With PHP

Posted: Mon Sep 04, 2006 10:48 pm
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>

Posted: Mon Sep 04, 2006 10:57 pm
by feyd
mkdir() is the general function to use, often in conjunction with chmod().

Posted: Mon Sep 04, 2006 11:55 pm
by JustinMs66
ok....so how to i put that to use with code....?

Posted: Tue Sep 05, 2006 12:05 am
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.

Posted: Tue Sep 05, 2006 12:10 am
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!";
    }
}
?>

Posted: Tue Sep 05, 2006 12:25 am
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.

Posted: Tue Sep 05, 2006 3:03 am
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 ?

Posted: Tue Sep 05, 2006 2:48 pm
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.

Posted: Wed Sep 06, 2006 5:43 pm
by JustinMs66
...plz help?? :( :( :(