Solved

Access only part of a file

Posted on 2015-01-19
3
94 Views
Last Modified: 2015-01-19
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
0
Comment
Question by:tonelm54
3 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 40558285
You can read a partial file using fgets().

See:
http://php.net/manual/en/function.fgets.php

For the details and options.

Cd&
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40558364
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!
0
 

Author Comment

by:tonelm54
ID: 40558375
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

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question