public function installFile($sSourcePath, $sInstallPath)
	{
		// If the temp file doesn't exist, we cannot install it
		if(! file_exists($sSourcePath))
			throw new NoSuchFileException($sSourcePath, 1);

		try
		{
			// Create the install directory if needed
			$sInstallDir = dirname($sInstallPath);
			$this->installDir($sInstallDir);
		}
		catch(Exception $e)
		{
			throw new Exception("Cannot install file, no such directory no perms to create it: $sInstallPath", 11, $e);
		}

		// First move temp path up to a temporary filename on the install drive

		$sTempInstallPath = $this->findTempSafeInstallPath($sInstallPath);

		// @silence copy() PHP warnings, we check for failure and throw our own exception
        $success = true;
        $originalUmask = umask(0377); // set secure copy() permissions (read-only by current user, nobody else has ANY perms on it at all)
		if(! @copy($sSourcePath, $sTempInstallPath))
            $success = false;
        umask($originalUmask); // reinstate original umask

        if(! $success)
            throw new Exception("Unable to copy file to temp install path: $sTempInstallPath", 17);

        // Explicitly set the file owner and permissions of the newly copied file to be the same
        // as the source path.  PHP copy() seems to disregard the settings of the source file and
        // instead use the current proc's umask() and user identity.

        clearstatcache(); // We don't care what the cache says, we want to know the current stat info

        $hSrcStat = stat($sSourcePath);
        $sSrcOwner = $hSrcStat['uid'];
        $sSrcGroup = $hSrcStat['gid'];
        $sSrcMode = $hSrcStat['mode'];

        $hDstStat = stat($sTempInstallPath);
        $sDstOwner = $hDstStat['uid'];
        $sDstGroup = $hDstStat['gid'];
        $sDstMode = $hDstStat['mode'];

        if($sSrcOwner !== $sDstOwner)
            @chown($sTempInstallPath, $sSrcOwner);

        if($sSrcGroup !== $sDstGroup)
            @chgrp($sTempInstallPath, $sSrcGroup);

        if($sSrcMode !== $sDstMode)
            @chmod($sTempInstallPath, $sSrcMode);

        // Now rename the temp path to the final location
		// @silence rename() PHP warnings, we check for failure and throw our own exception
		if(! @rename($sTempInstallPath, $sInstallPath))
			throw new Exception("Unable to rename temp file to $sInstallPath", 21);
	}
public function installFile($sSourcePath, $sInstallPath)
	{
		// If the temp file doesn't exist, we cannot install it
		if(! file_exists($sSourcePath))
			throw new NoSuchFileException($sSourcePath, 1);

		try
		{
			// Create the install directory if needed
			$sInstallDir = dirname($sInstallPath);
			$this->installDir($sInstallDir);
		}
		catch(Exception $e)
		{
			throw new Exception("Cannot install file, no such directory no perms to create it: $sInstallPath", 11, $e);
		}

		// First move temp path up to a temporary filename on the install drive

		$sTempInstallPath = $this->findTempSafeInstallPath($sInstallPath);

		// @silence copy() PHP warnings, we check for failure and throw our own exception
        $success = true;
        $originalUmask = umask(0377); // set secure copy() permissions (read-only by current user, nobody else has ANY perms on it at all)
		if(! @copy($sSourcePath, $sTempInstallPath))
            $success = false;
        umask($originalUmask); // reinstate original umask

        if(! $success)
            throw new Exception("Unable to copy file to temp install path: $sTempInstallPath", 17);

        // Explicitly set the file owner and permissions of the newly copied file to be the same
        // as the source path.  PHP copy() seems to disregard the settings of the source file and
        // instead use the current proc's umask() and user identity.

        clearstatcache(); // We don't care what the cache says, we want to know the current stat info

        $hSrcStat = stat($sSourcePath);
        $sSrcOwner = $hSrcStat['uid'];
        $sSrcGroup = $hSrcStat['gid'];
        $sSrcMode = $hSrcStat['mode'];

        $hDstStat = stat($sTempInstallPath);
        $sDstOwner = $hDstStat['uid'];
        $sDstGroup = $hDstStat['gid'];
        $sDstMode = $hDstStat['mode'];

        if($sSrcOwner !== $sDstOwner)
            @chown($sTempInstallPath, $sSrcOwner);

        if($sSrcGroup !== $sDstGroup)
            @chgrp($sTempInstallPath, $sSrcGroup);

        if($sSrcMode !== $sDstMode)
            @chmod($sTempInstallPath, $sSrcMode);

        // Now rename the temp path to the final location
		// @silence rename() PHP warnings, we check for failure and throw our own exception
		if(! @rename($sTempInstallPath, $sInstallPath))
			throw new Exception("Unable to rename temp file to $sInstallPath", 21);
	}
public function installFile ( $ sSourcePath , $ sInstallPath ) { // If the temp file doesn't exist, we cannot install it if ( ! file_exists ( $ sSourcePath ) ) throw new NoSuchFileException ( $ sSourcePath , 1 ) ; try { // Create the install directory if needed $ sInstallDir = dirname ( $ sInstallPath ) ; $ this -> installDir ( $ sInstallDir ) ; } catch ( Exception $ e ) { throw new Exception ( "Cannot install file, no such directory no perms to create it: $sInstallPath" , 11 , $ e ) ; } // First move temp path up to a temporary filename on the install drive $ sTempInstallPath = $ this -> findTempSafeInstallPath ( $ sInstallPath ) ; // @silence copy() PHP warnings, we check for failure and throw our own exception $ success = true ; $ originalUmask = umask ( 0377 ) ; // set secure copy() permissions (read-only by current user, nobody else has ANY perms on it at all) if ( ! @ copy ( $ sSourcePath , $ sTempInstallPath ) ) $ success = false ; umask ( $ originalUmask ) ; // reinstate original umask if ( ! $ success ) throw new Exception ( "Unable to copy file to temp install path: $sTempInstallPath" , 17 ) ; // Explicitly set the file owner and permissions of the newly copied file to be the same // as the source path.  PHP copy() seems to disregard the settings of the source file and // instead use the current proc's umask() and user identity. clearstatcache ( ) ; // We don't care what the cache says, we want to know the current stat info $ hSrcStat = stat ( $ sSourcePath ) ; $ sSrcOwner = $ hSrcStat [ 'uid' ] ; $ sSrcGroup = $ hSrcStat [ 'gid' ] ; $ sSrcMode = $ hSrcStat [ 'mode' ] ; $ hDstStat = stat ( $ sTempInstallPath ) ; $ sDstOwner = $ hDstStat [ 'uid' ] ; $ sDstGroup = $ hDstStat [ 'gid' ] ; $ sDstMode = $ hDstStat [ 'mode' ] ; if ( $ sSrcOwner !== $ sDstOwner ) @ chown ( $ sTempInstallPath , $ sSrcOwner ) ; if ( $ sSrcGroup !== $ sDstGroup ) @ chgrp ( $ sTempInstallPath , $ sSrcGroup ) ; if ( $ sSrcMode !== $ sDstMode ) @ chmod ( $ sTempInstallPath , $ sSrcMode ) ; // Now rename the temp path to the final location // @silence rename() PHP warnings, we check for failure and throw our own exception if ( ! @ rename ( $ sTempInstallPath , $ sInstallPath ) ) throw new Exception ( "Unable to rename temp file to $sInstallPath" , 21 ) ; }
Safely install a file

This will first make a temporary copy of the file in the same directory as the
new $sInstallPath.  Thus if there are any problems or delay associated with NFS
or other remote-mounted/virtual file systems, the production file will not be
affected during the copy.  Once the file has been totally copied, an atomic
rename will put it into place.

@param string $sSourcePath
@param string $sInstallPath

@throws NoSuchFileException If $sSourcePath does not exist
@throws Exception if unable to create parent directories of $sInstallPath
@throws Exception if unable to install temp file and/or unable to rename it to the $sInstallPath
Safely install a file