PHP Classes

File: classes/File.class.php

Recommend this page to a friend!
  Classes of Marius Zadara   ExifSort   classes/File.class.php   Download  
File: classes/File.class.php
Role: Class source
Content type: text/plain
Description: file utilities
Class: ExifSort
Read EXIF information from picture files
Author: By
Last change:
Date: 16 years ago
Size: 3,689 bytes
 

Contents

Class file image Download
<?php

/**
 * Class used for file operations.
 * The class is final, meaning it can not be extended anymore.
 *
 * @author Marius Zadara <marius@zadara.org>
 * @copyright Marius Zadara <marius@zadara.org>
 * @final
 */

final class File
{
   
// the file path
   
private $path;
   
   
// date and time
   
private $dateTime = false;
   
   
// mime type
   
private $mimeType = false;
   
   
   
/**
     * Constructor
     *
     * @param string $path The absolute path of the file
     */
   
public function __construct($path)
    {
       
$this->path = $path;
       
       
// read the exif information
       
$this->readExif();
    }
   
   
/**
     * This function will try to read the exif data from the file
     * and update the class members with the information found.
     *
     * If the datetime field has not been found, it will be updated with the time
     * the file has been last modified.
     *
     */
   
private function readExif()
    {
       
// init the default values
       
$fileDateTime = false;
       
$mimeType = false;
               
       
// try to read the exif data from the file
       
$exifData = @read_exif_data($this->path);

       
// has the date been read ?
       
if ($exifData !== false)
        {
           
// FileDateTime ///////////////////////////////////////////////////////////////////////
// if (isset($exifData['FileDateTime']))
// {
// // load directly from the record
// $fileDateTime = $exifData['FileDateTime'];
//
// echo "FileDateTime: " . $fileDateTime . "<br />";
//
// }
// else
           
{
               
// load from the DateTime record
               
if (isset($exifData['DateTime']))
                {
                   
// get the components
                   
list($year, $month, $day, $hour, $min, $sec) = sscanf($exifData['DateTime'], "%d:%d:%d %d:%d:%d");

                   
// make the unix timestamp
                   
$mkTime = @mktime($hour, $min, $sec, $month, $day, $year);
                                        
                   
// validate the timestamp
                   
if (($mkTime !== false) && ($mkTime != -1))
                       
$fileDateTime = $mkTime;
                }
                else
                {
                   
// if all failed, set the last modification of the file
                   
$fileDateTime = filemtime($this->path);
                }
            }
           
           
// Mime Type //////////////////////////////////////////////////////////////////////////
           
if (isset($exifData['MimeType']))
               
$mimeType = $exifData['MimeType'];
        }
        else
        {
           
// if all failed, set the last modification of the file
           
$fileDateTime = filemtime($this->path);
           
           
$extension = pathinfo($this->path, PATHINFO_EXTENSION);
           
$extension = strtolower(trim($pathinfo));
            if ((
$pathinfo != "") && isset(Constants::$EXTENSION_2_MIMETYPE[$extension]))
               
$mimeType = Constants::$EXTENSION_2_MIMETYPE[$extension];
        }

       
       
// update the class members
       
$this->dateTime = $fileDateTime;
       
$this->mimeType = $mimeType;
    }
   
   
/**
     * Getter for the dateTime field
     *
     * @return Timestamp
     */
   
public function getDateTime()
    {
       
// return the class member
       
return $this->dateTime;
    }
   
   
   
/**
     * Getter for the mime type
     *
     * @return String The file mime type
     */
   
public function getMimeType()
    {
        return
$this->mimeType;
    }
   
   
   
/**
     * Function used to copy the file to a destination directory
     *
     * @param string $destination
     * @return boolean TRUE/FALSE
     */
   
public function copyTo($destination)
    {
       
// return the result of copy
       
return copy($this->path, $destination);
    }
   
   
   
/**
     * Function used to move the file to a destination directory
     *
     * @param string $destination
     * @return boolean TRUE/FALSE
     */
   
public function moveTo($destination)
    {
       
// return the result of move operation
       
return @move($this->path, $destionation);
    }
}

?>