Rene
Rene

Reputation: 642

Files uploaded into subfolders based on their filenames

I'm a n00b and I need some help figuring out this issue, I would be grateful for all the help that I can get.

So…the idea is when a user uploads for example "Abiotic Stress in Plants.pdf", it is uploaded into a folder called "Book covers - pdf"(because of the .pdf extension) folder and has a subfolder called Abiotic Stress in Plants, which has the actual file inside. Thank you in advance.

private function get_subdir($file_name) {



        return implode('/', array_reverse(explode('.', $file_name))).'/';
    }

Upvotes: 0

Views: 195

Answers (1)

sascha
sascha

Reputation: 4690

Untested.

<?php
$prepend = 'Book covers - ';

$file = explode('.', $file_name);

if(!is_dir($prepend.$file[1]))
    mkdir($prepend.$file[1]);

if(!is_dir($prepend.file[1].'/'.$file[0]))
    mkdir($prepend.file[1].'/'.$file[0]);

move_uploaded_file($file_name, $prepend.file[1].'/'.$file[0]);

?>

This should create the folders, if they don't exist and move your file to it. Needs a bit modification, but should work I think.

Upvotes: 1

Related Questions