0

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();       
    }               
}
2
  • If you have increased the values for individual uploads, max uploads then perhaps look at the timeout function. You might simply be taking too long to upload all the data and then the script times out. Commented Sep 1, 2013 at 9:10
  • Thanks Fluffeh, how can I increase the max time upload? Where/What is the correct configuration file and variable? Commented Sep 1, 2013 at 9:13

1 Answer 1

1

Have you checked your upload_max_filesize and max_file_uploads values in the configuration of php? Use phpinfo().

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, using phpinfo helped me to find all important parameters such what you suggested and others (max_execution_time, max_input_time...). I don't know what exactly fixed it but now all good, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.