PHP Classes
Icontem

File: classes/File.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Marius Zadara  >  ExifSort  >  classes/File.class.php  
File: classes/File.class.php
Role: Class source
Content type: text/plain
Description: file utilities
Class: ExifSort
Read EXIF information from picture files
 

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->pathPATHINFO_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);        
    }
}

?>



 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products