PHP form throws fatal error in version 8.01 when trying to use the count function for file upload. Data in not a countable function:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array
Changing count to isset took care of the error but now the response email sent by the form is not encoded correctly. The and base64 encoding in the email appear in the email.
This was the code I had that worked before version 8.01:
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet stream;name="".basename($files[$i]).""n" .
"Content-Description: ".basename($files[$i])."n" .
"Content-Disposition: attachment;n" . " filename="".basename($files[$i]).""; size=".filesize($files[$i]).";n" .
"Content-Transfer-Encoding: base64nn" . $data . "nn";
}
}
}
So I tried casting the $files to an array:
$files = (array) $files;
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name="".basename($files[$i]).""n" .
"Content-Description: ".basename($files[$i])."n" .
"Content-Disposition: attachment;n" . " filename="".basename($files[$i]).""; size=".filesize($files[$i]).";n" .
"Content-Transfer-Encoding: base64nn" . $data . "nn";
}
}
}
This cleared the error too but produced emails with html code appearing in the email
So then I tried this:
if(isset($files)){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name="".basename($files[$i]).""n" .
"Content-Description: ".basename($files[$i])."n" .
"Content-Disposition: attachment;n" . " filename="".basename($files[$i]).""; size=".filesize($files[$i]).";n" .
"Content-Transfer-Encoding: base64nn" . $data . "nn";
}
}
}
It works as far as getting the uploaded file into the temp directory and moving it to the correct folder. It also doesn’t throw a fatal error, but the email response email looks like this:
–==Multipart_Boundary_xe5853c74b9fb3bc6378b9c0706e7e587x
Content-Type: text/html; charset=”UTF-8″
Content-Transfer-Encoding: 7bit
Thank you No Name for the print quote or order. You will receive another email with your prices or if a quote was not required an email updating the status and delivery date. If you have any questions email [email protected] or call Anybody at xxx-xxx-xxxx”
–==Multipart_Boundary_xe5853c74b9fb3bc6378b9c0706e7e587x–
And the form email looks like this:
–==Multipart_Boundary_xe5853c74b9fb3bc6378b9c0706e7e587x
Content-Type: text/html; charset=”UTF-8″
Content-Transfer-Encoding: 7bit
Customer
Date: 04/25/2024
Ordered By: Name on Order
Department: test
Phone: XXX-XXX-XXXX
Email: [email protected]
Expense Code: 75
Cost Code:000000/75
Cost Code not Listed:test
Needed By: 05/08/2024
Mail Drop: Plaza
Print Specs
Quantity: 100
Print type: color
Item: test.pdf
Size: 5.5×8.5
Paper size: 70#offset
Fold: halffold
Second Print Specs
–==Multipart_Boundary_xe5853c74b9fb3bc6378b9c0706e7e587x
Content-Type: application/octet-stream; name=”Screenshot 2024-03-04 174848.png”
Content-Description: Screenshot 2024-03-04 174848.png
Content-Disposition: attachment;
filename=”Screenshot 2024-03-04 174848.png”; size=231147;
Content-Transfer-Encoding: base64
iVBORw0KGgoAAAANSUhEUgAABYMAAAK0CAYAAABRKiIYAAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABjaVRYdFNuaXBNZXRhZGF0YQAAAAAAeyJjbGlw…..
–==Multipart_Boundary_xe5853c74b9fb3bc6378b9c0706e7e587x–
Not sure why the change in count has affected the email body, but hoping someone here can lead me in the right direction. Thanks everyone for all the help.