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;

?>
Edited by affordit
Link to comment
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.