PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
phpquestion
Forum Newbie
Posts: 3 Joined: Tue Aug 17, 2010 2:49 am
Post
by phpquestion » Tue Aug 17, 2010 3:11 am
In javascript code, I create a <input type="file"> element, such that:
Code: Select all
......
var e = document.createElement("input");
e.setAttribute("type", "file");
......
In PHP code, I would like to get the file information:
Code: Select all
.....
foreach($_FILES as files){
$tmp_name = $files["tmp_name"];
if (file_exists($tmp_name){
echo "upload succeded. ";
}
}
.....
The code does not work. If I simply use HTML to create <input type="file"> element, PHP works. Can you tell me what is wrong?
maneetpuri
Forum Commoner
Posts: 60 Joined: Tue Oct 07, 2008 6:32 am
Post
by maneetpuri » Tue Aug 17, 2010 3:57 am
Hi,
There seems to be something missing in the code that is creating these input boxes, maybe that you have not shared the complete code. Another missing thing is the names of the input boxes that you are creating.
Cheers,
phpquestion
Forum Newbie
Posts: 3 Joined: Tue Aug 17, 2010 2:49 am
Post
by phpquestion » Tue Aug 17, 2010 4:25 am
This code is working fine:
Code: Select all
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$to="somebody@example.com";
$subject="E-mail with attachment";
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message="This is an example";
$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";
foreach($_FILES as $userfile){
$tmp_name = $userfile['tmp_name'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$message.="--{$mime_boundary}--\n";
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>
But use Javascript, it does not work:
Code: Select all
<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<SCRIPT type="text/javascript">
function add(){
var e = document.createElement("input");
e.setAttribute("type", "file");
document.getElementById("moreFile").appendChild(e);
}
</SCRIPT>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$to="somebody@example.com";
$subject="E-mail with attachment";
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message="This is an example";
$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";
foreach($_FILES as $userfile){
$tmp_name = $userfile['tmp_name'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$message.="--{$mime_boundary}--\n";
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>File: <input type="file" name="file1"></p>
<p id="moreFile" ></p>
<p><a href="#" onclick="add()"; />Add File</a></P>
</form>
<?php } ?>
</body>
</html>