How do I upload files and move them to their perspective folder using php, I'm trying to make the script automatically upload
images such as jpg and png to jpg/ folder mp3 to mp3/ folder and mp4 flv mpeg to mp4 folder
<?php
$ds = DIRECTORY_SEPARATOR;
$jpg = "jpg/";
$mp3 = "mp3/";
$mp4 = "mp4/";
if (!empty($_FILES))
{
if(substr($_FILES['file']['name'], -3) == "mp3")
{
$destination = substr($_FILES['file']['name'], -3);
$file = $_FILES['file']['name'];
echo "this : " . $mp3,$destination . $ds;
move_uploaded_file($mp3,$destination . $ds ) or die("mp3 error");
echo "uploaded : " . $file . "<br>";
}
else if(substr($_FILES['file']['name'], -3) == "mp4" or substr($_FILES['file']['name'], -3) == "mp4" or substr($_FILES['file']['name'], -4) == "mpeg" )
{
$destination = substr($_FILES['file']['name'], -3);
$file = $_FILES['file']['name'];
move_uploaded_file($mp4,$destination . $ds ) or die("mp4 error");
echo "uploaded : " . $file . "<br>";
}
else if(substr($_FILES['file']['name'], -3) == "jpg" or substr($_FILES['file']['name'], -3) == "png")
{
$destination = substr($_FILES['file']['name'], -3);
$file = $_FILES['file']['name'];
move_uploaded_file($jpg,$destination . $ds ) or die("jpg error");
echo "uploaded : " . $file . "<br>";
}
else
{
}
}
?>
Read move_uploaded_file
manual.
First argument is "The filename of the uploaded file.". And you pass what:
move_uploaded_file($mp3,$destination . $ds )
$mp3
is a folder mp3
.
Usually you need to pass $_FILES['file']['tmp_name']
as first argument.
Second note - concatenation $destination . $ds
returns what?
In case of music.mp3
file it will be mp3/
.
So you call move_uploaded_file
with arguments:
move_uploaded_file('mp3/', 'mp3/');
Do you really want this?
So, the simpliest fix is (for an mp3 case):
move_uploaded_file($_FILES['file']['tmp_name'], $mp3 . $_FILES['file']['name']);
In this case file will be saved in mp3
folder on the same level as your script with file's original name.
Also make sure that mp3
folder exists and has proper permissions.