Jump to content

Reading a file without uploading it


affordit

Recommended Posts

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;

?>
Link to comment
https://forums.phpfreaks.com/topic/279463-reading-a-file-without-uploading-it/
Share on other sites

I don't see any uploading going on there, nor do I fully understand what you're looking to do. Mixing up your terminology a bit, perchance?

 

Just in case you're wondering: Reading a file with PHP (from a client computer) without uploading it is impossible. That's like eating a bag of chips, without opening the bag.

Since PHP runs on the server-side it cannot access the file, unless it is uploaded to the server by the client. Same way that you can put the chips in your mouth, without first opening the bag to get to them.

That doesn't really make much sense. In order to get the file to the server, you have to upload it. Whether you do that as a single large file or 20 small files doesn't really make much difference. Splitting it and doing it separate would actually increase the overall upload size due to http overhead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.