Hi

I've just got a nice php form done and I now need my visitors to be able to upload pics and have them sent with the form data. Is that possible in a php form, if so, can anybody point me in the right direction.

Many thanks.

LEE

Dani AI

Generated

Short answer: yes — a PHP form can accept photo uploads and those files can be mailed, but upload handling and email composition are two distinct steps. This thread already points you in the right direction (thanks for recommending a mail library and for the server-storage approach). Below are the practical, non-duplicative points missing from the existing replies.

Workflow to follow

  1. accept the files from the form and place them into a temporary storage area you control;
  2. validate each file (size, true image type, dimensions);
  3. move/rename to a safe location (use unpredictable filenames and keep outside the web root);
  4. attach/send the files with your mail-sending code or library (recipient email is supplied to the mail step — i.e., whatever function or library call you use to actually send mail is where the “to” address goes);
  5. clean up temporary files after successful send (or keep them and send links instead).

Key practical notes

  • Check and adjust PHP limits: upload_max_filesize, post_max_size, max_file_uploads and memory_limit — these determine whether larger or multiple images will even arrive.
  • Email size matters: attachments are base64-encoded (roughly +33% overhead) and many mail providers reject large messages (commonly ~20–25MB). For many photos prefer uploading and emailing links instead of attaching every image.
  • Avoid building MIME attachments by hand — use a maintained mail library (as suggested earlier in the thread) so you don’t get headers/encoding wrong.

Security & troubleshooting checklist

  • Do server-side checks (use file info functions to confirm images, enforce extension/MIME whitelist and a sensible size limit).
  • Sanitize filenames, store outside webroot, and set safe permissions.
  • Check UPLOADERR* return codes, tmp directory permissions, and webserver/PHP error logs when uploads fail.
  • If sending many or large files, queue the mail send to a background job to avoid timeouts.

These steps fill the gap between saving uploaded images (what showed) and actually delivering them by email (what / hinted at).

Recommended Answers

All 8 Replies

I use a free script called phpMailer which lets you do attachments really easily as well as embedded images.

The docs and examples all go with it but if you get stuck just post back here.

I use a free script called phpMailer which lets you do attachments really easily as well as embedded images.

The docs and examples all go with it but if you get stuck just post back here.

Thanks for the link. :)

leroytrolley, if you wish to design your own image uploader or mailer, then I would suggest reading about file uploads. The form you have should probably have some sort of limit on file size or type, like to only allow jpg and gif formats or something. Otherwise, I guess the script listed above would do nicely. Why don't you check out the PHP.Net file uploads page if you want to create your own image upload console: http://us2.php.net/features.file-upload

Good luck.

thanks. but it was a little to advanced for me.

what do I need to add to the attached php code to be able to process image uploads (about 10 images per email).

Leroy

Okay, you have a form, right? Within the form, you add a series of file inputs. Something like:

<form action="imgUploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="photo[]" /><br />
<input type="file" name="photo[]" /><br />
<input type="file" name="photo[]" /><br />
<br />
<input type="submit" value="Upload 'Em!" />
</form>

If you wish, you could do a JavaScript text append thing to where a user clicks a button, and the browser will dynamically add another file input. Anyways, to process these uploads, you of course need something server-side. So, you make your PHP script. Something basic like this should do:

<?php 
foreach ($_FILES['photo']['error'] as $key => $error) { //For each image...
   if ($error == UPLOAD_ERR_OK) { //If no errors...
		$tmp_name = $_FILES['photo']['tmp_name'][$key];
		$name = $_FILES['photo']['name'][$key];
		  move_uploaded_file($tmp_name, "images/$name"); //Move image
  }  
}

Thanks and compliments to the grand PHP manual that I just happen to have. This script should run with PHP 3 and above, but you also have to have a reasonable browser that will handle these uploads. Now, I am not any type of expert on file uploads, as I haven't really messed around with them. But I do know that the form enctype is required, and that the name must have the "[]" thing. This is what creates the array of files; it is required.

Well, I hope this is what you're looking for. Maybe somebody else can help. If not, you may always go to Google and type your query as, "PHP image upload example." And again, good luck.

Many many thanks, but how does the php code know where to send the photos to i.e I cant see where I need put the email adress to send them to.

Sorry if I sound really thick. I'm just a humble designer who can't code for toffee.

Leroy

Well, the script above will only work to put the images on your server, like an image directory. It will take some modifications to make it function when sending an image in an email, but I believe you could do something to where when the image is uploaded, the link to that image is stored in a variable, and when you send the email, all you do is say here is image one titled "Andrew" and then place in the variable which then displays the link for you or your user to see and use.

the first responder told you all u needed there are scripts called mime classes or something which enable you to send emails reeeealy easy! otherwise
it would be

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.