Sathish Kumar
Sathish Kumar

Reputation: 369

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

Upvotes: 2

Views: 4524

Answers (6)

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

You can create a copy of the file 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);

Upvotes: 3

Gulfam Khan
Gulfam Khan

Reputation: 1

You can't store by calling move_uploaded_file() more than one for a single file. But here is a good news; you can copy the uploaded file at to multiple directories like

if(move_uploaded_file()){
    $main_file = first_directory+file name;
    $copy_file = second_directory+file name; 
    copy ($main_file, $copy_file); #put both parameters in copy function 
} 

Upvotes: 0

Ekene J
Ekene J

Reputation: 1

You can use something like this Html form

<form action="createsubfiles.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile"/>
<input type="submit" name="uploadthefile" value="Upload" class="btn btn-info"/>
</form>

The php

    foreach ($instanceof->somemethod($sn) as $directory){
            extract($directory);
    $target_dir = $parent/$child/filename.php;
    if( copy($_FILES['uploadfile']["tmp_name"], $target_dir)){

            echo "$target_dir - created<br/>";
       }
            else{
           echo "Directory cuold not be created<br/>";
       }
}

Do this without using file upload

Upvotes: 0

gingerCodeNinja
gingerCodeNinja

Reputation: 1233

Do you have a small coding example/sample?

You will likely need to move it to the first folder, then copy it to subsequent folders.

If you're using move_uploaded_file(), it moves, not copies, so once you call it the first time it no longer exists in it's original location.

http://uk.php.net/manual/en/function.move-uploaded-file.php

Upvotes: 0

christopher_b
christopher_b

Reputation: 958

After you move the file the first time, you will then need to copy it from the new location into the other locations.

$fileDestination = '/my/path';
$newDestination = '/my/other/path';
move_uploaded_file($_FILES['upload']['tmp_name'], $destination);
copy($destination, $newDestination);

Upvotes: 1

Sascha Galley
Sascha Galley

Reputation: 16091

try to copy the file, like this:

move_uploaded_file($_FILES['file']['tmp_name'], 'folder1/ex.sample.jpg');
copy('folder1/ex.sample.jpg', 'folder2/ex.sample.jpg');

Upvotes: 1

Related Questions