PHP version 8.01 count function gives fatal error, correction causes base64 encoding to not work in email

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật