file management basics part 1 php

Post on 24-May-2015

1.316 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Learn how to read, write and copy directories and fills in ProdigyView using PHP

TRANSCRIPT

File Management Basics

Overview

Objective

Learn how to create, copy and delete files within a file system using PHP and ProdigyView.

Requirements

Access read/write access to various locations in a file system

Estimated Time

5 Minutes

Follow Along With Code Example

1. Download a copy of the example code at www.prodigyview.com/source.

2.Install the system in an environment you feel comfortable testing in.

3.Proceed to examples/util/FileManagement1.php

What is File ManagementFile management is the process of reading and writing a file.

If you have performed reading and writing files in PHP, you may have come across minor frustration if fread() and fwrite() are being used. You may also have come across problems with encoding issues or questions on how to copy entire directories.

ProdigyView takes care of those difficulties for you.

File LocationsLet’s begin this example with creating the directories the file will be placed in. If you remember the PV_FILE defines, it sets the location in your file system to place files.

Writing A File

Passing the variables to this function break down like this.

$file: The location of the file to write

$content: The contents to put in the file

$mode : The mode in writing the file. The default is ‘w’ which means write only. If you pass ‘a’, that will append to the file.

$encoding: By default, no encoding is added to a file but you can always add one.

Writing a file is accomplished with this method:

Write a File Code

With our previous variables set up, lets write information to a file.

1. File Location 2. Content to write to file 3. Append content to file

4. Set the encoding

Reading A FileReading a file has similar options of writing a file. To read a file, this function is used:

$file: The location of the file to read

$mode: The mode to use when reading the file. The same mode options are used that are used in fopen() function can be used here.

$encoding: Default is no encoding, but an encoding can be used for the file being returned.

Reading File ExampleNow that you have the functions for writing and reading a file, lets put our code to use.

`. Read the file from a location

3. Set the encoding for the file

2. Set the mode for reading a file

New File Only!Now that we have basics for writing and reading a file, lets implement some file protection. Using the function writeNewFile() takes in the exact same parameters as writeFile, except it checks if the file exist first.

Will write a file only if it does not exist

Write File Only If It ExistThe opposite of of writeNewFile() is to rewriteNewFile(). This function will only write to a file if it already exist.

1. Write to file only if it exist

2. Read the file

DirectoriesOne of the things ProdigyView tries to make easier to manage is directories. ProdigyView provides the ability to copy entire directories and the ability to delete directories will relative ease.

1. Set directory to copy 2. Set location to copy directory

3. Delete an entire directory

Review1. Write a file with PVFileManager::writeFile()

method.1. PVFileManager::writeNewFile() will only write

a file if it DOES NOT already exist.

2. PVFileManager::rewriteNewFile() will only write a file if it DOES exist

2. Read a file using PVFileManager::readFile() method.

3. Copy directories using PVFileManager::copyDirectory() method.

4. Delete A directory by using PVFileManager::deleteDirectory() method.

API ReferenceFor a better understanding of file management in ProdigyView, visit the api by clicking on the link below.

PVFileManager

www.prodigyview.com

More Tutorials

For more tutorials, please visit:

http://www.prodigyview.com/tutorials

top related