I Am not able to read 84987931 bytes file

Discussion in 'PHP' started by Subikar, May 26, 2007.

  1. #1
    I am trying to read the content of a file whose size is 84987931 bytes with a php function file();

    It is giving error :
    Fatal error: Allowed memory size of 83886080 bytes exhausted (tried to allocate 4097 bytes) in "filepath/index.php" on line no 7.

    I have tried with this code too :

    $handle = @fopen('file_path', "r");
    if ($handle) {
    while (!feof($handle)) {
    $lines[] = fgets($handle, 4096);
    }
    fclose($handle);
    }

    But the same error is giving.

    Any one know any function so I can read a long file any how.

    Thanks in advance for any help :)

    Subikar Burman
     
    Subikar, May 26, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    It seems a limitation set by your hosting company.
    Do you really need to read the file completely before to process ?
    How about processing while reading ?, that's inside your while loop.
     
    ajsa52, May 26, 2007 IP
  3. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    After few minutes it is giving error what I mention in my first post.
    Is there any solution ? or function I can read it?
     
    Subikar, May 26, 2007 IP
  4. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I beleive you can modify this option in the php.ini file, I cant remember what the actual command is, but im sure google will relish some answers.
     
    HuggyCT2, May 26, 2007 IP
  5. nks

    nks Well-Known Member

    Messages:
    1,602
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    160
    #5
    If he can gain access to php.ini file, then it can be modified to increase the memory limitation. If not, then it is down to the hosting company to modify that for him.
     
    nks, May 26, 2007 IP
  6. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Is it possible for doing it from programming site. Actually I don't want to do any change in php.ini. If it is possible to do by any php coding then it can be helpful.
     
    Subikar, May 26, 2007 IP
  7. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #7
    You can find several examples of managing large files on this page: http://es.php.net/fgets
    I like the code from hackajar on that page, however probably you don't need a buffer so huge (16Mb).
     
    ajsa52, May 26, 2007 IP
  8. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    What are you trying to do with the file? Your best bet is to probably read it in chunks with a while() loop. If need-be you can store it in a database for processing, but I can't think of any reason you'd really need to handle the entire chunk of data at a time.
     
    projectshifter, May 26, 2007 IP
  9. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Asking your host to increase your memory limit will only work until you run into another file that's even larger than *that* limit. As a couple people have said, the clever solution to this problem is to process the file in chunks. There is some sample code in this thread that might help you:

    http://forums.digitalpoint.com/showthread.php?t=325513
     
    lemaitre, May 26, 2007 IP
  10. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #10
    use ini_set() function to change the file size limitation.but there is a problem.u have to set the php.ini to change its setting by using ini_set().so anyhow u have to change php.ini ;)
     
    coderbari, May 28, 2007 IP
  11. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #11
    OK guys I got the option
    $ic and ic_max and within this two range I am using fgets() and fetching the data.

    Thanks a lot for you all kind cooperation. Thanks again :)
     
    Subikar, May 28, 2007 IP