Uploading audio to 2 separate folders:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

Uploading audio to 2 separate folders:

Post by chrismicro »

I have created an uploader script I need it to upload to 2 separate folders on the system:

here is my code for uploading to one folder:

Code: Select all

$target_path = "/var/lib/asterisk/sounds/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$filename = $_FILES['uploadedfile']['name'];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";


} else{
    echo "There was an error uploading the file, please try again!";

}
I also need it to upload to

Code: Select all

/srv/www/htdocs/k2zwf6jks9yx6zhs6vr06842jj51fx/
how do i do this?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uploading audio to 2 separate folders:

Post by Celauran »

Use copy() instead of move_uploaded_file()?
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

Re: Uploading audio to 2 separate folders:

Post by chrismicro »

I am not sure i Understand, where do I put the 2nd target path?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uploading audio to 2 separate folders:

Post by Celauran »

Either call the copy function twice, or copy once followed by move_uploaded_file once.

Code: Select all

$target_path = "/var/lib/asterisk/sounds/";
$backup_path = "/srv/www/htdocs/k2zwf6jks9yx6zhs6vr06842jj51fx/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$backup_path .= basename( $_FILES['uploadedfile']['name']);
$filename = $_FILES['uploadedfile']['name'];
if (copy($_FILES['uploadedfile']['tmp_name'], $target_path) && copy($_FILES['uploadedfile']['tmp_name'], $backup_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";

}
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

Re: Uploading audio to 2 separate folders:

Post by chrismicro »

sweet works awesome, your the man
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

Re: Uploading audio to 2 separate folders:

Post by chrismicro »

how can i take the code above and make it so that if i upload a file of the same name it will not do this and show an error?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uploading audio to 2 separate folders:

Post by Celauran »

Depends what you want to happen to the existing file. You could try using shell_exec() to rename the existing file, or you could rename the new file, say by appending .1 to the end.
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

Re: Uploading audio to 2 separate folders:

Post by chrismicro »

could you show me code wise, so i can better understand?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uploading audio to 2 separate folders:

Post by Celauran »

Rename the existing file using shell_exec:

Code: Select all

if (file_exists($target_path . $filename))
{
    shell_exec("mv " . $target_path . $filename . " " . $target_path . $filename . ".1");
}
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

Re: Uploading audio to 2 separate folders:

Post by chrismicro »

what about just giving an error instead of renaming the file?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uploading audio to 2 separate folders:

Post by Celauran »

Code: Select all

if (file_exists($target_path . $filename))
{
    echo "Error:  A file called {$filename} already exists in {$target_path}";
}
Or am I not understanding your question?
Post Reply