More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg,...

23
More on Functions PHP mod 4 B

Transcript of More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg,...

Page 1: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

More on Functions

PHP mod 4 B

Page 2: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Simple Mail Transfer Protocol (SMTP)

• mail($to, $subject, $msg, $mailheaders);• m08/8-1simple_form.html• m08/8-1send_simpleform.php

Page 3: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

File operations• $dir = opendir($dir_name);

– http://php.net/manual/en/function.opendir.php– Opens up a directory handle to be used in subsequent closedir(),

readdir(), and rewinddir() calls. • readdir($dir);

– http://php.net/manual/en/function.readdir.php– Returns the filename of the next file from the directory. The

filenames are returned in the order in which they are stored by the filesystem.

• closedir($dir)– http://php.net/manual/en/function.closedir.php– Closes the directory stream indicated by dir_handle . The

stream must have previously been opened by opendir().

Page 4: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Example

• m09/9-1listfiles.php• filetype()

– string filetype ( string $filename )– Returns the type of the given file.

Page 5: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Opening/closing a file

• fopen — Opens file or URL– http://php.net/manual/en/function.fopen.php– resource fopen ( string $filename , string $mode [,

bool $use_include_path = false [, resource $context ]] )

– fopen() binds a named resource, specified by filename , to a stream.

• fclose()

Page 6: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Modes of fopen()'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to

zero length. If the file does not exist, attempt to create it. 'w+‘ Open for reading and writing; place the file pointer at the beginning of the file and truncate the

file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist,

attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not

exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file

already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

Page 7: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Example

• M09/9-2newfile.php

Page 8: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Since “w+” truncates the length to 0 …

• file_exists — Checks whether a file or directory exists

• http://php.net/manual/en/function.file-exists.php

• Example: m09/9-3newfile_checkfirst.php• To suppress PHP warnings:

– precede PHP functions with @

Page 9: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

To write to a file

• fwrite — Binary-safe file write– Syntax: int fwrite ( resource $handle , string

$string [, int $length ] )– fwrite() writes the contents of string to the file

stream pointed to by handle .– Return value: fwrite() returns the number of bytes

written, or FALSE on error. • Example: m09/9-4writedata.php• die(String message) function

Page 10: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

To read from a file

• fread — Binary-safe file read– Syntac: string fread ( resource $handle , int $length )– fread() reads up to length bytes from the file pointer

referenced by handle . Reading stops as soon as one of the following conditions is met:

• length bytes have been read • EOF (end of file) is reached • a packet becomes available (for network streams) • 8192 bytes have been read (after opening userspace stream)

– Return: Returns the read string or FALSE in case of error. • http://php.net/manual/en/function.fread.php for

warnings - difference between text and binary

Page 11: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Examples

• m09/9-5readdata.php– Wait a minute! Compare textfile.txt and the

printout. Any difference?

• m09/9-5Breaddata.php• To deal with the missing line break, use the

nl2br() function– nl2br() - newline_to_break function

Page 12: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

email contents of a file

• M09/9-6emailcontents.php

Page 13: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Copy file

• copy — Copies file• bool copy ( string $source , string $dest [,

resource $context ] )• m09/9-7copyfile.php

Page 14: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

rename()

• rename — Renames a file or directory• bool rename ( string $oldname , string

$newname [, resource $context ] )• Attempts to rename oldname to newname . • Return Values

– Returns TRUE on success or FALSE on failure.

• Example: m09/9-8renamefile.php

Page 15: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

unlink — Deletes a file

• bool unlink ( string $filename [, resource $context ] )– Deletes filename . Similar to the Unix C unlink()

function. – Return Values: Returns TRUE on success or FALSE

on failure.

Page 16: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Uploading a file

How to produce an input element with a Browse button in HTML?

Page 17: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

To get a Browse button:

• <input type="file" name="img1" size="30“ />• m10/10-1upload_form.html

Page 18: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

Superglobal $_FILES

• $_FILES – The $_FILES Superglobal represents data available

to a PHP script from HTTP POST file uploads. – Using $_FILES is the currently preferred way to

handle uploaded files in PHP.

Page 19: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

$_FILES [‘userfile’], where userfile is name of input element

• $_FILES['userfile']['name'] (Specifies the original name of the file being uploaded on the client computer).

• $_FILES['userfile']['type'] (Specifies the MIME type of the file being uploaded, for instance, "image/jpeg").

• $_FILES['userfile']['size'] (Indicates the size in bytes of the file being uploaded).

• $_FILES['userfile']['tmp_name'] (Indicates the temporary name used by the web server to store the uploaded file).

• $_FILES['userfile']['error'] (Specifies the error code associated with a specific file upload).

Page 20: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

• Variables automatically placed in the $_FILES superglobal• img1 is name of input element in the original html form• $_FILES[img1][tmp_name] - refers to the temporary file on

the web server• $_FILES[img1][name] - Actual name of the file that was

uploaded• $_FILES[img1][size] - size of uploaded file in bytes• $_FILES[img1][type] - The MIME type of the uploaded file,

such as image/jpg

Page 21: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.

if ($_FILES["img1"] != "") {

@copy($_FILES[img1][tmp_name] ,

"C:/wamp/www/m10/".$_FILES[img1][name] )

or die("Couldn't copy the file.");

} else {

die("No input file specified");

}

See m10/10-1do_upload.php

Page 22: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.
Page 23: More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.