PHP: Including and Requiring Files

This part is of the PHP Post Series dedicated to spread knowledge about PHP

Code organization is one of the reasons for including and requiring files in PHP applications. This is mostly used for configuration files, settings, etc.

Include

include() is a function that allows you to include files in PHP files.

Example Code
include('simplepie.inc');

Include Once

include_once makes sure that a file is included only once in the file. If the file was already included, the file won’t be included again.

include_once('simplepie.inc');
Include / Include Once Trick

include and include_once can also be written as follows if we want to check if a file has been included.

if (@include('simplie.inc')) {
    echo 'File has been included';
} else {
    echo 'File has not been included';
}

Require

Another function to be used when including files is the require function. The require function doesn’t just include a file but requires it. This means that if a file fails to be included, probably due to the file not existing, a fatal error is thrown.

require('simplepie.inc');

Require Once

require_once just like include_once makes sure that a file is included only once.

require_once('simplepie.inc');

 Rename a Table in Rails

Sometimes you aimlessly create a table and you’d want to change it. Here’s how to rename a table in Rails 3 and higher.

Rails 3

class RenameOldTable < ActiveRecord::Migration
  def self.up
    rename_table :old_table, :new_table
  end
  def self.down
    rename_table :new_table, :old_table
  end
end

Rails 3.1+

Rails 3.1 and above offers a shorter alternative to renaming tables

class RenameOldTable < ActiveRecord::Migration
  def change
    rename_table :old_table_name, :new_table_name
  end
end
 

 PHP: Delete files with unlink()

This part is of the PHP Post Series dedicated to spread knowledge about PHP

Deleting, creating and modifying files critical filesystem functionalities you can expect from programming languages.

PHP doesn’t want to get left down. Here are a few functionalities and tricks for deleting files.

Delete a single file

unlink('index.html');

Delete all files in a directory

// get all file names
$files = glob('path/to/folder/*');
// loop through files
foreach($files as $file){
  if(is_file($file)) {
    // delete file
    unlink($file);
  }
}
// A better alternative is to use the array_map function in conjunction with the glob function
array_map('unlink', glob("path/to/folder/*"));

Deleting certain file types

$jpeg_files = glob('path/to/folder/*.jpg');
// loop through files
foreach($jpeg_files as $file){
  if(is_file($file)) {
    // delete file
    unlink($file);
  }
}
// Alternative
array_map('unlink', glob("path/to/folder/*.jpg"));

 PHP: Creating Copies of Files

This part is of the PHP Post Series dedicated to spread knowledge about PHP

PHP has a bunch of functions that works with files. copy is a very useful one. copy makes copies of files in the filesystem.

Example Code

copy("ad_images/image1.png", "ad_images/thumb/image1.png");
// Test if a copy has been made.
if (file_exists("ad_images/thumb/images1.png") {
    echo "File has been copied over";
} else {
    echo "File has not been copied over";
}
// A better alternative is to use the copy method's return value
// Copy and Test if a copy has been made.
if (copy("ad_images/image1.png", "ad_images/thumb/image1.png")) {
    echo "File has been copied over";
} else {
    echo "File has not been copied over";
}

The copy function requires two parameters, the original file location with name and the destination filename with a new or old filename.