Mario Parra
Mario Parra

Reputation: 1554

Create new folder on upload based on date

I'm new to PHP and I've written a script to upload files to a server with an appended integer. Now, I'm trying to adjust the script to create a new folder and name it based on the date when a file is uploaded.

I think I know what I need to do, but I'm struggling with the syntax.

PHP:

if(!empty($_FILES)) {

    $temp = $_FILES['file']['tmp_name'];
    $dir_separator = DIRECTORY_SEPARATOR;
    $folder = "uploads";

    $uploaddate = date("m-d-y");
    if(!is_dir($uploaddate)) mkdir($uploaddate);

    $destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator;

    $target_path = $destination_path.(rand(10000, 99999)."_".$_FILES['file']['name']);
    move_uploaded_file($temp, $target_path);
}

Any help would be appreciated! Thanks.

Upvotes: 0

Views: 631

Answers (1)

der_michael
der_michael

Reputation: 3362

you should create the $destination_path variable first, create the folder properly afterwards. Like:

$uploaddate = date("m-d-y");

$destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;

if(!is_dir($destination_path)) mkdir($destination_path);

Or maybe you confused $folder and $destination_path with $target_path ;)

Upvotes: 1

Related Questions