Forum Moderators: coopster

Message Too Old, No Replies

Segmenting a file?

         

daperson0

11:04 pm on Feb 17, 2008 (gmt 0)

10+ Year Member



Here's another problem. My php system needs to be able to take really huge files, like 50Mb or even more, and read them into a string. I've already concluded it's impossible to put them into one string, and am trying to read the file into an array of segments - just have no idea how. fread dosn't have the facility to start from the nth byte and read for a certain distance.
So.. any suggestions?

phparion

6:23 am on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



due to forum charter I cannot give you the link to an article that can help you really. What you need to do is google "read large file in parts php" and in the results go to the PHP FOUNDATION article. You will see how does the author read large file in chunks.

vincevincevince

7:12 am on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The key is that fread take a number of bytes at a time; take the number you can handle, process them and then reuse the same file handle to read the next set.

daperson0

12:35 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



Ah, thanks, i never knew file handles worked like that (i'm kinda bad at php :P)

daperson0

7:24 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



Erm.. new problem. I'm uploading big files to php - here is my upload script:

<?php
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);
?>

Simple one liner, it just makes the file. The problem is, with large files it seems to throw a HTTP 500 Internal Server Error and die rather hideously.
Any ideas?

PHP_Chimp

11:10 pm on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont understand why this would cause a 500 error :( So I'm only guessing...

Have you looked in your logs to see if there is another error getting put in there by php? Or to see if there is any more info on what is killing the server?

It has also occurred to me that if you still have an active file handle then that file may well be locked. So have you closed all of your file handles before you are trying to move that file?
For example -


$fh = fopen('somefile.txt', 'r');
move_uploaded_file('somefile.txt', 'somefile1.txt');
// may well fail, as $fh may well be locking somefile.txt
$fh = fopen('somefile.txt., 'r');
fclose($fh);
move_uploaded_file('somefile.txt', 'somefile1.txt');
// should work as there is no lock on somefile.txt

I dont know if that is your problem, however it may be worth checking just in case.

You may also want to check out rename [uk.php.net] just in case the 500 error is just a random problem with move_uploaded_file function.

vincevincevince

12:40 am on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm guessing it's a php.ini setting, there are settings for maximum file size. Increase those to cover it, restart PHP.

phparion

7:28 am on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



also try set_time_limit(0); on the top of your code to make sure your code session does not expire before completing the process.

daperson0

11:54 am on Feb 19, 2008 (gmt 0)

10+ Year Member



ooh.. thanks guys - tracked it down to a setting in the php.ini that limits the filesie to 8MB. Unfortunatly, i do not have access to the php config file on this server. Is there a workaround?

PHP_Chimp

7:07 pm on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may well be able to change this in an .htaccess file.
Something like -

php_value upload_max_filesize 20M
php_value post_max_size 20M
# php_value followed by whatever directive you want to alter.

However you may find that your host will block you from making these changes within htaccess. As they dont want you uploading large files that chew up there server.

If that doesnt work then there is a sneaky way to get around it if you have shell access to the server...although if you use this then you will annoy your host so be prepared.
If the htaccess doesnt work and you have shell access then come back and I'll let you know (but dont blame me when you get a kicking from your provider).

If you search for 'increasing max file size php.ini shell access' on the web then you will come across a few articles that tel you how to alter php.ini if you have shell access...so then you can find them and not blame me when you get a kicking from your provider ;)

daperson0

11:43 pm on Feb 19, 2008 (gmt 0)

10+ Year Member



I don't have shell access :P, and .htaccess doesn't work.
Grreat...
Now another problem has popped up. Now the script claims everything is hunky-dory, yet the file simply isn't there, sometimes with files smaller than tha limit!