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
chrismicro
Forum Newbie
Posts: 21 Joined: Fri Nov 12, 2010 7:03 am
Post
by chrismicro » Fri Nov 12, 2010 7:13 am
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?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 12, 2010 11:49 am
Use
copy() instead of move_uploaded_file()?
chrismicro
Forum Newbie
Posts: 21 Joined: Fri Nov 12, 2010 7:03 am
Post
by chrismicro » Sat Nov 13, 2010 7:05 am
I am not sure i Understand, where do I put the 2nd target path?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Sat Nov 13, 2010 7:11 am
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
Post
by chrismicro » Sat Nov 13, 2010 8:18 am
sweet works awesome, your the man
chrismicro
Forum Newbie
Posts: 21 Joined: Fri Nov 12, 2010 7:03 am
Post
by chrismicro » Sun Nov 14, 2010 9:24 pm
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?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Nov 15, 2010 6:06 am
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
Post
by chrismicro » Mon Nov 15, 2010 10:28 am
could you show me code wise, so i can better understand?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Nov 15, 2010 10:55 am
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
Post
by chrismicro » Tue Nov 16, 2010 6:07 pm
what about just giving an error instead of renaming the file?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Nov 16, 2010 6:56 pm
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?