HMV.co.in

September 27, 2008

Copy a file, or recursively copy a folder and its contents

Filed under: php — Tags: , , — Harsha M V @ 2:17 am

<?php
/**
* Copy a file, or recursively copy a folder and its contents
*
* @author      Aidan Lister <aidan@php.net>
* @version     1.0.1
* @link        http://aidanlister.com/repos/v/function.copyr.php
* @param       string   $source    Source path
* @param       string   $dest      Destination path
* @return      bool     Returns TRUE on success, FALSE on failure
*/
function copyr($source, $dest)
{
// Simple copy for a file
if (is_file($source)) {
return copy($source, $dest);
}

// Make destination directory
if (!is_dir($dest)) {
mkdir($dest);
}

// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == ‘.’ || $entry == ‘..’) {
continue;
}

// Deep copy directories
if ($dest !== “$source/$entry”) {
copyr(“$source/$entry”, “$dest/$entry”);
}
}

// Clean up
$dir->close();
return true;
}

?>

September 25, 2008

Magical PHP Constants

Filed under: php — Tags: , , , , , — Harsha M V @ 8:06 pm

A few “magical” PHP constants Name Description
__LINE__
The current line number of the file.

__FILE__
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

__DIR__
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

__FUNCTION__
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

__CLASS__
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

__METHOD__
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).

__NAMESPACE__
The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).

September 23, 2008

Write data to a text file

Filed under: php — Tags: , , — Harsha M V @ 10:53 pm

Description

This will open a plain text file (.txt) and save information to it.

The code


<?php

$your_data "This is the data to be stored in the text file.";

// Open the file and erase the contents if any

$fp fopen("textfile_name.txt""w");

// Write the data to the file

fwrite($fp$your_data);

// Close the file

fclose($fp);

?>

Blog at WordPress.com.