Tuesday, April 17, 2007

PHP code Copy server files

$log_folder = "logs";
$log_name1 = "logs.txt";
if (!(copy($log_folder,$log_name1))) { echo "Error copying $log_name1 to $log_folder"; }
else { echo "File successfully copied"; }
?>

PHP Rename files

$log_folder = "logs";
$log_name1 = "logs.txt";
$log_name2 = "logs.txt.old";
$log_name3 = "logs.txt.archive";
if (!(unlink($log_name3))) {
echo "Error deleting $log_name3
";
} else {
echo "$log_name3 deleted successfully
"; }
if (!(rename($log_name2,$log_name3))) {
echo "Error renaming $log_name2 to $log_name3
";
} else {
echo "$log_name2 successfully renamed to $log_name3
";
}
if (!(rename($log_name1,$log_name2))) {
echo "Error renaming $log_name1 to $log_name2
";
} else {
echo "$log_name1 successfully renamed to $log_name2
";
}
if (!(copy($log_folder,$log_name1))) {
echo "Error copying $log_name1 to $log_folder
";
} else {
echo "$log_name1 copied to $log_folder successfully
";
}
?>

PHP Copying and Deleting Files

To copy a file, the copy() function is used:

bool copy(string source_file,string destination_file)


The copy() function will return true if the file was correctly copied, or false if there was an error.

$log_folder = "logs";
$log_name1 = "logs.txt";
if (!(copy($log_folder,$log_name1))) { echo "Error copying $log_name1 to $log_folder"; }
else { echo "File successfully copied"; }
?>

Introduction to File Handling in PHP - Reading the File

Reading the File

We will now enhance the prior example, providing a means to read the contents of a file after writing to it. Reading from a file requires the use of the fread() function:

string fread (resource handle, int length)

The fread() function reads up to length bytes from the file pointer referenced by handle. Reading stops when length bytes have been read, EOF (end of file) is reached, or (for network streams) when a packet becomes available, whichever comes first.

$file = "newfile.txt";
if (!$file_handle = fopen($file,"r")) {
echo "Cannot open file.";
}
if (!$file_contents = fread($file_handle, filesize($file))) {
echo "Cannot retrieve file contents.";
}
else { echo "$file_contents"; }
fclose($file_handle);
?>

PHP - Create and Write to File

Create and Write to File

Let's use the 'a' mode to open/create our file. This will append any new data to the end of the file, but opens the file access for writing only. If the file does not exist, this mode will attempt to create it first:

$data = "This is a new file entry!\n";
$file = "newfile.txt";
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }
echo "You have successfully written data to $file";
fclose($file_handle);
?>

PHP - file Process mode settings info

Selecting a Mode of Access
Before working with a file, it must exist. Lets use the fopen() function to create and open a file:

resource fopen (string filename, string mode [, int use_include_path [, resource zcontext]])


The fopen() function will attempt to open a stream to the specified file. Using the correct mode parameter is very important at this point, as it identifies the type of file access that will be required. Here is a list of all possible mode parameters you can use with the fopen() function:

mode Description
'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. This option is supported in PHP 4.3.2 and later, and only works
for local files.
'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. This option is supported in PHP 4.3.2 and later, and only works for local files.



Note: Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.

File handling in PHP

Introduction
This tutorial will concentrate on the basic methods used to handle files in PHP. There are multiple functions available in PHP for these types of operation, but we will only be concentrating on some of the more important functions that PHP provides for file handling. This should leave you with enough insight to continue on your own.


This tutorial assumes several things about the reader:


* You are familiar with PHP
* You have access to a server or servers running the PHP package
* You have read/write privileges to the root folder on your server


The easiest way to provide yourself with read/write privileges to a file and/or folder on your server is to open an ftp session to your active directory and use the CHMOD command to grant full permissions to the target file/folder. If this option is not open to you, you can also use the chmod() function in PHP:

bool chmod (string filename, int mode)


This function will return a boolean true or false, where true is success and false is a change of permissions failure. For our example, you will require the permissions of the root folder on your server to be set to 777.


Once you have completed this tutorial, I would urge you to consult the PHP manual to experiment further with privileges and functions regarding file handling.

PHP Global Varibale settings

Meet the super globals.

Time to bring on the next generation of globals - super globals. What are these strange new creatures? Essentially, they are new global arrays which are automatically created. Check out the list below (taken from the manual).

Figure 2.1 - Super Globals (from the manual)

$GLOBALS
Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables. $GLOBALS has existed since PHP 3.

$_SERVER
Variables set by the web server or otherwise directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).

$_GET
Variables provided to the script via HTTP GET. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).

$_POST
Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).

$_COOKIE
Variables provided to the script via HTTP cookies. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).

$_FILES
Variables provided to the script via HTTP post file uploads. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated). See POST method uploads for more information.

$_ENV
Variables provided to the script via the environment. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).

$_REQUEST
Variables provided to the script via any user input mechanism, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the variables_order configuration directive. This array has no direct analogue in versions of PHP prior to 4.1.0. See also import_request_variables().

Note: When running on the command line , this will not include the argv and argc entries; these are present in the $_SERVER array.

$_SESSION
Variables which are currently registered to a script's session. Analogous to the old $HTTP_SESSION_VARS array (which is still available, but deprecated). See the Session handling functions section for more information.

As you can see, there are quite a few to choose from. In normal practice, however, you will probably only use a few, with the most likely three being $_POST, $_GET, and $_COOKIE. These will allow you to manage any form data that you might encounter, in addition to cookies.

Assuming that your form uses method="get" then to access an element named fish you would now use $_GET['fish'] instead of $fish. Easy, eh? Please take note of the single quote marks around the variable name - essentially your are using a named index of an array (look up arrays in the manual for more information). The same general principle can be applied to any other methods of transmission - with the added benefit that you can expressly limit the acceptable incoming data to a specific method.

If you wanted to accept information from get, post, or cookie, then you could also use $_REQUEST, but this is considered bad form, as you are essentially going back to bad habits of register_globals = on.

Note: If you are having trouble with a page, and want to find out exactly what data is being passed, the try using the function print_r([SUPERGLOBAL]);. This will output all the contents of the super global you use to replace "[SUPERGLOBAL]" (e.g. $_POST).

Easy Varibale PHP

PHP Variable declaration Via : -

$GLOBALS, $_SESSION, $_GET, $_POST, $_COOKIE, $_REQUEST, $_SERVER, $_FILES, $_ENV

Sunday, April 15, 2007

PHP E-Commerce Solutions

A while back I was asked, "Where is the money?"
This has inspired me to comment on PHP E-Commerce Solutions

Below are links to systems that are I consider to be "Best of Breed" PHP E-Commerce solutions. Remember though...trust no one! The only secure system is the one you build yourself! The following in no way represents an exhaustive list of PHP E-Commerce solutions. Web links to BSD licensed PHP E-Commerce software solutions are encouraged!

BakeSale
Couffin
Zen Cart
phPay

While the above projects are all decent in their own way, searching for a BSD Licensed e-commerce solution leads to MyMarket. Because MyMarket is BSD licensed, it provides an ideal starting point of departure towards the creation of a PHP E-Commerce solution. Interested in the concept of building a custom BSD licensed PHP shopping cart solution? A really good hands on tutorial is this link to building-a-simple-php-shopping-cart.

Have you checked out the current list of PHP functions? Dive into the PHP Function list! Maybe the code you are looking for is already written. These are documented PHP functions that can be called by PHP.

PHP code sources
PHP Builder
Dev Shed
weberdev
PHP Classes Repository
PHP code search engine

http://www.hotscripts.com/PHP/


Further reading can be found at
DevX PHP articles doorway
and as usual, the best place to look for information about PHP is www.php.net

Thanks to the contributors of NYPHPtalk for inspiring this posting.