<?php
if($_POST && isset($_FILES['my_file'])) 
{ 

	$sender_email = '[email protected]'; //from mail, sender email addrress 
	$recipient_email = '[email protected]'; //recipient email addrress 
	
	//Load POST data from HTML form 
	$sender_name = $_POST["sender_name"]; //sender name 
	$reply_to_email = $_POST["sender_email"] ;//sender email, it will be used in "reply-to" header 
	$subject	 = $_POST["subject"]; //subject for the email 
	$message	 = $_POST["message"]; //body of the email 
	

	
	//Get uploaded file data using $_FILES array 
	$file_tmp_name = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server 
	$file_name= $_FILES['my_file']['name']; // get the name of the file 
	$file_size	 = $_FILES['my_file']['size']; // get size of the file for size validation 
	$file_type	 = $_FILES['my_file']['type']; // get type of the file 
	$file_error= $_FILES['my_file']['error']; // get the error (if any) 

	//validate form field for attaching the file 
	if($file_error > 0) 
	{ 
		die('Upload error or No files uploaded'); 
	} 

	//read from the uploaded file & base64_encode content 
	$handle = fopen($file_tmp_name, "r"); // set the file handle only for reading the file 
	$content = fread($handle, $file_size); // reading the file 
	fclose($handle);				 // close upon completion 

	$encoded_content = chunk_split(base64_encode($content)); 

	$boundary = md5("random"); // define boundary with a md5 hashed value 

	//header 
	$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
	$headers .= "From:".$sender_email."\r\n"; // Sender Email 
	$headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email addrress to reach back 
	$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 
	$headers .= "boundary = $boundary\r\n"; //Defining the Boundary 
		
	//plain text 
	$body = "--$boundary\r\n"; 
	$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
	$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
	$body .= chunk_split(base64_encode($message)); 
		
	//attachment 
	$body .= "--$boundary\r\n"; 
	$body .="Content-Type: $file_type; name=".$file_name."\r\n"; 
	$body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
	$body .="Content-Transfer-Encoding: base64\r\n"; 
	$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n"; 
	$body .= $encoded_content; // Attaching the encoded file with email 
	
	$sentMailResult = @mail($recipient_email, $subject, $body, $headers); 

	if($sentMailResult ) 
	{ 
	echo "File Sent Successfully."; 
	
	} 
	else
	{ 
	die("Sorry but the email could not be sent. 
					Please go back and try again!"); 
	} 
}
else{
?>
<form enctype="multipart/form-data" method="POST" action=""> 
	<label>Your Name <input type="text" name="sender_name" /> </label> <br>
	<label>Your Email <input type="email" name="sender_email" /> </label><br> 
	<label>Subject <input type="text" name="subject" /> </label><br>
	<label>Message <textarea name="message"></textarea> </label><br>
	<label>Attachment <input type="file" name="my_file" /></label><br> 
	<label><input type="submit" name="button" value="Submit" /></label><br>
</form> 
<?php
}
?> 

PHP Online Compiler

Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP and start coding.

Taking inputs (stdin)

OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.

<?php
	fscanf(STDIN, "%s\n", $name);           
    echo "Hello ".$name.".\n";
?>

About PHP

PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.

Key features

  • Free
  • powerful tool for making dynamic and interactive web pages
  • can integrate with almost all popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix, Microsoft SQL Server etc.
  • C like Syntax and easy to learn.
  • Object oriented scripting language.
  • easily embeddable into HTML
  • Loosely typed language.

Syntax help

Variables

In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.

$variable_name = value;  

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
    //code if above condition is true  
} elseif(condition-expression2){  
    //code if above condition is true  
}  
elseif(condition-expression3) {  
    //code if above condition is true  
}  
...  
else {  
    //code if all the conditions are false  
}  

2. Switch:

Switch is used to execute one set of statement from multiple conditions.

switch(conditional-expression) {    
case value1:    
 // code if the above value is matched    
 break;  // optional  
case value2:    
 // code if the above value is matched    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 
 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

For-each:

// you can use any of the below syntax
foreach ($array as $element-value) {  
    //code  
}

foreach ($array as $key => $element-value) {   
    //code 
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

How to define a Function

function function_name(parameters) {  
  //code
}

How to call a Function

function_name (parameters)