Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Reading a file without uploading it

$
0
0

I have a script to split video files into chunks and and write each chunk to a separate file, and another one to reassemble the files.

What I can't figure out is how to read the file 1 chunk at a time without uploading it, does anyone have any idea how this might be done, or at least point me in the right direction?

// File to split
$filename = "";

// Target folder. Splitted files will be stored here. Original file never gets touched.
$targetfolder = 'uploads';

// File size in Mb per piece/split.
// For a 200Mb file if piecesize=10 it will create twenty 10Mb files
$piecesize = 10; // splitted file size in MB


$buffer = 1024;
$piece = 1048576*$piecesize;
$current = 0;
$splitnum = 1;

if(!file_exists($targetfolder)) {
    if(mkdir($targetfolder, 0777)) {
        //echo "Created target folder $targetfolder".br();
    }
}

if(!$handle = fopen($filename, "rb")) {
    die("Unable to open $filename for read! Make sure you edited filesplit.php correctly!".br());
}

$base_filename = basename($filename);

$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
if(!$fw = fopen($piece_name,"w")) {
    die("Unable to open $piece_name for write. Make sure target folder is writeable.".br());
}
//echo "Splitting $base_filename into $piecesize Mb files ".br()."(last piece may be smaller in size)".br();
//echo "Writing $piece_name...".br();
while (!feof($handle) and $splitnum < 999) {
    if($current < $piece) {
        if($content = fread($handle, $buffer)) {
            if(fwrite($fw, $content)) {
                $current += $buffer;
            } else {
                die("filesplit.php is unable to write to target folder. Target folder may not have write permission! Try chmod +w target_folder".br());
            }
        }
    } else {
        fclose($fw);
        $current = 0;
        $splitnum++;
        $piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
        //echo "Writing $piece_name...".br();
        $fw = fopen($piece_name,"w");
    }
}
fclose($fw);
fclose($handle);
//echo "Done! ".br();
exit;

?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles