Hello I'm writing PHP upload multiple files and I want to add ordinal number suffix to the filename.
but my for loop start from $i = 0
(because of array)
so I want to add suffix filename from 1 not zero by use $i + 1
on the line : $newname = "id".$id."_".date('Y-m-d')."_".$i+1.".".$file_extension;
but It's get syntax error
here is my code.
$count = count($_FILES["images"]["name"]);
$arr_newname = array();
for($i=0; $i <= $count; $i++){
if ((($_FILES["images"]["type"][$i] == "image/gif")
|| ($_FILES["images"]["type"][$i] == "image/jpeg")
|| ($_FILES["images"]["type"][$i] == "image/png")
|| ($_FILES["images"]["type"][$i] == ""))
&& ($_FILES["images"]["size"][$i] < 9000000)) //9 MB
{
$split = explode(".", $_FILES["images"]["name"][$i]);
$file_extension = end($split);
$newname = "id".$id."_".date('Y-m-d')."_".$i+1.".".$file_extension;
$arr_newname[$i] = $newname;
move_uploaded_file($_FILES["images"]["tmp_name"][$i] , "images/location/".$newname);
}
else{
echo "Invalid file;
}
}
for now, I solve problem by use for loop start from $i=1
and use $i-1
on array instead. but it's very ugly code for me.
for($i=1; $i <= $count; $i++){
if ((($_FILES["images"]["type"][$i-1] == "image/gif")
|| ($_FILES["images"]["type"][$i-1] == "image/jpeg")
|| ($_FILES["images"]["type"][$i-1] == "image/png")
|| ($_FILES["images"]["type"][$i-1] == ""))
&& ($_FILES["images"]["size"][$i-1] < 9000000)) //9 MB
{
$split = explode(".", $_FILES["images"]["name"][$i-1]);
$file_extension = end($split);
$newname = "id".$id."_".date('Y-m-d')."_".$i.".".$file_extension;
$arr_newname[$i-1] = $newname;
move_uploaded_file($_FILES["images"]["tmp_name"][$i-1] , "images/location/".$newname);
}
else{
echo "Invalid file;
}
}
So Is there other way to I use $i+1
on the $newname file? and why $i-1
work fine on those array