Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Access only part of a file

I'm trying to import a lot of files into a database as the files are 750Gb, but using 10.5Tb of space (lots of small files). So I want to put them into several sqlite database's to reduce the space used on disk (I cant change the cluster sizes).

Instead of putting the entire file into a blob field I want to split the file into multiple segments of like 20Kb and then put each of the segments into the SQLite database and then when it comes to redownloading the file re-assemble the file.

Ive done this using vb.net before quite successfully, but now I want to do it in PHP, however Im unable to find any information on how to open a file and read the first 20000 bytes, then the next and next until the end.

Has anyone got any pointers on how to access a file and read a section of it?

Thank you
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can read a partial file with an offset from the normal starting point using file_get_contents()

http://php.net/manual/en/function.file-get-contents.php

Some combination of fgets() and file_get_contents() will probably fill the bill!
Avatar of tonelm54
tonelm54

ASKER

Thanks for the comments, for the record Ive got the following working well so just need to add the SQL code now (which is the easy part)

<?php    
    $handleR = @fopen("C:\Rob\wordpress.jpg", "r");
    $handleW = @fopen("C:\Rob\wordpress2.jpg", "w");
    
    $length = 20000;
    
    while ($buffer = fgets($handleR, $length)) {
        fwrite($handleW, $buffer);
        }
        
    if (!feof($handleR)) {
        echo "Error: unexpected fgets() fail\n";
        }
    fclose($handleR);
    fclose($handleW);
?>

Open in new window