I am uploading files via php. The amount of files can be uploaded is determined by a value which is stored in database. So let's say, this value is 3, than my code show three html file input. This is working. I could also rename one file too. working extension filter, and size restrictions.. But when I tried to do this with more than one, I stacked... The new name should be the 'number of list element'-'base filename'. Example:
1-thisisanimage.jpg
2-anotherimage.jpg
3-andthisalso.jpg
I tried with foreach but I think I over-complicated. I also tried different scripts from the internet but none of them really what I just cannot do this. Anybody? :)
EDIT: added the part of my code
if (!file_exists("uploads/".$date."-".$email."-vid-".$videoID)) {
mkdir("uploads/".$date."-".$email."-vid-".$videoID."/", 0777, true);
}
$imagecounter = 0;
$_FILES["file"] = array();
foreach ($_FILES["file"] as $file) {
$imagecounter++;
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.jpg','.jpeg','.png','.gif');
if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
{
// Rename file
$newfilename = "$imagecounter-".$file_basename . $file_ext;
if (file_exists("uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename))
{
// file already exists error
echo "You have already uploaded this file.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename);
echo "File uploaded successfully.";
}
}