Download image from blob field to hard disk using php - php

I have a mysql blob field in which our team stored an image for every record. Now I want to download all images on my hard disk through php script. A prompt response will be appreciated.
Best Regards...
Aisha

$result=mysql_query("SELECT * FROM test WHERE 1");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$filename = rand(1, 100).'.txt';
file_put_contents('./'.$filename, $row['blob']);
}
This is working (tested).
It wil save the file under a random filename (1-100.txt).
You can change the filename by changing $filename.
And here is a simple sample MySQL-table with 2 demo files with content Test #1 and Test #2:
CREATE TABLE IF NOT EXISTS `test` (`blob` blob NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `test` (`blob`) VALUES(0x5468697320697320746573742023312e), (0x5468697320697320546573742023322e);

you can use file_put_contents,
fetch rows from your database and foreach row write blob/data to file
Error in Writing to Image file from PHP
http://php.net/manual/en/function.file-put-contents.php

You can try this also........
function save_file($file_id, $target_folder) {
$result = $DB->get_file($file_id);
$file_content = $result->fields['your_file_content_field'];
$name = $result->fields['name'];
/* Decode only if the file contents base64 encoded
* before inserting to database.
*/
$file_content = base64_decode($file_content);
return file_put_contents($target_folder.'/'.$name, $file_content);
}

Related

What happens to binary files in file_get_contents?

I'm trying to read a file in php and store it as a varbinary in sql server. The process works for text files, but I'm still having trouble with images.
I'm using the following lines to read the file contents into a string, but I'm not sure that that's not where my problem lies:
$data = NULL;
$validators = NULL;
if($file = file_save_upload('file', $validators, FALSE, 0)){
$data = file_get_contents($file->getFileUri());
$filename = $file->getFilename();
}
Then I pass the $data string to a prepared statement:
$conn = $this->_get_connection();
$sql = "
EXEC JC_Update_Document_SP
...
?,
...;
";
$file_input = [
[$data, NULL, NULL, SQLSRV_SQLTYPE_VARBINARY],
];
$stmt = sqlsrv_prepare($conn, $sql, $file_input);
if(sqlsrv_execute($stmt) === false){
die(print_r(sqlsrv_errors(), true));
}
where the $data field feeds into a varbinary column in a stored procedure in the database.
Another method I tried involved converting the $data varchar to a varbinary(max) field in the database, but either way, I get back a broken image.
So my question is this... is file_get_contents messing up my binary data? How would I read the image file and upload it to the database for later retrieval?
I found the bin2hex function. I'm still unclear on why this is needed, but it gets the job done.

Download to root and after delete files from database

I have this function, and this deletes textfiles after a certain age from my database automatically.
$r = new textfiles;
$db = new DB;
$currTime_ori = $db->queryOneRow("SELECT NOW() as now");
...
if($this->site->textfilesretentiondays != 0)
{
echo "PostPrc : Deleting textfiles older than ".$this->site->textfilesretentiondays." days\n";
$result = $db->query(sprintf("select ID from textfiles where postdate < %s - interval %d day", $db->escapeString($currTime_ori["now"]), $this->site->textfilesretentiondays));
foreach ($result as $row)
$r->delete($row["ID"]);
}
Now I would edit this function so that at first all textfiles are automatically downloaded in a root directory /www/backup and then the script should delete the textfiles with the string $r->delete($row["ID"]);
At the moment I have no idea how I could implement this.
For me it's seems to be impossible to give you an completely answer to your question because leak of informations.
Do you store the whole file-content in database or only the path and filename?
It would help us to see whats the content of "$row" which represents one row from database.
If you just store the filename (and optionally the path) you could use the "copy" (http://php.net/manual/de/function.copy.php) function from php to copy the file to your backup-directory. Please note, you have to ensure that the user who's executing the script or running the web-server have the privileges to write into the directory.
You could add this functionality to class textfiles as as method like makeBackup.
There are few information, but I'll give it a try. If you want to backup the rows before deleting them, you can store them in .txt file in json_encoded form using this piece of code inserted in the FOREACH loop, before delete command:
$myfile = fopen("/www/backup/".$row["ID"].".txt", "w") or die("Unable to open file!");
$txt = json_encode($row);
fwrite($myfile, $txt);
fclose($myfile);
By your approach ..
function delete ($id){
$result = $db->query(sprintf("select * from textfiles where id=$id);
//if you have filepath use copy as SebTM suggested
$path = $row['path']; //assuming path is the column name in ur db
$filename = basename($path); //to get filename
$backup_location = '/www/backup/'.$filename;
copy($path, $backup_location);
//if you have data in db
$content = $row['data'] //assuming data to be backed up to file is in a field 'data'
$backup_location = '/www/backup/file.txt';
file_put_contents($backup_location, $content);
}
But this is not the most optimal approach , you could shift even the initial query into delete function above , and call delete function only once, instead of calling it in a loop ..

Creating, Displaying Images From PHP

Question:
How do I get my blob image to actually work, is there a size limit to what this can handle? I have my sql db set to longblob incase that was the problem, but everything still doesn't parse correctly. I tried dumping the variable and I get a large blob of text, so I'm not sure whats wrong.
Code
$image = getImgFromDB();
header('Content-Type:image/jpeg');
echo $image;
code http://s10.postimg.org/wn8m7k1s8/Screen_Shot_2015_05_08_at_10_51_46_PM.jpg
Research:
http://php.net/manual/en/function.header.php
I need my PHP page to show my BLOB image from mysql database
PHP display image BLOB from MySQL
How to display an BLOB image stored in MySql database?
http://forum.codecall.net/topic/40849-tutorial-storing-images-in-mysql-with-php-part-ii-display-your-images/
Update on Insertion into the DB
The file from the row, which is checked by PHP and JS to make sure that it is the type of image and that it is within a certain size.
$theImageData = file_get_contents($filedata['tmp_name']);
Update 3
Header is text/plain
This is tested and should pin point the problem.
There is not much that can go wrong, so I do not understand why there is a problem.
This checks to see if the image was read in. The length should be the same as the size of the image. And the base 64 will show the rest.
header('Content-Type:text/plain');
$filename = "pitbull3p.jpg";
$handle = fopen($filename, "r");
$image = fread($handle, filesize($filename));
$len = strlen($image);
$base64 = base64_encode($image);
echo "Length=$len\n\n" . $base64;
Result
This is my actual result
Length=1864
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
end of update 3
Update 2
The breaking the string into bytes works. (NOT Recommended) But a string Hex representation does not work. A hex string representation works in an SQL query (without the quotes, not with), but does not work with an echo.
You may be able to pack the string in to a binary string. It is frustrating.
header('Content-Type:image/jpg');
$image = "\xff"."\xd8"."\xff"."\xe0"."\x00"."\x10"."\x4a"."\x46"."\x49"."\x46"."\x00"."\x01"."\x01"."\x01"."\x00"."\x48"."\x00"."\x48"."\x00"."\x00"."\xff"."\xdb"."\x00"."\x43"."\x00"."\x0a"."\x07"."\x07"."\x08"."\x07"."\x06"."\x0a"."\x08"."\x08"."\x08"."\x0b"."\x0a"."\x0a"."\x0b"."\x0e"."\x18"."\x10"."\x0e"."\x0d"."\x0d"."\x0e"."\x1d"."\x15"."\x16"."\x11"."\x18"."\x23"."\x1f"."\x25"."\x24"."\x22"."\x1f"."\x22"."\x21"."\x26"."\x2b"."\x37"."\x2f"."\x26"."\x29"."\x34"."\x29"."\x21"."\x22"."\x30"."\x41"."\x31"."\x34"."\x39"."\x3b"."\x3e"."\x3e"."\x3e"."\x25"."\x2e"."\x44"."\x49"."\x43"."\x3c"."\x48"."\x37"."\x3d"."\x3e"."\x3b"."\xff"."\xdb"."\x00"."\x43"."\x01"."\x0a"."\x0b"."\x0b"."\x0e"."\x0d"."\x0e"."\x1c"."\x10"."\x10"."\x1c"."\x3b"."\x28"."\x22"."\x28"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\x3b"."\xff"."\xc0"."\x00"."\x11"."\x08"."\x00"."\x64"."\x00"."\x64"."\x03"."\x01"."\x22"."\x00"."\x02"."\x11"."\x01"."\x03"."\x11"."\x01"."\xff"."\xc4"."\x00"."\x1b"."\x00"."\x00"."\x03"."\x01"."\x01"."\x01"."\x01"."\x01"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x04"."\x05"."\x03"."\x06"."\x02"."\x01"."\x07"."\xff"."\xc4"."\x00"."\x32"."\x10"."\x00"."\x02"."\x01"."\x03"."\x02"."\x05"."\x03"."\x03"."\x02"."\x05"."\x05"."\x00"."\x00"."\x00"."\x00"."\x00"."\x01"."\x02"."\x03"."\x00"."\x04"."\x11"."\x12"."\x21"."\x05"."\x13"."\x31"."\x41"."\x51"."\x06"."\x22"."\x61"."\x14"."\x32"."\x71"."\x23"."\xa1"."\x15"."\x24"."\x33"."\x81"."\x91"."\x42"."\x52"."\xb1"."\xd1"."\xf0"."\xff"."\xc4"."\x00"."\x16"."\x01"."\x01"."\x01"."\x01"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x01"."\x02"."\xff"."\xc4"."\x00"."\x1b"."\x11"."\x01"."\x00"."\x02"."\x02"."\x03"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x00"."\x01"."\x11"."\x02"."\x21"."\x22"."\x41"."\x61"."\xff"."\xda"."\x00"."\x0c"."\x03"."\x01"."\x00"."\x02"."\x11"."\x03"."\x11"."\x00"."\x3f"."\x00"."\xfc"."\x99"."\x52"."\xb4"."\x58"."\x8f"."\x8a"."\x62"."\x18"."\x73"."\x4c"."\xad"."\xb6"."\x07"."\x4a"."\x8d"."\x12"."\x58"."\xbe"."\x2b"."\x41"."\x17"."\xc5"."\x3f"."\x1d"."\xa6"."\x7b"."\x53"."\x31"."\xd8"."\xe7"."\xb5"."\x41"."\x27"."\x94"."\x45"."\x02"."\x33"."\xe2"."\xac"."\x3d"."\x91"."\x03"."\xed"."\xaf"."\x22"."\xd3"."\x6e"."\x94"."\x12"."\xb9"."\x66"."\x81"."\x1b"."\x55"."\x61"."\x69"."\x9e"."\xd5"."\xaa"."\xd8"."\x64"."\x6c"."\x28"."\x22"."\x04"."\x65"."\x35"."\xba"."\x9c"."\xae"."\x08"."\xa7"."\xda"."\xcb"."\x0d"."\xb8"."\xaf"."\x46"."\xc8"."\x69"."\xce"."\x28"."\x25"."\x34"."\x6b"."\x9e"."\x95"."\x9b"."\xa0"."\x03"."\xa5"."\x51"."\x7b"."\x62"."\xb4"."\xac"."\xb1"."\x9a"."\x09"."\xaf"."\x08"."\x2d"."\x45"."\x32"."\x53"."\x7a"."\x2a"."\x87"."\xad"."\x63"."\x18"."\x06"."\x9f"."\x48"."\x81"."\xda"."\x96"."\xb4"."\x43"."\x81"."\xb5"."\x54"."\xb7"."\x8e"."\x36"."\x60"."\xaf"."\x28"."\x4a"."\x83"."\xec"."\x56"."\xe8"."\x00"."\xda"."\x9b"."\x86"."\x24"."\x1e"."\x2b"."\xea"."\xc5"."\x6e"."\xc4"."\xa2"."\xdc"."\x61"."\x94"."\x6e"."\x06"."\xe6"."\xb7"."\x16"."\xf1"."\xf2"."\xf0"."\x99"."\x77"."\xff"."\x00"."\x71"."\x34"."\x18"."\x4c"."\x21"."\xc8"."\x05"."\x94"."\x7c"."\x50"."\x56"."\x00"."\x00"."\x18"."\xc9"."\xe9"."\xb5"."\x69"."\x1d"."\x8a"."\x86"."\x01"."\x80"."\x24"."\xf9"."\xa7"."\xe3"."\xe1"."\xeb"."\x22"."\xea"."\x00"."\x64"."\x7d"."\xb4"."\x12"."\x79"."\x2a"."\x18"."\x8c"."\x74"."\xaf"."\x58"."\x55"."\xee"."\x05"."\x5d"."\xb8"."\xb0"."\x8b"."\x96"."\xb2"."\xe0"."\x2b"."\x67"."\x04"."\x52"."\x33"."\x59"."\x68"."\x70"."\x1d"."\x40"."\x53"."\xd0"."\xf6"."\x34"."\x42"."\x3c"."\x95"."\x90"."\x65"."\x70"."\x6b"."\x07"."\x81"."\x86"."\x70"."\x2a"."\xd2"."\xdb"."\x7d"."\x3a"."\xe5"."\x54"."\x15"."\xf0"."\x77"."\xc5"."\x27"."\x75"."\x71"."\x14"."\x71"."\xe5"."\xe2"."\x19"."\x53"."\xee"."\x20"."\xe3"."\x6a"."\x09"."\x32"."\x43"."\xb1"."\xc8"."\xa9"."\xf3"."\xc3"."\xb9"."\xda"."\xad"."\x49"."\x71"."\x66"."\xce"."\x15"."\x2e"."\x01"."\xc8"."\xc9"."\x38"."\xc8"."\x5f"."\x8a"."\x5e"."\xe6"."\x14"."\xea"."\xa4"."\x30"."\x23"."\xa8"."\xa2"."\xb9"."\xd9"."\x23"."\xc3"."\x9a"."\x29"."\xd9"."\x62"."\x1a"."\xce"."\xd4"."\x50"."\x37"."\x60"."\xa1"."\x90"."\x6d"."\x5d"."\x67"."\xa6"."\xbd"."\x1b"."\xfc"."\x62"."\x19"."\xee"."\xaf"."\x79"."\x90"."\xc4"."\xa0"."\x84"."\x23"."\x6c"."\x9f"."\x35"."\xc9"."\xd9"."\x48"."\x63"."\xc1"."\x53"."\x82"."\x3b"."\xd7"."\xe9"."\x31"."\x7a"."\xae"."\xdf"."\x83"."\x7a"."\x46"."\x18"."\xa4"."\x90"."\xcd"."\x7d"."\xc9"."\x27"."\x42"."\x8e"."\xfd"."\xb3"."\x55"."\x1c"."\x0d"."\x85"."\x91"."\xb4"."\xba"."\x9a"."\x26"."\x62"."\xec"."\xae"."\x43"."\x39"."\xee"."\x01"."\xda"."\xaf"."\x42"."\x11"."\x23"."\xdc"."\x81"."\xdc"."\xd4"."\x4e"."\x17"."\x33"."\x4e"."\x65"."\x96"."\x42"."\x79"."\x8e"."\xd9"."\x6c"."\xf9"."\xef"."\x55"."\xc0"."\x66"."\x6d"."\x5a"."\x3f"."\x4f"."\x60"."\x76"."\xa8"."\x1f"."\xb3"."\xb5"."\xfa"."\x89"."\xb5"."\x7f"."\xa7"."\xce"."\x29"."\xc9"."\x12"."\x2b"."\x54"."\xd5"."\xb7"."\x5d"."\xb3"."\x58"."\x70"."\xfe"."\x2b"."\x03"."\x40"."\xf0"."\x1c"."\x2b"."\x28"."\xc0"."\xdb"."\x6a"."\xde"."\xe2"."\x48"."\xe7"."\xb4"."\x07"."\x57"."\x51"."\xd3"."\xc1"."\xaa"."\x3e"."\xc6"."\xca"."\xe4"."\x03"."\xee"."\x04"."\xe4"."\x6d"."\x5a"."\xcb"."\x6a"."\x92"."\xa9"."\x52"."\x33"."\x9e"."\xfe"."\x2a"."\x77"."\x06"."\x98"."\x3a"."\x33"."\x48"."\xdb"."\x03"."\xa4"."\x03"."\xe4"."\x53"."\x8f"."\xc4"."\x63"."\xb7"."\xd6"."\xcc"."\x33"."\xe0"."\x03"."\x41"."\x2a"."\x79"."\x4d"."\xa4"."\xdc"."\x89"."\x03"."\x78"."\xd4"."\x46"."\xc4"."\x54"."\xcb"."\xe3"."\x80"."\x4a"."\xef"."\xa7"."\xf7"."\x14"."\xfb"."\xdc"."\x7f"."\x16"."\x66"."\xc6"."\x03"."\x29"."\xdc"."\x9f"."\x14"."\x85"."\xed"."\xb4"."\xb1"."\xc4"."\x70"."\x43"."\xe9"."\xc8"."\x24"."\x1e"."\xa2"."\xa0"."\xea"."\x7d"."\x01"."\xc2"."\x38"."\x74"."\x9c"."\x12"."\x60"."\xf6"."\xf1"."\xcb"."\xce"."\x73"."\xcc"."\x76"."\x19"."\x23"."\xe0"."\x54"."\x1f"."\x52"."\x70"."\x28"."\x78"."\x2f"."\x10"."\x6b"."\x68"."\xa7"."\xd6"."\x8d"."\xee"."\x45"."\x23"."\x75"."\x1e"."\x2a"."\x5f"."\x00"."\xf5"."\x1d"."\xff"."\x00"."\x04"."\x91"."\x5a"."\xda"."\x5d"."\x51"."\xcc"."\xe5"."\x4c"."\x2d"."\xd3"."\x23"."\xbd"."\x5a"."\xe2"."\xdc"."\x7e"."\x5e"."\x31"."\x22"."\xf3"."\x63"."\x48"."\xf2"."\xbe"."\xe5"."\xee"."\x08"."\xaa"."\x39"."\x39"."\x62"."\xfd"."\x43"."\xb5"."\x14"."\xc4"."\xe0"."\x73"."\x4e"."\xf4"."\x54"."\x54"."\xb8"."\xae"."\x74"."\x2d"."\x32"."\xfc"."\x5b"."\xea"."\x30"."\x64"."\x8c"."\xae"."\x18"."\x65"."\x94"."\xf5"."\xa9"."\x48"."\x72"."\x2b"."\x60"."\x3d"."\x80"."\x79"."\x34"."\x15"."\xb8"."\x75"."\xc2"."\x41"."\x6f"."\x70"."\xe5"."\xc0"."\x3c"."\xc1"."\xf9"."\x20"."\xd5"."\x8e"."\x0f"."\xc5"."\x23"."\xbc"."\x52"."\x5e"."\xd2"."\x6f"."\xa7"."\x0c"."\x63"."\xfa"."\x83"."\x22"."\x80"."\x18"."\x63"."\xa2"."\x9c"."\x12"."\x32"."\xca"."\x36"."\xdf"."\x71"."\x80"."\x6a"."\x2d"."\xa8"."\x36"."\xc3"."\x98"."\x98"."\x61"."\xb8"."\x60"."\x7b"."\xd5"."\xee"."\x13"."\x77"."\xf4"."\xf1"."\x38"."\x84"."\xbc"."\x62"."\x56"."\xe6"."\x06"."\x11"."\xa3"."\x80"."\xd8"."\xd9"."\x86"."\xa0"."\x70"."\xdf"."\x23"."\xc5"."\x67"."\x2b"."\xad"."\x2c"."\x57"."\x6f"."\xb6"."\xf6"."\xa2"."\xd7"."\x8a"."\x4b"."\x13"."\xcc"."\x02"."\x03"."\xac"."\x12"."\x73"."\x9c"."\xf6"."\xff"."\x00"."\x9a"."\xe8"."\x23"."\x84"."\x48"."\x8e"."\x50"."\xe0"."\x32"."\x74"."\x1d"."\xab"."\x9c"."\xb6"."\x49"."\x21"."\x94"."\xab"."\xbb"."\x3c"."\x6a"."\x36"."\x2e"."\xa3"."\x27"."\x7d"."\xc7"."\xef"."\x5d"."\x37"."\x0b"."\xb4"."\x91"."\x52"."\x46"."\x32"."\x36"."\xe0"."\x85"."\xc0"."\xe9"."\xfd"."\xab"."\x4c"."\xc9"."\x4e"."\x11"."\x67"."\x24"."\x70"."\xca"."\x59"."\x40"."\xfd"."\x46"."\x3b"."\xef"."\xb5"."\x67"."\xc5"."\x52"."\x11"."\x01"."\x4c"."\x90"."\x47"."\xb8"."\x10"."\x3f"."\x6a"."\xbd"."\x69"."\x08"."\x50"."\xc8"."\x3a"."\x1c"."\xf5"."\x15"."\xcd"."\x71"."\x4b"."\x47"."\x49"."\x9c"."\x8c"."\x95"."\x27"."\x19"."\x3b"."\x11"."\xff"."\x00"."\xba"."\x55"."\x0b"."\x70"."\xb5"."\x10"."\x70"."\xa6"."\x6b"."\x58"."\xd6"."\x4b"."\xa9"."\x9b"."\x62"."\xf1"."\x3c"."\xa1"."\x72"."\x70"."\xa3"."\x0b"."\xb9"."\x27"."\x1b"."\x74"."\x1b"."\x1f"."\x15"."\x25"."\x2e"."\xb8"."\x8a"."\x71"."\x29"."\xc5"."\xdc"."\x11"."\xb7"."\x26"."\x67"."\x89"."\x9e"."\x26"."\x01"."\x1f"."\x49"."\x20"."\x95"."\xcf"."\x6d"."\xa9"."\xee"."\x1f"."\x6e"."\xf6"."\x06"."\x69"."\xd0"."\x30"."\x91"."\xc6"."\x96"."\x63"."\x2b"."\x0f"."\x6f"."\x61"."\xb7"."\x6a"."\x97"."\x73"."\x2c"."\x92"."\xdc"."\x88"."\x36"."\x1a"."\xbb"."\x28"."\xc0"."\x55"."\xc5"."\x63"."\x95"."\xf8"."\xd6"."\xa9"."\x2e"."\x55"."\xb8"."\x9f"."\x0b"."\x03"."\x04"."\x1a"."\x89"."\x04"."\xf5"."\xc9"."\xaa"."\x3c"."\x3b"."\x87"."\xdc"."\x59"."\xa3"."\xbc"."\xf3"."\x19"."\x1d"."\x86"."\xdb"."\xd2"."\xef"."\xa6"."\x32"."\x42"."\x0d"."\xc6"."\x0e"."\x6a"."\x9c"."\x33"."\x73"."\x2d"."\x94"."\xd6"."\x90"."\xa4"."\x80"."\xeb"."\x39"."\xa2"."\x89"."\x7f"."\xa8"."\x77"."\xa2"."\x82"."\x0c"."\x7d"."\x05"."\x30"."\xab"."\xa8"."\x60"."\xf6"."\xa5"."\x62"."\x38"."\x02"."\x9f"."\xb7"."\x50"."\xd4"."\x14"."\x2c"."\x31"."\x24"."\x2c"."\xa7"."\xc5"."\x6b"."\xc3"."\x6f"."\xcd"."\xbc"."\xc2"."\xda"."\x41"."\x90"."\x0e"."\x14"."\x9d"."\xa9"."\x28"."\xa5"."\x6b"."\x59"."\xb5"."\x03"."\xed"."\x6d"."\x8d"."\x69"."\x17"."\x29"."\xee"."\x23"."\x92"."\x59"."\x31"."\x18"."\x6d"."\x6c"."\x31"."\xbf"."\xe2"."\x83"."\xa8"."\x45"."\x59"."\x6e"."\x81"."\x61"."\xfa"."\x43"."\x07"."\xf3"."\x5d"."\x6d"."\xb4"."\x44"."\x28"."\x7c"."\xe0"."\x30"."\xce"."\x3c"."\x57"."\x13"."\x6d"."\x76"."\xb3"."\x13"."\xc9"."\x38"."\x04"."\x6f"."\xf1"."\x5d"."\x6d"."\xad"."\xd1"."\x36"."\xe9"."\xcc"."\x3a"."\x17"."\x48"."\x38"."\xfe"."\xd5"."\x50"."\xda"."\xa1"."\x8a"."\x63"."\xa4"."\x8d"."\x27"."\xcd"."\x25"."\xc4"."\x61"."\x53"."\x0b"."\xc8"."\xf9"."\x31"."\xe3"."\xdd"."\xf1"."\xf3"."\x5b"."\x7d"."\x44"."\x52"."\x00"."\xca"."\xc0"."\xaf"."\x6a"."\x9f"."\xc6"."\x5a"."\x46"."\xb1"."\x60"."\x84"."\xb2"."\x67"."\x3e"."\xd3"."\xb8"."\xff"."\x00"."\xba"."\x0e"."\x67"."\x88"."\xdd"."\xbd"."\xac"."\x72"."\x68"."\x21"."\x80"."\x18"."\x0c"."\x3b"."\xd4"."\x1e"."\x1f"."\x39"."\x6b"."\x89"."\x6e"."\x25"."\x1b"."\x9e"."\xd9"."\xe9"."\x4c"."\xdf"."\x5f"."\xe9"."\x2c"."\xac"."\xa0"."\xa3"."\x0c"."\x30"."\xa9"."\x6f"."\x22"."\x2a"."\x04"."\x8c"."\x1d"."\xc7"."\xf9"."\xa8"."\xad"."\x64"."\x99"."\x5d"."\xdc"."\xaf"."\x42"."\x69"."\xfb"."\x12"."\xcd"."\x6e"."\x71"."\xda"."\xa3"."\x96"."\x0b"."\x20"."\x41"."\xbe"."\x2a"."\xf7"."\x0a"."\x4f"."\xe5"."\x0b"."\x1e"."\xf4"."\x0b"."\xc8"."\x1f"."\x59"."\xa2"."\xb6"."\x94"."\x8e"."\x61"."\xa2"."\x83"."\x96"."\x59"."\x30"."\x29"."\xfb"."\x59"."\x77"."\xc8"."\x35"."\x23"."\x56"."\xd5"."\xb4"."\x37"."\x05"."\x0f"."\x5a"."\xa2"."\xdb"."\x48"."\x85"."\x7d"."\xdb"."\x83"."\xd6"."\x96"."\x12"."\x00"."\xcc"."\xb9"."\xf6"."\x9e"."\x84"."\xd2"."\xad"."\x70"."\x59"."\x76"."\xaf"."\x0b"."\x23"."\x38"."\x0a"."\x3a"."\x93"."\x81"."\x50"."\x75"."\xbe"."\x9b"."\x54"."\x99"."\x4b"."\x4a"."\xe5"."\x40"."\xdb"."\x6a"."\xa3"."\x79"."\xc6"."\xe2"."\x81"."\x7e"."\x9e"."\x47"."\xcb"."\xae"."\x46"."\x33"."\xfe"."\x0d"."\x73"."\xf6"."\x17"."\x1f"."\x4b"."\x69"."\x2a"."\x82"."\x0e"."\xb5"."\xd8"."\x8a"."\xce"."\xce"."\x7b"."\x68"."\x11"."\x64"."\x99"."\xc0"."\xd6"."\x72"."\xcc"."\xc7"."\x72"."\x68"."\x2b"."\xaf"."\xa9"."\x0a"."\x46"."\x21"."\xe6"."\xee"."\x1b"."\x23"."\x7d"."\xa9"."\xa5"."\xf5"."\x0a"."\xdc"."\x94"."\xb7"."\x56"."\x0a"."\xe4"."\x8e"."\x87"."\x1a"."\x87"."\x8a"."\x48"."\xde"."\xf0"."\xbd"."\x43"."\x54"."\x51"."\x95"."\xc7"."\xdd"."\x8d"."\xea"."\x2d"."\xf5"."\xe5"."\xad"."\xca"."\x39"."\x85"."\xbe"."\xd3"."\xed"."\x3d"."\xc1"."\xfc"."\xd1"."\x0e"."\x7a"."\x92"."\x18"."\xa2"."\xba"."\x59"."\x60"."\xfe"."\x94"."\x9e"."\xe0"."\x0f"."\x6f"."\x35"."\x10"."\xcf"."\xca"."\x5d"."\x64"."\x0c"."\xb1"."\x38"."\xf8"."\xa7"."\xf8"."\x84"."\xe6"."\xe6"."\x18"."\xc3"."\xb6"."\x0a"."\xa6"."\x90"."\x3e"."\x6a"."\x2c"."\x92"."\x6e"."\xb9"."\xc5"."\x14"."\xcc"."\x2f"."\xee"."\xd6"."\xd5"."\x6e"."\xde"."\xe8"."\xa5"."\xb0"."\x50"."\x76"."\xae"."\x65"."\xa6"."\x25"."\xb0"."\x3a"."\x53"."\xb1"."\x5e"."\xe5"."\x42"."\x0a"."\xa2"."\x94"."\x97"."\x24"."\xb9"."\xa2"."\xa6"."\xb4"."\xa4"."\xb7"."\x5a"."\x2a"."\x09"."\x79"."\xcd"."\x04"."\xed"."\x45"."\x15"."\x47"."\x9d"."\x6c"."\x0e"."\xc4"."\xd6"."\xf6"."\x8e"."\x59"."\xb7"."\x3d"."\x0d"."\x14"."\x51"."\x15"."\xe3"."\x76"."\x2b"."\x82"."\x7e"."\xe1"."\x93"."\xf9"."\xa2"."\x3b"."\x68"."\xae"."\x91"."\xd6"."\x55"."\x24"."\x0d"."\xf6"."\x26"."\x8a"."\x2a"."\x29"."\x19"."\x06"."\x89"."\xb9"."\x0a"."\x4e"."\x8c"."\x63"."\x14"."\xcc"."\xf0"."\xc3"."\x04"."\x76"."\xeb"."\x14"."\x4a"."\xba"."\xc1"."\x62"."\xdb"."\xe4"."\xd1"."\x45"."\x06"."\x72"."\xb1"."\x27"."\xf1"."\x81"."\x53"."\x64"."\x24"."\xc8"."\x49"."\xa2"."\x8a"."\xa3"."\xce"."\x4d"."\x6d"."\x6e"."\x7d"."\xd4"."\x51"."\x40"."\xd0"."\x19"."\xa2"."\x8a"."\x2a"."\x0f"."\xff"."\xd9";
echo $image;
This works too. I store images as a base64 string. It is fool proof. Very simple. Stored in an SQL TEXT column.
header('Content-Type:image/jpg');
$filename = "pitbull3p.jpg";
$handle = fopen($filename, "r");
$image = base64_encode(fread($handle, filesize($filename)));
$len = strlen($image);
echo base64_decode($image);
And gzcompress will get the base64 string down to about the size of the original binary image. In this case less:
$image = gzcompress(base64_encode(fread($handle, filesize($filename))));
1,864 bytes original jpg image
2,488 bytes base64 before compression.
1,833 bytes after compression.
Also I noticed I did not put the header() in my previous code. It's there now.
end of update 2
Update
I assumed the problem was in saving the image into the BLOB. I used PHPmyAdmin to save the image. When I wrote some code to save the image I had problems.
This is the code I finally got to work. Starting with an empty table.
I used fread because it is binary safe. converted the files data to HEX and appended the hex to'0x'.
It was important the $image did not have single quotes in the query string.
header('Content-Type:image/jpg');
$filename = "pitbull3p.jpg";
$handle = fopen($filename, "r");
$image = fread($handle, filesize($filename));
$image = '0x' . bin2hex($image);
$sql = "INSERT INTO `isl_contact`.`image` (`id`, `image`) VALUES (1,$image)";
$result = mysql_query($sql);
if(mysql_errno() > 0){echo mysql_error() . "\n$sql";}
$result = mysql_query("SELECT `image` FROM `image` WHERE `id` = 1");
$row = mysql_fetch_array($result, MYSQL_NUM);
echo $row[0];
end of update
I just did the same and it worked fine.
How did you get the image into the table?
When I viewed the script in the Browser the image came up as it should.
Saved a jpg image in a LONGBLOB
header('Content-Type:image/jpg');
$result = mysql_query("SELECT `image` FROM `image` WHERE `id` = 1");
$row = mysql_fetch_array($result, MYSQL_NUM);
echo $row[0];
This is my test database
CREATE TABLE IF NOT EXISTS `image` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` longblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
INSERT INTO `image` (`id`, `image`) VALUES
(1, 0xffd8ffe000104a46494600010101004800480000ffdb0043000a07070807060a0808080b0a0a0b0e18100e0d0d0e1d15161118231f2524221f2221262b372f26293429212230413134393b3e3e3e252e4449433c48373d3e3bffdb0043010a0b0b0e0d0e1c10101c3b2822283b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3bffc00011080064006403012200021101031101ffc4001b00000301010101010000000000000000000004050306020107ffc4003210000201030205030302050500000000000102030004111221051331415106226114327123a115243381914252b1d1f0ffc400160101010100000000000000000000000000000102ffc4001b11010002020300000000000000000000000001110221224161ffda000c03010002110311003f00fc9952b4588f8a6218734cadb6074a8d1258be2b4117c53f1da67b5331d8e7b5412794450233e2ac3d9103edaf22d36e9412b966811b5561699ed5aad8646c2822046535ba9cae08a7dacb0db8af46c869ce2825346b9e959ba003a5517b62b4acb19a09af082d4532537a2a87ad6318069f4881da96b44381b554b78e3660af284a83ec56e800da9b86241e2beac56ec4a2dc61946e06e6b716f1f2f09977ff007134184c21c805947c5056000018c9e9b5691d8a86018024f9a7e3e1eb22ea00647db412792a188c74af5855ee055db8b08b96b2e02b670452335968701d4053d0f634423c959065706b078186702ad2db7d3ae55415f077c52775711471e5e21953ee20e36a093243b1c8a9f3c3b9daad497166ce152e01c8c938c85f8a5ee614eaa43023a8a2b9d923c39a29d9621aced4503760a1906d5d67a6bd1bfc6219eeaf7990c4a084236c9f35c9d94863c153823bd7e9317aaedf837a4618a490cd7dc927428efdb3551c0d8591b4ba9a2662ecae4339ee01daaf421123dc81dcd44e17334e659642798ed96cf9ef55c0666d5a3f4f6076a81fb3b5fa89b57fa7ce29c9122b54d5b75db35870fe2b0340f01c2b28c0db6adee248e7b4075751d3c1aa3ec6cae403ee04e46d5acb6a92a952339efe2a7706983a3348db03a403e4538fc463b7d6cc33e003412a794da4dc890378d446c454cbe3804aefa7f714fbdc7f1666c60329dc9f1485edb4b1c47043e9c8241ea2a0ea7d01c238749c1260f6f1cbce73cc761923e0541f527028782f106b68a7d68dee4523751e2a5f00f51dff0004915ada5d51cce54c2dd323bd5ae2dc7e5e3122f36348f2bee5ee08aa393962fd43b514c4e0734ef45454b8ae742d32fc5bea30648cae186594f5a948722b603d80793415b875c2416f70e5c03cc1f920d58e0fc523bc525ed26fa70c63fa8322801863a29c1232ca36df71806a2da836c3989861b8607bd5ee1377f4f13884bc6256e60611a380d8d986a070df23c5672bad2c576fb6f6a2d78a4b13cc0203ac12739cf6ff009ae82384488e50e032741dab9cb6492194abbb3c6a362ea3277dc7ef5d370bb49152463236e085c0e9fdab4cc94e11672470ca5940fd463befb567c55211014c9047b8103f6abd690850c83a1cf515cd714b47499c8c9527193b11ff00ba550b70b51070a66b58d64ba99b62f13ca17270a30bb9271b741b1f15252eb88a7129c5dc11b72667899e26011f492095cf6da9ee1f6ef60669d03091c696632b0f6f61b76a97732c92dc88361abb28c055c56395f8d6a92e55b89f0b03041a8904f5c9aa3c3b87dc59a3bcf3191d86dbd2efa632420dc60e6a9c33732d94d690a480eb39a2897fa877a2820c7d0530aba860f6a56238029fb750d4142c31242ca7c56bc36fcdbcc2da41900e149da928a56b59b503ed6d8d691729ee23925931186d6c31bfe283a845596e8161fa4307f35d6db444287ce030ce3c57136d76b313c938046ff15d6dadd136e9cc3a174838fed550daa18a63a48d27cd25c461530bc8f931e3ddf1f35b7d445200cac0af6a9fc65a46b16084b2673ed3b8ff00ba0e6788ddbdac72682180180c3bd41e1f396b896e251b9ed9e94cdf5fe92caca0a30c30a96f222a048c1dc7f9a8ad64995ddcaf4269fb12cd6e71daa3960b2041be2af70a4fe50b1ef40bc81f59a2b6948e61a28396593029fb5977c8352356d5b437050f5aa2db48857ddb83d6961200ccb9f69e84d2ad705976af0b23380a3a93815075be9b54994b4ae540db6aa379c6e2817e9e47cbae4633fe0d73f6171f4b692a820eb5d88acece7b68116499c0d672ccc772682bafa90a4621e6ee1b237da9a5f50adc94b7560ae48e871a878a48def0bd43545195c7dd8dea2df5e5adca3985bed3ed3dc1fcd10e7a9218a2ba5960fe949ee00f6f3510cfca5d640cb138f8a7f884e6e618c3b60aa6903e6a2c926eb9c514cc2feed6d56edee8a5b05076ae65a625b03a53b15ee5420aa2949724b9a2a6b4a4b75a2a0979cd04ed4515479d6c0ec4d6f68e59b73d0d145115e3762b827ee193f9a23b68ae91d655240df6268a2a29190689b90a4e8c6314ccf0c30476eb144abac162dbe4d1450672b127f181536424c849a28aa3ce4d6d6e7dd45140d019a28a2a0fffd9);
// Notice there are no quote marks around the Hex
maybe upload the image to server folder and save only the path to the image. It's more efficient.

Insert images (NSData) into BLOB fields

From iOS app to php script, I want to store a bench of images as BLOB types:
$profile_images = $_REQUEST['profile_images_array'];//get the array of images
//loop the images and store them one by one
foreach($profile_images as $image){
$sqlinsertimages = "insert into profile_images (id_client,image) Values ('".$id."',mysql_real_escape_string('".$image."'))";
$result = mysql_query($sqlinsertimages);
}
The insert would work fine if I eliminate the image field (which is BLOB type), so the issue is obviously with the saving BLOB types in the table.
How to store properly the images in PHP as BLOB?
PS: not willing to adopt the file system storage.
Ok, I guess that work finally, didn't knew that I had to save the image to the server before saving it to MySQL:
foreach($profile_images as $image){
//Get the file as a string before inserting it to table
$content = file_get_contents($image);
$content = mysql_real_escape_string($content);
$sqlinsertimages = "insert into profile_images (id_client,image) Values ('".$id."','".$content."')";
$result = mysql_query($sqlinsertimages);
}

Insert image binary from xml data to mysql in PHP

I have some photos (not big, only 8kb) in mysql database (in my desktop). the field type is blob. i want to export the table to xml file then upload it to my database website. but it not success. Here is what i have done :
Exporting the data to xml (in my computer desktop):
FileStream fs = new FileStream(filename,FileMode.Create,FileAccess.Write,FileShare.None);
StreamWriter sw = new StreamWriter(fs,Encoding.ASCII);
ds.WriteXml(sw); //write the xml from the dataset ds
Then upload the xml from my joomla website. i load the xml before insert it to the database
...
$obj = simplexml_load($filename);
$cnt = count($obj->mydata); //mydata is the table name in the xml tag
for($i=0;$i<cnt;$i++)
{
...
$myphoto = 'NULL';
if(!empty($obj->mydata[$i]->myphoto))
{
$myphoto = base64_code($obj->mydata[$i]->myphoto);
}
//insert to the database
$sqlinsert = "insert into jos_myphoto (id,myphoto) values(".$i.",".$myphoto.")";
...
}
...
it keep telling me 'DB function failed'. when value of $myphoto is null, the query work well but if $myphoto is not null, the error appears. i think there is something wrong with the code
$myphoto = base64_code($obj->mydata[$i]->myphoto).
i try to remove base64_code function but it dont work. How to solve this problem? Sorry for my bad english
Your data may contain which needs escaping put mysql_real_escape_string() function and try
It is always a good habit to store data using this function which save you from sql injection also.
And put quotes around the column data.
$sqlinsert = "insert into jos_myphoto (id,myphoto)
values(".$i.",'".mysql_real_eascape_string($myphoto)."')";

Categories