3

I'm creating my first website ever, which is for my custom tshirt business. I've created a form that allows a user to submit shirt constraints, which is emailed to me upon submission.

Everything is working except for the image, which only shows the file name in text. I'm guessing that I have to specify that its a file and not text?

I've looked around for an answer similar to this, but cannot find any posts with my specific problem.

Here's what I'm using and getting text only. All other variables are set up this way then called in a string variable, $message. Sorry for the noob question

<input type="file" multiple="multiple" name="fileUpload"><br>
$Image = $_POST["fileUpload"];
Jake Stewart
  • 143
  • 2
  • 12
  • 1
    Take a look at `$_FILES` instead of `$_POST`. – arkascha Jun 27 '16 at 03:51
  • Also consider using a web page toolkit like Jimdo. It will save you _a lot_ of hassle allowing you to invest your energy into more useful areas. They also allow you to operate a safe online shop. – arkascha Jun 27 '16 at 03:52

2 Answers2

3

Assuming you simply want to embed the picture, not send it as an attachment.

PHP uploaded files are not retrieved from $_POST array, but from $_FILE array.

Try this snippet:

$imageFile = $_FILES["fileToUpload"]["tmp_name"];
$imgEncoded = base64_encode(file_get_contents($imageFile));

and then, retrieve use it:

echo "<img alt='Embedded Image' src='data:image/png;base64,$imgEncoded' />";

or whatever your specific use case is.

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
Wahib Mkadmi
  • 627
  • 2
  • 9
  • 26
0

Alright, so here's what you'll want to do...

  1. Select the image with HTML and submit it to a PHP script using a <form>.
  2. Use $_FILES to grab the image data inside of the PHP script and upload the image to your server.
  3. Create an HTML email with the location of that image inside an tag.
  4. Send the email with mail().

First create the HTLM page that the user will upload their image from...

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="uploaded_file">
    <input type="submit" value="Upload Image" name="submit">
</form>

When they click "Upload Image", the file data will be sent to "upload.php". This script will upload the file to your server and then send the email...

<?php

//Upload the file to the root directory

$image_name = $_FILES['uploaded_file']['name'];
$image_temp_name = $_FILES['uploaded_file']['tmp_name'];

move_uploaded_file($image_temp_name, "/$image_name");

//Send the email

$to = "your email address here";
$subject = "email subject here";
$body = "<img src='http://yourwebsite.com/$image_name'>";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n;";

$mail = mail($to, $subject, $body, $headers);
if(!$mail){
    echo "Error!";
} else {
    echo "Your email was sent successfully.";
}

?>