Jump to content

php file uploads - "blank" jpgs attached to email


rachelm

Recommended Posts

I have built a php enquiry form with a file upload feature. Previously, emails have come through this form with jpg attachments appearing normal. The last 2 enquiries have come through with jpgs that appear "blank" even though the filesizes were 57kb and 296kb. I guess they are being corrupted somewhere.

 

I have sent through a couple of tests with jpgs around 91kb and they worked fine... what might be causing some image attachments to fail?

The only thing I am checking is filesize, restricting it to 1mb.

 

The HTML is basically:

 

<form action="sendtest.php" method="post" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />

<input type="file" name="myfile" />

</form>

 

 

The code in sendtest.php:

 

if($_FILES['myfile']['size'] == 0){

die("Image upload failed. Please try again.");

}

if($_FILES['myfile']['size'] > 1048576){

die("The uploaded image exceeds maximum file size.");

}

 

$to = "[email protected]";

$message = "testing message text";

        $fileatt = $_FILES['myfile']['tmp_name'];

        $fileatttype = "image/jpg";

        $fileattname = "project.jpg";

        $headers = "From: $email";

        $file = fopen( $fileatt, 'rb' );

        $data = fread( $file, filesize( $fileatt ) );

        fclose( $file );

$semi_rand = md5( time() );

        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

   

        $headers .= "\nMIME-Version: 1.0\n" .

                    "Content-Type: multipart/mixed;\n" .

                    " boundary=\"{$mime_boundary}\"";

   

        $whole_message =

                "--{$mime_boundary}\n" .

                "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .

                "Content-Transfer-Encoding: 7bit\n\n" .

                $message."\n\n";

   

        $data = chunk_split( base64_encode( $data ) );

               

        $whole_message .= "--{$mime_boundary}\n" .

                "Content-Type: {$fileatttype};\n" .

                " name=\"{$fileattname}\"\n" .

                "Content-Disposition: attachment;\n" .

                " filename=\"{$fileattname}\"\n" .

                "Content-Transfer-Encoding: base64\n\n" .

                $data . "\n\n" .

                "--{$mime_boundary}--\n";

 

if( mail( $to, $subject, $whole_message, $headers )) {

            echo "<p>Thank you for your enquiry.</p>";

        }

        else {

            echo "<p>There was an error sending the mail.</p>";

        }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.