user5145953
user5145953

Reputation:

PHP move_uploaded_file multiple paths

I am working on an upload page. I need to send the same file upload (from the user) to various different paths for display.

The following work to upload the file to a single path:

    if(move_uploaded_file($fileTmpLoc, $filepath)){
    //Do something
}

Using move_uploaded_file I have tried the following but did not work:

if(move_uploaded_file($fileTmpLoc, ($filepath, $anotherfilepath))){
//Do something
} 


if(move_uploaded_file($fileTmpLoc, $filepath)){
//Do something
}
if(move_uploaded_file($fileTmpLoc, $anotherfilepath)){
//Do something
}

if(move_uploaded_file($fileTmpLoc, $filepath) &&     (move_uploaded_file($fileTmpLoc, $anotherfilepath)){
//Do something
}

Any suggestions would be helpful.

Upvotes: 0

Views: 1184

Answers (3)

ashok
ashok

Reputation: 81

move_uploaded_file will move the file, and not copy it -- which means it'll work only once.

i think you should user copy function after first copy like:

if(move_uploaded_file($fileTmpLoc,$filepath)){
//Do something
} 
if(copy($filepath, $filepath)){
//Do something
}
if(copy($filepath, $anotherfilepath)){
//Do something
}

Upvotes: 0

Ryan K
Ryan K

Reputation: 346

Try the following:

if(move_uploaded_file($tmp, $new_dest)){
    //moved to destination, now copy
    copy($new_dest, $another_new_dest);
}

if you need it to copy to more then one other

if(move_uploaded_file($tmp, $new_dest)){
    //moved to destination, now copy
    copy($new_dest, $another_new_dest);
    copy($new_dest, $yet_another_new_dest);
    //and so on...
}

Upvotes: 1

Mihai Matei
Mihai Matei

Reputation: 24276

My recommendation is to upload one single file to the server and to make it available on multiple paths via .htaccess. Something like:

RewriteRule ^/my-first-custom-path/(.*)$ /my-private-upload-directory/$1 [R=301,L]
RewriteRule ^/my-second-custom-path/(.*)$ /my-private-upload-directory/$1 [R=301,L]

Now, instead of accessing http://my-site.com/my-private-upload-directory/my-file.pdf you will be able to download the file from both paths http://my-site.com/my-first-custom-path/my-file.pdf and http://my-site.com/my-second-custom-path/my-file.pdf

Upvotes: 0

Related Questions