More on Functions - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

More on Functions

Description:

More on Functions PHP mod 4 B ... – PowerPoint PPT presentation

Number of Views:116
Avg rating:3.0/5.0
Slides: 24
Provided by: Wing151
Category:
Tags: functions | more

less

Transcript and Presenter's Notes

Title: More on Functions


1
More on Functions
  • PHP mod 4 B

2
Simple Mail Transfer Protocol (SMTP)
  • mail(to, subject, msg, mailheaders)
  • m08/8-1simple_form.html
  • m08/8-1send_simpleform.php

3
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().

4
Example
  • m09/9-1listfiles.php
  • filetype()
  • string filetype ( string filename )
  • Returns the type of the given file.

5
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()

6
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_EXCLO_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_EXCLO_CREAT flags for the
    underlying open(2) system call.

7
Example
  • M09/9-2newfile.php

8
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 _at_

9
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

10
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

11
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

12
email contents of a file
  • M09/9-6emailcontents.php

13
Copy file
  • copy Copies file
  • bool copy ( string source , string dest ,
    resource context )
  • m09/9-7copyfile.php

14
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

15
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.

16
Uploading a file
How to produce an input element with a Browse
button in HTML?
17
To get a Browse button
  • ltinput type"file" name"img1" size"30 /gt
  • m10/10-1upload_form.html

18
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.

19
_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).

20
  • Variables automatically placed in the _FILES
    superglobal
  • img1 is name of input element in the original
    html form
  • _FILESimg1tmp_name - refers to the temporary
    file on the web server
  • _FILESimg1name - Actual name of the file
    that was uploaded
  • _FILESimg1size - size of uploaded file in
    bytes
  • _FILESimg1type - The MIME type of the
    uploaded file, such as image/jpg

21
if (_FILES"img1" ! "")
_at_copy(_FILESimg1tmp_name ,
"C/wamp/www/m10/"._FILESimg1name )
or die("Couldn't copy the file.") else
die("No input file specified") See
m10/10-1do_upload.php
22
(No Transcript)
23
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com