public static function createFromExistingFile($source, $destination, $mimeType, $withFilename = true)
    {
        if (is_file($source) === true) {

            if (is_readable($source) === true) {

                // Should we build the path to $destination?
                $pathinfo = pathinfo($destination);

                if (is_dir($pathinfo['dirname']) === false) {

                    if (($mkdir = @mkdir($pathinfo['dirname'], '0770', true)) === false) {
                        $msg = "Unable to create destination directory at '" . $pathinfo['dirname'] . "'.";
                        throw new RuntimeException($msg);
                    }
                }
                
                $pathinfo = pathinfo($source);
                $filename = ($withFilename === true) ? ($pathinfo['filename'] . '.' . $pathinfo['extension']) : strval($withFilename);

                // --- We store the file name and the mimetype in the file itself.

                // filename.
                $len = strlen($filename);
                $packedFilename = pack('S', $len) . $filename;

                // MIME type.
                $len = strlen($mimeType);
                $packedMimeType = pack('S', $len) . $mimeType;

                $finalSize = strlen($packedFilename) + strlen($packedMimeType) + filesize($source);

                $sourceFp = @fopen($source, 'r');
                if ($sourceFp === false) {
                    throw new RuntimeException("Source file '${source}' could not be open.");
                }
                
                $destinationFp = @fopen($destination, 'w');
                if ($destinationFp === false) {
                    throw new RuntimeException("Destination file '${destination}' could not be open.");
                }

                fwrite($destinationFp, $packedFilename . $packedMimeType);

                // do not eat up memory ;)!
                while (ftell($destinationFp) < $finalSize) {
                    $buffer = fread($sourceFp, self::CHUNK_SIZE);
                    fwrite($destinationFp, $buffer);
                }

                @fclose($sourceFp);
                @fclose($destinationFp);

                return new static($destination);
            } else {
                // Source file not readable.
                $msg = "File '${source}' found but not readable.";
                throw new RuntimeException($msg);
            }
        } else {
            // Source file not found.
            $msg = "Unable to find source file at '${source}'.";
            throw new RuntimeException($msg);
        }
    }
public static function createFromExistingFile($source, $destination, $mimeType, $withFilename = true)
    {
        if (is_file($source) === true) {

            if (is_readable($source) === true) {

                // Should we build the path to $destination?
                $pathinfo = pathinfo($destination);

                if (is_dir($pathinfo['dirname']) === false) {

                    if (($mkdir = @mkdir($pathinfo['dirname'], '0770', true)) === false) {
                        $msg = "Unable to create destination directory at '" . $pathinfo['dirname'] . "'.";
                        throw new RuntimeException($msg);
                    }
                }
                
                $pathinfo = pathinfo($source);
                $filename = ($withFilename === true) ? ($pathinfo['filename'] . '.' . $pathinfo['extension']) : strval($withFilename);

                // --- We store the file name and the mimetype in the file itself.

                // filename.
                $len = strlen($filename);
                $packedFilename = pack('S', $len) . $filename;

                // MIME type.
                $len = strlen($mimeType);
                $packedMimeType = pack('S', $len) . $mimeType;

                $finalSize = strlen($packedFilename) + strlen($packedMimeType) + filesize($source);

                $sourceFp = @fopen($source, 'r');
                if ($sourceFp === false) {
                    throw new RuntimeException("Source file '${source}' could not be open.");
                }
                
                $destinationFp = @fopen($destination, 'w');
                if ($destinationFp === false) {
                    throw new RuntimeException("Destination file '${destination}' could not be open.");
                }

                fwrite($destinationFp, $packedFilename . $packedMimeType);

                // do not eat up memory ;)!
                while (ftell($destinationFp) < $finalSize) {
                    $buffer = fread($sourceFp, self::CHUNK_SIZE);
                    fwrite($destinationFp, $buffer);
                }

                @fclose($sourceFp);
                @fclose($destinationFp);

                return new static($destination);
            } else {
                // Source file not readable.
                $msg = "File '${source}' found but not readable.";
                throw new RuntimeException($msg);
            }
        } else {
            // Source file not found.
            $msg = "Unable to find source file at '${source}'.";
            throw new RuntimeException($msg);
        }
    }
public static function createFromExistingFile ( $ source , $ destination , $ mimeType , $ withFilename = true ) { if ( is_file ( $ source ) === true ) { if ( is_readable ( $ source ) === true ) { // Should we build the path to $destination? $ pathinfo = pathinfo ( $ destination ) ; if ( is_dir ( $ pathinfo [ 'dirname' ] ) === false ) { if ( ( $ mkdir = @ mkdir ( $ pathinfo [ 'dirname' ] , '0770' , true ) ) === false ) { $ msg = "Unable to create destination directory at '" . $ pathinfo [ 'dirname' ] . "'." ; throw new RuntimeException ( $ msg ) ; } } $ pathinfo = pathinfo ( $ source ) ; $ filename = ( $ withFilename === true ) ? ( $ pathinfo [ 'filename' ] . '.' . $ pathinfo [ 'extension' ] ) : strval ( $ withFilename ) ; // --- We store the file name and the mimetype in the file itself. // filename. $ len = strlen ( $ filename ) ; $ packedFilename = pack ( 'S' , $ len ) . $ filename ; // MIME type. $ len = strlen ( $ mimeType ) ; $ packedMimeType = pack ( 'S' , $ len ) . $ mimeType ; $ finalSize = strlen ( $ packedFilename ) + strlen ( $ packedMimeType ) + filesize ( $ source ) ; $ sourceFp = @ fopen ( $ source , 'r' ) ; if ( $ sourceFp === false ) { throw new RuntimeException ( "Source file '${source}' could not be open." ) ; } $ destinationFp = @ fopen ( $ destination , 'w' ) ; if ( $ destinationFp === false ) { throw new RuntimeException ( "Destination file '${destination}' could not be open." ) ; } fwrite ( $ destinationFp , $ packedFilename . $ packedMimeType ) ; // do not eat up memory ;)! while ( ftell ( $ destinationFp ) < $ finalSize ) { $ buffer = fread ( $ sourceFp , self :: CHUNK_SIZE ) ; fwrite ( $ destinationFp , $ buffer ) ; } @ fclose ( $ sourceFp ) ; @ fclose ( $ destinationFp ) ; return new static ( $ destination ) ; } else { // Source file not readable. $ msg = "File '${source}' found but not readable." ; throw new RuntimeException ( $ msg ) ; } } else { // Source file not found. $ msg = "Unable to find source file at '${source}'." ; throw new RuntimeException ( $ msg ) ; } }
Create a PersistentFile object from an existing file.

@param string $source The source path.
@param string $destination Where the file resulting from $source will be stored.
@param string $mimeType The MIME type of the file.
@param mixed $withFilename Whether or not consider the $source's filename to be the $destination's file name. Give true to use the current file name. Give a string to select a different one. Default is true.
@throws \RuntimeException If something wrong happens.
@return \qtism\common\datatypes\files\FileSystemFile
Create a PersistentFile object from an existing file .