Hi,
I am working on 2MB ASCII file. I am opening and read from php script and upload the content of this ASCII file into mysql database. I am doing this via command prompt on Redhat Linux Ent 4, PHP 5.2.0 and Mysql 5.0.22. It is ok with smaller ascii file. When i try this on 2MB file, i get this error message

FATAL: emalloc(): Unable to allocate 500001 bytes

when i ran the script second time i get this

FATAL: erealloc(): Unable to allocate 2150442 bytes

Here are the Resource limit in php.ini

max_execution_time = 30
max_input_time = 60
memory_limit = 80M

Below is the script i wrote, please excuse me for coding style (i am not a programmer and i have learnt a lot in past 7 months) i hope you will be able to understand my script

#!/usr/local/php/bin/php
<?php
include("config.php"); #Database connection hadling

#To list files in a directory
$ba_ear_dir="/home/userhome/ba/ba.dpl";
$ba_ear_ascii_ext="asc";
$ba_ear_ascii_ary = glob("$ba_ear_dir/*.$ba_ear_ascii_ext");

foreach ($ba_ear_ascii_ary as $order => $file){
$order++;

#Filename
$myFile = $file;
print "Uploading file $myFile\n";

$fh = fopen($myFile, 'r');

#To read entire file until EOF....
$file_lines = array();
$i = 0;
while (!feof($fh)){
$line = fgets($fh,500000);
$file_lines[$i] = $line;
$content .= $file_lines[$i];
$i++;
}

$content = mysql_real_escape_string($content);
$title = $file_lines[0];
$titles = preg_split("/:/", $title, 3);
$title = $titles[2];
$create_date = $titles[1];
$filename = $titles[0];

#To the file size......
$filename_asc = "$filename.asc";
$file_size_asc=filesize($filename_asc);

$filename_pdf = "$filename.pdf";
$file_size_pdf=filesize($filename_pdf);

$filename_wpd = "$filename.wpd";
$file_size_wpd=filesize($filename_wpd);

#To insert title....
$query = "INSERT into ER_PROHIBIT (filename, title, content, create_date, asc_size, pdf_size, wpd_size) values ('$filename', '$title', '$content', '$create_date', $file_size_asc, $file_size_pdf, $file_size_wpd)";

#print "$query";

$queryres=mysql_query($query, $D😎;

if($queryres){
print "Success";
}
else{
print "error";
}
fclose($fh);
unset($content);
unset($title);
unset($titles);
unset($create_date);
unset($file_lines);
unset($file_size_asc);
unset($file_size_pdf);
unset($file_size_wpd);
unset($create_date);
}

?>

    Hi,
    I changed memory_limit = 300M in php.ini and i am not getting any error messages, but the file is not getting uploaded to mysql.

    Is there any limit to insert command length or Mysql database limit to size?

    How do i find out the error with regard to php/mysql error messages from script?

    I have enabled "error_reporting = E_ALL", still i am not able to get the error details for mysql.

    Thanks in advance.
    PHSHSS

      Is this file in a specific format? You might be able to use MySQL's "LOAD DATA INFILE" syntax so that PHP doesn't have to deal with it.

      Alternatively, if you're inserting the values one by one, you can use [man]mysql_error/man after each query if an error occurs. The common way I use this is:

      mysql_query($query) or die('MySQL error: ' . mysql_query() . "<hr>$query");

        To find out what MySQL might be complaining about:

        $queryres=mysql_query($query, $DB) or die("MySQL error: " . mysql_error() . "<br>\nQuery: $query");
        

          Please don't cross-post (or post duplicate threads about) the same issue. Thanks.

            Write a Reply...