Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access only part of a file

Posted on 2015-01-19
3
Medium Priority
?
100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 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 111

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

722 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