Sathish Kumar Sathish Kumar - 3 months ago 9
PHP Question

PHP - How to upload a file in more than one directories

How can I upload a single file (ex.sample.jpg) in more than one folder (ex. folder1 and folder2) using php

I tried using for loop but it wont work its move the file (sample.jpg) to the first folder (folder1) only, while moving the same file to the second folder (folder2) it throws an error

Answer

You can create a copy of the before moving it to the first directory, then move the copy into the second directory.

Use the copy() function to copy the file.

Your code should look something like this -

$firstDestination = "path/to/your/firstDirectory/" . $_FILES['userfile']['name'];
$secondDestination = "path/to/your/secondDirectory/";

move_uploaded_file($_FILES['file']['tmp_name'], $firstDestination );
copy($firstDestination, $secondDestination);