Sathish Kumar Sathish Kumar - 1 year ago 68
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download