14.module4 - PHP FILE EMAIL NEW - 12 PDF
14.module4 - PHP FILE EMAIL NEW - 12 PDF
14.module4 - PHP FILE EMAIL NEW - 12 PDF
NALINI N
AP(SR)
SCOPE
VIT
Overview
file's contents.
Opening a File
In PHP, a file is created using a command that is also used to open files.
In PHP the fopen function is used to open files. However, it can also create a
file if it does not find the file specified in the function call.
So if you use fopen on a file that does not exist, it will create it, given that
?>
How to create a file ?
Modes Description
w Write only. Opens and clears the contents of file; or creates a new file if it
doesn't exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it
doesn't exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn't
exist
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists
Closing a File
fclose($file);
?>
PHP fread() Function
fread(file,length)
The function will stop at the end of the file or when it reaches the specified length,
whichever comes first.
Parameter Description
file Required. Specifies the open file to read from
length Required. Specifies the maximum number of
bytes to read
PHP fread() Function
<?php
$file = fopen("test.txt","r");
fread($file,"10");//fread($file,filesize("test.txt"));
fclose($file);
?>
Check End-of-file
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br>";
}
fclose($file);
?>
Reading a File Character by Character
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
Reading/writing an entire file
14
# reverse a file
$text = file_get_contents("poem.txt");
$text = strrev($text);
file_put_contents("poem.txt", $text);
XXX
(919)685-2181
570-86-7326 contents of file personal.txt
reads the file into an array of lines and unpacks the lines into
variables
Need to know a file's exact length/format
Write
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable
to open file!");
$txt = “xxx\n";
fwrite($myfile, $txt);
$txt = “yyy\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Delete File - unlink()
Syntax :
Example
<?php
unlink('data.txt');
27
12) file_exists(fp) - Checks whether a file or directory exists
Some File Functions
By using the global PHP $_FILES array you can upload files from a client
computer to the remote server.
The first parameter is the form's input name and the second index can be
either "name", "type", "size", "tmp_name" or "error". Like this:
29
File Upload
E.g.
<html>
<body> The enctype attribute of the
<form action="upload_file.php" <form> tag specifies which
method="post" content-type to use when
enctype="multipart/form-data"> submitting the form.
<label for="file">Filename:</label> "multipart/form-data" is used
<input type="file" name=“myfile" when a form requires binary
id="file" /> data, like the contents of a
<br /> file, to be uploaded.
<input type="submit" The type="file" attribute of
name="submit" value="Submit" /> the <input> tag specifies that
</form> the input should be processed
</body> as a file.
</html>
30
<html> <form method=post action="upload.php" enctype=multipart/form-data>
select a file<input type=file name=userfile >
<input type=submit value=submit>
</form> </html>
<?php
if($_FILES['userfile']['size']<1000000) AND $_FILES['userfile']['type']=="text/plain")
{
//if(@copy($_FILES['userfile']['tmp_name'],"./upload/".$_FILES['userfile']['name']))
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"./upload/".$_FILES['userfile']['n
ame']))
echo "file uploaded";
else
echo "fail1";
}
else
echo "fail2";
?>
Adding Restrictions
<?php //upload_file.php
if ((($_FILES[“myfile"]["type"] == "image/gif")
|| ($_FILES[“myfile"]["type"] == "image/jpeg")
|| ($_FILES[“myfile"]["type"] == "image/pjpeg"))
&& ($_FILES[“myfile"]["size"]/1024 < 20))
{
if ($_FILES[“myfile"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
move_uploaded_file($_FILES['userfile']['tmp_name'],"./upload/".$_FILES['user
file']['name'])
33
Email – mail()
It is a function in PHP used to send an email. It returns true on success of
sending an email, otherwise false.
mail(to,sub,message, headers, other_parameters)
(In this, the first three parameters are must. )
Where to- the receiver(s) email address(es). “,” used as delimiter to separate the
addresses.
Sub – subject of the mail
Message – text which has to delivered to the receiver(s).
Headers – additional parameter used
1. to include BCC & CC email address(es),
2. send the mail as multipart, and
3. contains the attachment file
Other_parameters – used to display some additional information to the receiver(s).
34
Simple Mail program
<? php
$to=“user123@example.com”;
$sub=“Test mail”;
$message1 = “This is a sample test mail";
$mailsent = mail($to,$sub,$message1);
If ($mailsent)
echo “the mail was sent to $to successfully”;
Else
echo “the mail couldn’t send. Pl verify your mail settings / connection”;
?>
Output:
The mail was sent to user123@example.com successfully.
35
Email – with cc & Bcc
<? php
$to=“user123@example.com”;
$sub=“Test mail”;
$message1 = “This is a sample test mail";
$header = “cc:aa@aa.com, ab@aa.com\r\n”;
$header .=“bcc: ac@aa.com”;
$mailsent = mail($to,$sub,$message1,$header);
If ($mailsent)
echo “the mail was sent to $to and $headersuccessfully”;
Else
echo “the mail couldn’t send. Pl verify your mail settings /
connection”;
?>
Output:
The mail was sent to user123@example.com cc:aa@aa.com,
ab@aa.com bcc: ac@aa.com successfully.
36
Email with an attachment
1. Collect the message and store it in a variable.
2. Add the boundary at the end of that message.
3. Enclose the content type of that message and its
transfer encoding method.
4. Now add boundary to mark it as end of message.
5. Open the file which has to be attached with the
mail in rb mode and transfer the content into a php
variable.
6. Now add the content-type to the message as the
type of attached file.
37
Email with an attachment
7. Then set the name as the file name which has to be
sent an attachment.
8. Add “content-disposition: attachment” – which
indicates the mail is coming with an attachment.
9. Can add the rename option of the attached file.
10. Add the transfer encoding type.
11. Now it is a time to add the content of the
attachment file.
12. Add the boundary with the message as a ended
one.
38
Sample Program
39
<?php
$to="nalini@vit.ac.in";
$sub="fileattachmaent";
$msg="pdf";
$fn="ss.pdf";
$fo=fopen($fn,"rb");
$data=fread($fo,filesize($fn));
fclose($fo);
$data1=chunk_split(base64_encode($data));
//$h="Content-Type:text/plain\r\n";
//$h.="Content-Transfer-Encoding:7bit".$msg."\r\n";
$h="Content-Type:multipart/mixed\r\n";
$h.="Content-Disposition:attachment;filename=$fn\r\n";
$h.="Content-Transfer-Encoding:base64".$data1."\r\n";
$m=mail($to,$sub,$msg,$h);
if($m)
echo "success";
else
echo "fail";