Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/filesystem/file.php

Documentation is available at file.php

  1. <?php
  2. /**
  3.  * @version        $Id: file.php 12541 2009-07-22 17:36:38Z ian $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    FileSystem
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. jimport('joomla.filesystem.path');
  19.  
  20. /**
  21.  * A File handling class
  22.  *
  23.  * @static
  24.  * @package     Joomla.Framework
  25.  * @subpackage    FileSystem
  26.  * @since        1.5
  27.  */
  28. class JFile
  29. {
  30.     /**
  31.      * Gets the extension of a file name
  32.      *
  33.      * @param string $file The file name
  34.      * @return string The file extension
  35.      * @since 1.5
  36.      */
  37.     function getExt($file{
  38.         $dot strrpos($file'.'1;
  39.         return substr($file$dot);
  40.     }
  41.  
  42.     /**
  43.      * Strips the last extension off a file name
  44.      *
  45.      * @param string $file The file name
  46.      * @return string The file name without the extension
  47.      * @since 1.5
  48.      */
  49.     function stripExt($file{
  50.         return preg_replace('#\.[^.]*$#'''$file);
  51.     }
  52.  
  53.     /**
  54.      * Makes file name safe to use
  55.      *
  56.      * @param string $file The name of the file [not full path]
  57.      * @return string The sanitised string
  58.      * @since 1.5
  59.      */
  60.     function makeSafe($file{
  61.         $regex array('#(\.){2,}#''#[^A-Za-z0-9\.\_\- ]#''#^\.#');
  62.         return preg_replace($regex''$file);
  63.     }
  64.  
  65.     /**
  66.      * Copies a file
  67.      *
  68.      * @param string $src The path to the source file
  69.      * @param string $dest The path to the destination file
  70.      * @param string $path An optional base path to prefix to the file names
  71.      * @return boolean True on success
  72.      * @since 1.5
  73.      */
  74.     function copy($src$dest$path null)
  75.     {
  76.         // Initialize variables
  77.         jimport('joomla.client.helper');
  78.         $FTPOptions JClientHelper::getCredentials('ftp');
  79.  
  80.         // Prepend a base path if it exists
  81.         if ($path{
  82.             $src JPath::clean($path.DS.$src);
  83.             $dest JPath::clean($path.DS.$dest);
  84.         }
  85.  
  86.         //Check src path
  87.         if (!is_readable($src)) {
  88.             JError::raiseWarning(21'JFile::copy: ' JText::_('Cannot find or read file'": '$src'");
  89.             return false;
  90.         }
  91.  
  92.         if ($FTPOptions['enabled'== 1{
  93.             // Connect the FTP client
  94.             jimport('joomla.client.ftp');
  95.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  96.  
  97.             // If the parent folder doesn't exist we must create it
  98.             if (!file_exists(dirname($dest))) {
  99.                 jimport('joomla.filesystem.folder');
  100.                 JFolder::create(dirname($dest));
  101.             }
  102.  
  103.             //Translate the destination path for the FTP account
  104.             $dest JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dest)'/');
  105.             if (!$ftp->store($src$dest)) {
  106.                 // FTP connector throws an error
  107.                 return false;
  108.             }
  109.             $ret true;
  110.         else {
  111.             if (!copy($src$dest)) {
  112.                 JError::raiseWarning(21JText::_('Copy failed'));
  113.                 return false;
  114.             }
  115.             $ret true;
  116.         }
  117.         return $ret;
  118.     }
  119.  
  120.     /**
  121.      * Delete a file or array of files
  122.      *
  123.      * @param mixed $file The file name or an array of file names
  124.      * @return boolean  True on success
  125.      * @since 1.5
  126.      */
  127.     function delete($file)
  128.     {
  129.         // Initialize variables
  130.         jimport('joomla.client.helper');
  131.         $FTPOptions JClientHelper::getCredentials('ftp');
  132.  
  133.         if (is_array($file)) {
  134.             $files $file;
  135.         else {
  136.             $files[$file;
  137.         }
  138.  
  139.         // Do NOT use ftp if it is not enabled
  140.         if ($FTPOptions['enabled'== 1)
  141.         {
  142.             // Connect the FTP client
  143.             jimport('joomla.client.ftp');
  144.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  145.         }
  146.  
  147.         foreach ($files as $file)
  148.         {
  149.             $file JPath::clean($file);
  150.  
  151.             // Try making the file writeable first. If it's read-only, it can't be deleted
  152.             // on Windows, even if the parent folder is writeable
  153.             @chmod($file0777);
  154.  
  155.             // In case of restricted permissions we zap it one way or the other
  156.             // as long as the owner is either the webserver or the ftp
  157.             if (@unlink($file)) {
  158.                 // Do nothing
  159.             elseif ($FTPOptions['enabled'== 1{
  160.                 $file JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$file)'/');
  161.                 if (!$ftp->delete($file)) {
  162.                     // FTP connector throws an error
  163.                     return false;
  164.                 }
  165.             else {
  166.                 $filename    basename($file);
  167.                 JError::raiseWarning('SOME_ERROR_CODE'JText::_('Delete failed'": '$filename'");
  168.                 return false;
  169.             }
  170.         }
  171.  
  172.         return true;
  173.     }
  174.  
  175.     /**
  176.      * Moves a file
  177.      *
  178.      * @param string $src The path to the source file
  179.      * @param string $dest The path to the destination file
  180.      * @param string $path An optional base path to prefix to the file names
  181.      * @return boolean True on success
  182.      * @since 1.5
  183.      */
  184.     function move($src$dest$path '')
  185.     {
  186.         // Initialize variables
  187.         jimport('joomla.client.helper');
  188.         $FTPOptions JClientHelper::getCredentials('ftp');
  189.  
  190.         if ($path{
  191.             $src JPath::clean($path.DS.$src);
  192.             $dest JPath::clean($path.DS.$dest);
  193.         }
  194.  
  195.         //Check src path
  196.         if (!is_readable($src&& !is_writable($src)) {
  197.             JError::raiseWarning(21'JFile::move: ' JText::_('Cannot find, read or write file'": '$src'");
  198.             return false;
  199.         }
  200.  
  201.         if ($FTPOptions['enabled'== 1{
  202.             // Connect the FTP client
  203.             jimport('joomla.client.ftp');
  204.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  205.  
  206.             //Translate path for the FTP account
  207.             $src    JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$src)'/');
  208.             $dest    JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dest)'/');
  209.  
  210.             // Use FTP rename to simulate move
  211.             if (!$ftp->rename($src$dest)) {
  212.                 JError::raiseWarning(21JText::_('Rename failed'));
  213.                 return false;
  214.             }
  215.         else {
  216.             if (!rename($src$dest)) {
  217.                 JError::raiseWarning(21JText::_('Rename failed'));
  218.                 return false;
  219.             }
  220.         }
  221.         return true;
  222.     }
  223.  
  224.     /**
  225.      * Read the contents of a file
  226.      *
  227.      * @param string $filename The full file path
  228.      * @param boolean $incpath Use include path
  229.      * @param int $amount Amount of file to read
  230.      * @param int $chunksize Size of chunks to read
  231.      * @param int $offset Offset of the file
  232.      * @return mixed Returns file contents or boolean False if failed
  233.      * @since 1.5
  234.      */
  235.     function read($filename$incpath false$amount 0$chunksize 8192$offset 0)
  236.     {
  237.         // Initialize variables
  238.         $data null;
  239.         if($amount && $chunksize $amount$chunksize $amount}
  240.         if (false === $fh fopen($filename'rb'$incpath)) {
  241.             JError::raiseWarning(21'JFile::read: '.JText::_('Unable to open file'": '$filename'");
  242.             return false;
  243.         }
  244.         clearstatcache();
  245.         if($offsetfseek($fh$offset);
  246.         if ($fsize filesize($filename)) {
  247.             if($amount && $fsize $amount{
  248.                 $data fread($fh$amount);
  249.             else {
  250.                 $data fread($fh$fsize);
  251.             }
  252.         else {
  253.             $data '';
  254.             $x 0;
  255.             // While its:
  256.             // 1: Not the end of the file AND
  257.             // 2a: No Max Amount set OR
  258.             // 2b: The length of the data is less than the max amount we want
  259.             while (!feof($fh&& (!$amount || strlen($data$amount)) {
  260.                 $data .= fread($fh$chunksize);
  261.             }
  262.         }
  263.         fclose($fh);
  264.  
  265.         return $data;
  266.     }
  267.  
  268.     /**
  269.      * Write contents to a file
  270.      *
  271.      * @param string $file The full file path
  272.      * @param string $buffer The buffer to write
  273.      * @return boolean True on success
  274.      * @since 1.5
  275.      */
  276.     function write($file$buffer)
  277.     {
  278.         // Initialize variables
  279.         jimport('joomla.client.helper');
  280.         $FTPOptions JClientHelper::getCredentials('ftp');
  281.  
  282.         // If the destination directory doesn't exist we need to create it
  283.         if (!file_exists(dirname($file))) {
  284.             jimport('joomla.filesystem.folder');
  285.             JFolder::create(dirname($file));
  286.         }
  287.  
  288.         if ($FTPOptions['enabled'== 1{
  289.             // Connect the FTP client
  290.             jimport('joomla.client.ftp');
  291.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  292.  
  293.             // Translate path for the FTP account and use FTP write buffer to file
  294.             $file JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$file)'/');
  295.             $ret $ftp->write($file$buffer);
  296.         else {
  297.             $file JPath::clean($file);
  298.             $ret file_put_contents($file$buffer);
  299.         }
  300.         return $ret;
  301.     }
  302.  
  303.     /**
  304.      * Moves an uploaded file to a destination folder
  305.      *
  306.      * @param string $src The name of the php (temporary) uploaded file
  307.      * @param string $dest The path (including filename) to move the uploaded file to
  308.      * @return boolean True on success
  309.      * @since 1.5
  310.      */
  311.     function upload($src$dest)
  312.     {
  313.         // Initialize variables
  314.         jimport('joomla.client.helper');
  315.         $FTPOptions JClientHelper::getCredentials('ftp');
  316.         $ret        false;
  317.  
  318.         // Ensure that the path is valid and clean
  319.         $dest JPath::clean($dest);
  320.  
  321.         // Create the destination directory if it does not exist
  322.         $baseDir dirname($dest);
  323.         if (!file_exists($baseDir)) {
  324.             jimport('joomla.filesystem.folder');
  325.             JFolder::create($baseDir);
  326.         }
  327.  
  328.         if ($FTPOptions['enabled'== 1{
  329.             // Connect the FTP client
  330.             jimport('joomla.client.ftp');
  331.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  332.  
  333.             //Translate path for the FTP account
  334.             $dest JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dest)'/');
  335.  
  336.             // Copy the file to the destination directory
  337.             if (is_uploaded_file($src&& $ftp->store($src$dest))
  338.             {
  339.                         $ret true;
  340.                         unlink($src);
  341.             else {
  342.                 JError::raiseWarning(21JText::_('WARNFS_ERR02'));
  343.             }
  344.         else {
  345.             if (is_writeable($baseDir&& move_uploaded_file($src$dest)) // Short circuit to prevent file permission errors
  346.                 if (JPath::setPermissions($dest)) {
  347.                     $ret true;
  348.                 else {
  349.                     JError::raiseWarning(21JText::_('WARNFS_ERR01'));
  350.                 }
  351.             else {
  352.                 JError::raiseWarning(21JText::_('WARNFS_ERR02'));
  353.             }
  354.         }
  355.         return $ret;
  356.     }
  357.  
  358.     /**
  359.      * Wrapper for the standard file_exists function
  360.      *
  361.      * @param string $file File path
  362.      * @return boolean True if path is a file
  363.      * @since 1.5
  364.      */
  365.     function exists($file)
  366.     {
  367.         return is_file(JPath::clean($file));
  368.     }
  369.  
  370.     /**
  371.      * Returns the name, sans any path
  372.      *
  373.      * param string $file File path
  374.      * @return string filename
  375.      * @since 1.5
  376.      */
  377.     function getName($file{
  378.         $slash strrpos($fileDS);
  379.         if ($slash !== false{
  380.             return substr($file$slash 1);
  381.         else {
  382.             return $file;
  383.         }
  384.     }
  385. }

Documentation generated on Sat, 14 Nov 2009 11:12:49 +0000 by phpDocumentor 1.3.1