How can I insert large files in MySQL db using PHP? - php

I want to upload a large file of maximum size 10MB to my MySQL database. Using .htaccess I changed PHP's own file upload limit to "10485760" = 10MB. I am able to upload files up to 10MB without any problem.
But I can not insert the file in the database if it is more that 1 MB in size.
I am using file_get_contents to read all file data and pass it to the insert query as a string to be inserted into a LONGBLOB field.
But files bigger than 1 MB are not added to the database, although I can use print_r($_FILES) to make sure that the file is uploaded correctly. Any help will be appreciated and I will need it within the next 6 hours. So, please help!

You will want to check the MySQL configuration value "max_allowed_packet", which might be set too small, preventing the INSERT (which is large itself) from happening.
Run the following from a mysql command prompt:
mysql> show variables like 'max_allowed_packet';
Make sure its large enough. For more information on this config option see
MySQL max_allowed_packet
This also impacts mysql_escape_string() and mysql_real_escape_string() in PHP limiting the size of the string creation.

As far as I know it's generally quicker and better practice not to store the file in the db as it will get massive very quickly and slow it down. It's best to make a way of storing the file in a directory and then just store the location of the file in the db.
We do it for images/pdfs/mpegs etc in the CMS we have at work by creating a folder for the file named from the url-safe filename and storing the folder name in the db. It's easy just to write out the url of it in the presentation layer then.

Some PHP extensions for MySQL have issues with LONGBLOB and LONGTEXT data types. The extensions may not support blob streaming (posting the blob one segment at a time), so they have to post the entire object in one go.
So if PHP's memory limit or MySQL's packet size limit restrict the size of an object you can post to the database, you may need to change some configuration on either PHP or MySQL to allow this.
You didn't say which PHP extension you're using (there are at least three for MySQL), and you didn't show any of the code you're using to post the blob to the database.

The best answer is to use an implementation that is better and also works around that issue.
You can read an article here. Store 10MB, 1000MB, doesn't matter. The implementation chunks/cuts the file into many smaller pieces and stores them in multiple rows.. This helps with load and fetching so memory doesn't also become an issue.

You could use MySQL's LOAD_FILE function to store the file, but you still have to obey the max_allowed_packet value and the fact that the file must be on the same server as the MySQL instance.

You don't say what error you're getting (use mysql_error() to find out), but I suspect you may be hitting the maximum packet size.
If this is the case, you'd need to change your MySQL configuration max_allowed_packet

You don't say what error you're getting (use mysql_error() to find out), but I suspect you may be hitting the maximum packet size.
If this is the case, you'd need to change your MySQL configuration max_allowed_packet
Well I have the same problem. And data cannot be entered in the mysql database chunck by chunck in a "io mode"
loop for :
read $data from file,
write $data to blob
end loop
close file
close blob
A solution seems to create a table with multi-part blobs like
create table data_details
(
id int pk auto_increment,
chunck_number int not null,
dataPart blob
);
???

Related

How can download files from longblob mysql without (extension,type,size) fields

I searched a lot about how to download files from longblob mysql but all the code requests (extension,type,size) and i do not make These fields and i do not want make it . SO, Is there any way to download any file type (jpg,gif,pdf,png,txt,ect..) from the database without (extension,type,size) fields ?
It's not a good idea to save a file in your database. It's an old tradition. Try to come outside. Save the file in the directory and after that save the file name or path in the database. This will decrease the load on the database and also decrease unusual use of the db storage
A couple things here:
Do NOT store files in the database. The database is for data, the
file system is for files. The smart way to do something like this is
to have a varchar storing /path/to/your/file.foo
Unless you plan to examine each file (costly and slow), you benefit GREATLY from just storing this data for re-use in the future

creating a text file is good or saving it to database is good?

I am creating an application where users will post texts. Currently I am creating the text files of that texts post but i am not sure if storing it to MySql table is good or creating a txt file is good. .
Note:
1) There is no limit of the size of text post. Users may post a very big text or user me post containing only a letter.
2) There is no limit of post a user can upload.
Please Help
Thank You. .
Saving data into a file is more cost operation rather than in database.So its better to go for database.Because saving in file will do IO operation and this file I/O operation is really very costly in terms of computation and from OS perspectives.
You should store it in the database instead of text file. There are many reasons for it. Go through this question for more descriptive answer. In short, you should choose database over files for security reason, speed, code maintainability and concurrency.
You can use LOAD DATA INFILE to read the contents of a file and store it in a table in the database in a structured format.
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
You can also use largetext in mysql.But it would be slower and non-resilient.
Please check here
See there are some major things you should know before taking decision on where to save things
If the file is an image/Video/PDF
For this you should save the FILE NAME into the database and the image on server
If the file is a large text
For this you should save the file to the server as the database will not be able to take loads of text and many requests for long text at the same time and this will also save you space for more things in database
For any other thing, if you find it space consuming just save it on the server as this will give you space for database and more advantages

Partial data missing from an image file retrieved from MSSQL DB

I'm trying to show image from DB saved in Image field. But I can see only 63KB of image.
This is my code:
$sql='SELECT Photo FROM Personel WHERE ID_USER = '.$id;
$result=mssql_query($sql);
$res=mssql_fetch_assoc($result);
header('Content-type:image/jpeg');
echo$res['Photo'];
As you can see nothing special, so I think problem in DB or server configuration.
DB installed on Windows XP server.
Actually it's Access control systems DB, and SQL files installed in that program's folder, but don't know this information useful or not.
Any idea?
UPDATE
Screenshot for Photo field type:
had the same problem, just run this first:
mssql_query('SET TEXTSIZE 10000000'); // sets limit to 10MB
It's likely that you're using a data type (e.g. blob) for the Photo field that is too restrictive for the size of images you're trying to save. Each data type has different limits on the amount of data that can be stored in that field.
See this page in the MySQL documentation for an example of some different field types and their associated storage capacity / suitability.
The blob type, for instance, has a limit of 2^16 bytes (64K).
If your images are larger than that (quite likely), try changing the field type to something like mediumblob (2^24 = 16.78MB).
If you're dealing with really large images then you may need longblob (2^32 = 4.29 GB) instead, but at that stage you're going to have more than just data storage problems :)
Check if you have limitation on field Photo.
Given that your comments say that the entire image is being stored in the DB, it's possible that the issue is related to a data limit / timeout within PHP and / or the webserver.
EDIT: I've left the following part of the answer here for historic info (to show what else we tried) but please see the comments below, as it's more likely that the issue is related to a PHP or webserver config setting, limiting the size of the response data. Still not fully resolved yet.
I notice you have a Content-type header, but not a Content-length header.
You could try adding a Content-length header with some arbitrary, known value (e.g. 12288) and see if this affects the size of the file returned (since you know that it's currently 63K - does it change to 12K?)
If it does, then you'll need to get PHP to calculate / find out the actual size of your image and add a Content-length header of that length.

Passing Binary Data to a Stored Procedure in SQL Server 2008

I'm trying to figure out a way to store files in a database. I know it's recommended to store files on the file system rather than the database, but the job I'm working on would highly prefer using the database to store these images (files).
There are also some constraints. I'm not an admin user, and I have to make stored procedures to execute all the commands. This hasn't been of much difficulty so far, but I cannot for the life of me establish a way to store a file (image) in the database.
When I try to use the BULK command, I get an error saying "You do not have permission to use the bulk load statement." The bulk utility seemed like the easy way to upload files to the database, but without permissions I have to figure a work-a-round.
I decided to use an HTML form with a file upload input type and handle it with PHP. The PHP calls the stored procedure and passes in the contents of the file. The problem is that now it's saying that the max length of a parameter can only be 128 characters.
Now I'm completely stuck. I don't have permissions to use the bulk command and it appears that the max length of a parameter that I can pass to the SP is 128 characters.
I expected to run into problems because binary characters and ascii characters don't mix well together, but I'm at a dead end...
Thanks
In general, we don't pass binary data in SQL. We upload the file to the server, then load the image from the server into the database.
Load the image into the database from a file:
UPDATE images
SET image = LOAD_FILE('images/myimage.jpg')
WHERE image_id = 1234
Get the image back out to a file:
SELECT image
INTO DUMPFILE 'images/myimage.jpg'
FROM images
WHERE image_id = 1234
Here is an example I've found in David Hayden's blog.
It's a c# example, but the steps should be similar in PHP:
Convert your uploaded file to a byte array
Execute dynamic TSQL on the server

copy mysql blob field from one database to the other

I happen to have a database with pictures stored as blob fields. Can't help it, it was the previous developer's choice.
Now I need that data in a new site and the provider won't let me copy the data the easy way (file has become 11Mb big - won't upload that and I don't have shell access).
So I thought I'd write a script that opens a connection in db1, selects all the records, and then copies each into a table in the new db2.
Works all fine if I exclude the blobs. If I want to copy them too, it won't insert.
Anyone had something similar before?
Should I treat the blobs differently when it comes to inserting?
Thanks for any ideas or help.
11MB isn't a huge file, I'm surprised your host has such a low max upload size.
Have you thought about exporting as SQL, splitting the file in two (in Notepad++ or something) then uploading it in smaller sections? Wouldn't take long.
Perhaps check to see if you can increase the max_allowed_packet setting on your mysql DB. I'm not sure if it affects inserts, but I remember having to adjust this setting when I worked on a web-app that allowed users to download 3-5MB binaries from blob fields in the DB.
This link may be helpful, from a quick google search: http://www.astahost.com/info.php/max_allowed_packet-mysql_t2725.html

Categories