Solved

Access only part of a file

Posted on 2015-01-19
3
91 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now