Page 1 of 1

image upload 2 form send prob

Posted: Fri Dec 12, 2003 8:59 pm
by dev2761
I am posting again as I got told off for "Hijacking a thread".
I have a real problem with this!
The first part of the script works perfectly, (it might not be brilliant code but it does what I want) which is to upload an image, show it and then post a form which asks for the usual, who to ?, who from?, email... etc.
Then I have another page of code that sends the image, which supposedly works fine, I get the message "Mail sent! Yay PHP!", but in my email nothing, yes I recieve the mail but there's nothing there.
Now all I need to know is what and where do I need to put into this code so that it works, thank you?
Now that I've formulated the question properly I might get an answer!!!
upload.php

Code: Select all

<?php
$action = $_POST["action"];
$max_size = "1048576"; // Max size in BYTES (1MB)

echo "
//javascript and stylesheet here
<form action='$PHP_SELF' method=post name=f enctype='multipart/form-data' onsubmit="return check();">
<input type='file' name='filename' accept='image/jpg'>
<input type='hidden' name='action' value='upload'>
<input type='submit' value='Upload File'><br>
Image File jpg(max size: $max_size bytes/".($max_size/1024)." kb)
</form><br>
<DIV class=end></DIV>";


if ($action == 'upload')
{
	
	
	if ($_FILES["filename"]["size"] > $max_size) die ("<b>File too big!  Try again...</b>");
	
	
	copy($_FILES["filename"]["tmp_name"],"./".$_FILES["filename"]["name"]) or die("<b>Choose an image!!!</b>");
	echo "<b>File Uploaded.</b><br>"; // for debug -->  $filename --> ".$destination."/".$filename_name."</h2>";
	    echo "temp file: $filename_name<br>\n"; //$filename = temp_name : $filename_name = original_name
	    echo "<br>\n"; 
	    echo "<img src=./$filename_name><br>\n";
	    echo "<DIV class=end></DIV><br>\n"; 
	    echo "<br>\n";
	    //form send now loads
	    echo "<FORM name='myform' ACTION='send.php' METHOD='POST'>\n";
	    echo "Email To : <INPUT TYPE='TEXT' NAME='email_t'><br>\n";
	    echo "Your ForeName: <INPUT TYPE='TEXT' NAME='name'><br>\n";
	    echo "Your Surname: <INPUT TYPE='TEXT' NAME='surname'><br>\n";
	    echo "Your Email: <INPUT TYPE='TEXT' NAME='email_f'><br>\n";
	    echo "Subject: <INPUT TYPE='TEXT' NAME='subject' value'Oh Goddess'><br>\n";
	    echo "Message: <textarea name='message' rows='10' cols='40'></textarea>\n";
	    //echo "<INPUT TYPE='hidden' NAME='$filename_name'><br>\n";
	    echo "<INPUT TYPE='SUBMIT' VALUE='Send' NAME='sendit'>\n";
	    echo "<INPUT TYPE='SUBMIT' VALUE='Cancel' NAME='cancelit'><br>\n";
	    echo "</form>\n";
	    echo "<br>\n";
	    echo "<br>\n";
}
?>
send.php many thanks to Bech100 for this well formulated script!!!

Code: Select all

<?php
// Read POST request params into global vars 
$to      = $_POST['email_t']; 
$from    = $_POST['email_f']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 

// Obtain file upload vars 
$fileatt      = $_FILES['filename']['tmp_name']; 
$fileatt_type = $_FILES['filename']['type']; 
$fileatt_name = $_FILES['filename']['name']; 

$headers = "From: $from"; 

if (is_uploaded_file($fileatt)) { 
  // Read the file to be attached ('rb' = read binary) 
  $file = fopen($fileatt,'rb'); 
  $data = fread($file,filesize($fileatt)); 
  fclose($file); 

  // Generate a boundary string 
  $semi_rand = md5(time()); 
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
  
  // Add the headers for a file attachment 
  $headers .= "\nMIME-Version: 1.0\n" . 
              "Content-Type: multipart/mixed;\n" . 
              " boundary="{$mime_boundary}""; 

  // Add a multipart boundary above the plain message 
  $message = "This is a multi-part message in MIME format.\n\n" . 
             "--{$mime_boundary}\n" . 
             "Content-Type: text/plain; charset="iso-8859-1"\n" . 
             "Content-Transfer-Encoding: 7bit\n\n" . 
             $message . "\n\n"; 

  // Base64 encode the file data 
  $data = chunk_split(base64_encode($data)); 

  // Add file attachment to the message 
  $message .= "--{$mime_boundary}\n" . 
              "Content-Type: {$fileatt_type};\n" . 
              " name="{$fileatt_name}"\n" . 
              //"Content-Disposition: attachment;\n" . 
              //" filename="{$fileatt_name}"\n" . 
              "Content-Transfer-Encoding: base64\n\n" . 
              $data . "\n\n" . 
              "--{$mime_boundary}--\n"; 
} 

// Send the message 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
  echo "<p>Mail sent! Yay PHP!</p>"; 
} else { 
  echo "<p>Mail could not be sent. Sorry!</p>"; 
} 
?>

Posted: Fri Dec 12, 2003 9:14 pm
by infolock
I think it's because this has been posted already in this thread :

viewtopic.php?t=12683&highlight=javascript+stylesheet

Posted: Fri Dec 12, 2003 11:29 pm
by dev2761
I know that, buuuut because that wasn't "my thread/post" nobody replied and the answer isn't in there.
I realise that the answer is something along the lines of $_POST['filename']; or something like that its just that I cannot seem to find it. I've tried 'name', 'tmp_name', 'temp_name', everything I can think of and nothing works. I even found a script on an Indian PHP Forum that promised to do exactly what I wanted and it didn't work.

Posted: Fri Dec 12, 2003 11:41 pm
by infolock

Code: Select all

echo " 
//javascript and stylesheet here 
<form action='$PHP_SELF' method=post name=f enctype='multipart/form-data' onsubmit="return check();"> 
<input type='file' name='filename' accept='image/jpg'> 
<input type='hidden' name='action' value='upload'> 
<input type='submit' value='Upload File'><br> 
Image File jpg(max size: $max_size bytes/".($max_size/1024)." kb) 
</form><br> 
<DIV class=end></DIV>";
try this:

Code: Select all

echo '
<form action="'. $_SERVER['PHP_SELF']  .'" method=post name=f 
enctype="multipart/form-data" onsubmit="'.return check().'"> 
<input type="file" name="filename" accept="image/jpg"> 
<input type="hidden" name="action" value="upload"> 
<input type="submit" value="Upload File"><br> 

Image File jpg(max size: '.$max_size bytes.'/"'.($max_size/1024).'" kb) 
</form><br> 
<DIV class=end></DIV>';

edit : ...

Image File jpg(max size: '.$max_size bytes.'/"'.($max_size/1024).'" kb)


is this suposed to be

Image File jpg(max size: '.$max_size_bytes.'/"'.($max_size/1024).'" kb)

??

Posted: Sat Dec 13, 2003 2:05 am
by aquila125
A class for sending mail with attachments, works perfect, no need for header knowledge:

http://phpmailer.sourceforge.net/

Posted: Sat Dec 13, 2003 8:16 am
by dev2761
infolock,
thank you for the reply, as for that code it doesn't need editing as it works perfectly.The problem comes afterwards, when I need to incorporate the image (name, tmp_name....?) into the mail.
And where the code sez $max_size bytes it just reads out how many bytes are allowed, see pic

Image

aquila125, thank you as well for being interested in this post. I've already looked at phpmailer and got totally lost in the documentation!
Thank you but that is far too complicated for me, maybe later.