Solved

Access only part of a file

Posted on 2015-01-19
3
93 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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