Reputation: 2535
I have to upload around 3000 images into mysql database, on Apache server. (I know some of you will say it is not wise to insert images to table instead of using url, but i was forced to do that) Anyway, my script is working well when I am uploading around 20 images. When I am trying to upload around 40 images, it insert only 18-20 images. If I am trying to upload 50+ images, no image is inserted and the script not working at all. I tried to increase the "max" upload/import/session/query values in some configuration files such "php.ini", "conf" etc, but I think I am missing the correct fix.
Can somebody give me direction?
My source code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="userfile[]" multiple/>
<input type="submit" value="Submit"/>
</form>
PHP:
if(!isset($_FILES['userfile']))
{
echo '<p>Please select a file</p>';
}
else
{
try {
upload();
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}
function upload(){
foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){
$imgfp = fopen($_FILES['userfile']['tmp_name'][$key], 'rb');
$nameTemp = $_FILES['userfile']['name'][$key];
$name = preg_replace( '/\.[a-z0-9]+$/i' , '' , $nameTemp );
/*** connect to db ***/
$dbh = new PDO("mysql:host=localhost;dbname=testYossi", 'root', 'password');
/*** set the error mode ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** our sql query ***/
$stmt = $dbh->prepare("INSERT INTO pictures (picNmae , picData) VALUES (? ,?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
$stmt->execute();
}
}
Upvotes: 0
Views: 1103
Reputation: 843
Have you checked your upload_max_filesize
and max_file_uploads
values in the configuration of php? Use phpinfo()
.
Upvotes: 1