librarion
librarion

Reputation: 147

Create destination folder on file upload with same name as filename

I've been looking at various PHP/AJAX file upload plugins online, but I'm having some trouble finding one feature I really need. For the purposes of this upload, each file that I upload must go into a directory with the same name as that file (minus the extension). Naturally, the nicest way would be to create that folder during the upload and then send the file to it. I know this involves mkdir() in some way, and I've found a number of scripts that even perform basic folder creation, but I'm not clear on how to do this dynamically using the filename. Any ideas?

Thanks!

Upvotes: 0

Views: 1091

Answers (2)

Liam Bailey
Liam Bailey

Reputation: 5905

When you upload a file in php it is stored in the $_FILES array, its name is stored in $_FILES['inputfield']['name'] where 'inputfield' is the name in the file input like:

<input type='file' name='inputfield' />

So then you would do:

$exp = explode(".",$_FILES['inputfield']['name']);
$filename = $exp[0];
$path = "/path/to/base/folder/" . $filename . "/" . $_FILES['inputfield']['name'];
move_uploaded_file($_FILES['inputfield']['tmp_name'], $path);

Upvotes: 1

Pit Digger
Pit Digger

Reputation: 9800

$fileName =   $_FILES['fieldname']['name']
$foldername = substr($fileName, 0, strrpos($fileName, '.')); 

Upvotes: 0

Related Questions