Partial data missing from an image file retrieved from MSSQL DB - php

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.

Related

Storing images to sql - specific scenario

I have a very specific scenarion of storing images to mySql DB via PHP. Each image is send to server via POST request in JSON base64 encoded with other attributes and has in average 1MB. Image is written via PHP script to one dedicated table for images as string. Then it is usually read right after because of synchronization. Then it can be read several times in next 24 hours. Up to 20, average 2-3 times. Then image is not read anymore, only in special case it can be read by administrator. There are in average written 100-500 images in 24 hours range. Images older then 1 year are deleted.
In this case does it has benefit to change it and store images in file system ?
a) Store it via PHP script in file system as copy of received base64 data (needs more space)
b) Store it via PHP script in file system as jpg image (needs more performance)
Api via POST/JSON can not be changed
I read a lot of articles about such problems, but I am asking specificaly for my scenario if it is worth to change it, and what will be gained.
There are a lot things you must consider in order to know what is the best solution for you. Both approaches have their pros and cons. You gave to little information about your scenario. What's the average size os those images? What's the database size ?
You should check this thread:
Storing Images in DB - Yea or Nay?

Is there a limit to the number of characters in an HTML textbox?

The user is able to upload an image which will have a massive amount of image data. This image data is going to be stored temporarily on the browser in the textbox. Once the user submits the form to the PHP server using POST, I'm going to add the contents of that textbox to a blob in the database.
My question: Is there a maximum limit to the amount of characters an HTML textbox can take?
I read online somewhere that I won't be able to POST this textbox to the server (with PHP).
There are limits on client computer (limits of virtual memory) and there can be and usually is a limit on server-side of how many bytes can be received. POST limit size on Apache can be viewed and changed like answered in this question.
The preferred way of sending file is using multipart/form-data and convert it to blob there in server before storing in DB.
As of i know, we can store any amount of data in the input type text. There is no limit on the browser side. With POST, there is no technical limit in the browser, but usually one on the server side - see e.g. Apache's LimitRequestBody, PHP's post_max_size and so on.
from specs:
MAXLENGTH
The maximum number of characters that will be accepted as input. This can be greater that specified by SIZE , in which case the field will scroll appropriately. The default is unlimited.

PHP and SQL Server - Retrieve image from an image field

So I have a webcam widget to take snapshots from guests. My guest table have an image field and I want to store this pic on it. I tried storing raw binary data, base64 encoded data and it didn't works. I don't know if database is receiving correct data or if I don't dealing with returned data well. I'm using PDO in my app and I've read on the web that it can have an issue with data larger than 64 kb, anyone knows if it's true?
This is how I got the image data from the webcam:
$imagem = file_get_contents('php://input');
Like I said above I tried to store image's pure data $imagem and then this way:
$data = #unpack("H*hex", $imagem);
$imagem_bd='0x'.$data['hex'];
And for both of that I've tried encoding with base64 too, but none worked.
Just for information: This app I'm working is a remake of another similar app that have this feature too, but on this old app I's used TSQL and it works fine.
Thanks in adv.
EDIT
For now I have done the insert well, but I can't retrieve and render the image at the browser.
you can encode images in blob instead of base64. But i recommend you to store image as file and store the link to the image in database. As storing images in database may consume large space and also returning it can eat your server and also it may take more time to load compared to file system.
If you use PreparedStatement, all you need to do is use bindValue, with your binary image:
$sql = "INSERT INTO your_table (image) VALUES (?)";
$stmt->bindValue(1, $imagem);
$stmt->execute();
Anyway, as said here, it's not a good idea to store images in the database.
About the 64KB limit, it depends of the data types of your columns in the table (in MySQL, the BLOB type has a limit of 64KB, but there is another types of data, like MEDIUMBLOB and LONGBLOB).

Image save in database

I want to save an uploaded image to database. Now my question is:
What should be the data type for SQL Server 2000 for image?
a. image
b. varbinary
what is the code for saving image in database?
How to retrieve the image from database and show?
Please refer me any tutorial or guidline. Or if you can please share with me.
Thanks in advance.
The data type should be text because the best way to save an image to a database is to save its path. Let your OS do the job of storing the actual files.
Typically on SQL Server, you would use a BLOB, Binary Large OBject, to store images. We used it for Word documents on a previous project, and it worked just fine. See this article on Database Journal for more info, although a quick Google for the BLOB type will throw up lots more examples.
I wrote this article a while back on this subject. It should help you with #2 and #3. It uses MySQL, but the basics are the same, you just need to replace the MySQL calls with the MSSQL calls.
As to #1, I would go with the obvious choice: "image".
I am not 100% sure of the differences between the two, however. It just seems obvious :)
Edit, according to this, it appears that the image datatype is deprecated. It will be removed in future versions. Not sure how much this affects you, seeing as you are using a 10 year old version, but it is worth keeping in mind.
In SQLServer IMAGE or VARBINARY are allmost the same. IMAGE is 2GB but VARBINARY() needs a length argument.
It's not good idea to store images in a database, the size increases a lot, with each Backup you need to save all images, with increasing size also increases the time to perform a Backup and Restore. You also need to change the network packet size (at the server properties, advanced, network, network packet size) In recents SQLServer versions the most adecuate datatype is varbinary(MAX)

How can I insert large files in MySQL db using 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
);
???

Categories