开发者

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

开发者 https://www.devze.com 2023-03-17 13:54 出处:网络
How can I upload a single file (ex.sample.jpg) in more than one folder (ex.开发者_如何学C folder1 and folder2) using php

How can I upload a single file (ex.sample.jpg) in more than one folder (ex.开发者_如何学C 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


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);


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');


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);


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


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


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 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消