Large .PDF Files Not Uploading To MySQL Database as Medium BLOB Via PHP, Files under 2MB Work Fine

后端 未结 5 1612
温柔的废话
温柔的废话 2021-01-03 05:14

I am developing a PHP script for uploading .PDF documents as medium BLOBs into a MySQL database via PHP. The script also allows users to search for files and open/download

相关标签:
5条回答
  • 2021-01-03 05:24

    YOu might be running up against the limitations of the HTTP request (which I think is 4MB), or your HTTP request might be timming out. I have run into this issue before. You might want to look @ your php.ini file for maximum upload size.

    Disclaimer: I am not a PHP developer :D

    0 讨论(0)
  • 2021-01-03 05:32

    I actually fixed the issue. I changed all of the values that were recommended here in php.ini and my.cnf but I also needed to change a setting for PDO.

    I changed: PDO::MYSQL_ATTR_MAX_BUFFER_SIZE (integer) Maximum buffer size. Defaults to 1 MiB.

    This has to be set when the PDO object is created to work though. All is good now.

    0 讨论(0)
  • 2021-01-03 05:35

    You might be running into a mySQL limit.

    Run the following in mySQL:

    show variables like 'max_allowed_packet'  
    

    That will show you your current setting. The default is 1047552 bytes.

    This can be changed with your my.cnf file. Simply add

    set-variable = max_allowed_packet=2M
    

    or what ever size is required and restart mySQL.

    EDIT:
    What about

    <input type="hidden" name="MAX_FILE_SIZE" value="xxx" />
    

    Might that be part of the problem?

    0 讨论(0)
  • 2021-01-03 05:38

    So to recap all the things I believe I had to set in order to allow file uploads >1MB on something I was working on:

    php.ini

    • Changed post_max_size (I wanted 15MB file uploads, so I set it to 20M just in case)
    • Changed upload_max_filesize to 15M
    • Left max_execution_time at 60
    • Left max_input_time at 60
    • Left default_socket_timeout at 60

    my.ini

    • Changed max_allowed_packet to 15M

    PHP Code

    • changed my PDO call to:

      new PDO("mysql:host=$host;dbname=$db_name", $db_username, $db_password, array (PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>15*1024*1024))

    0 讨论(0)
  • 2021-01-03 05:44

    If you're trying to do this on shared hosting (where you can't make changes to MySQL conf files, etc), Google 'mysqli prepared statements'.

    This allows you to upload large queries in predefined byte chunks, circumventing any buffer restrictions.

    0 讨论(0)
提交回复
热议问题