كيف أستطيع حفظ صورة في قاعدة البيانات عن طريق php وكيف استخرجها
صورة في قاعدة البيانات
معرفش انت عايز الفكرة و لا عايز الكود بس
قرائة صورة و حفظها فى قاعدة البيانات مش مختلف عن اى عملية حفظ بيانات تانية عموما
1- اقراء ملف الصورة
2- احفظها داخل قاعدة البيانات
طب لمة حفظتها عايز تخرجها تانى
1- استدعى مكونات الصورة من قاعدة البيانات
2- اطبعها على الشاشة عادى
بس قبل متطبع اى حاجة او تخرج اى شئ للمتصفح استخدم الـ headers عشان تعرف المتصفح ان دى صورة
حاجة زى كدة مثلا
<?php
//image data
header("content-type: image/gif");
print $imageData;
?>اوكى انا عملت Class حينفذ الى انت قلته
1- بيحفظ الصور فى قاعدة بيانات
2- بيسترجع الصور من قاعدة البيانات و يعرضها على المتصفح
و فى المرفقات الـ Class مع مثالين على استخدامه
وصورة للينكس تعمل test بيها عشان نرشى شباب اللينكس :D
<?php
class Shref_ImgInDb
{
##############################
# Start of the Calss #
# Shref_ImgInDb #
##############################
/*
this simple class can store an image in a data base (MySQL) and can get
it again and show it on the screen in a simple way.
programmer : Ahmed Shref
e-mail : a_shref@hotmail.com
Country : Egypt
City : Alexandria
Date : 30 jun 2005
*/
/* void conDb(str dbuser, str dbpass, str dbname,str [dbhost])
***## connect's to the data base ##***
- parameters :
--> the first : the data base user name
--> the second : the password for that user
--> the third : the data base name
--> the fourth : [optional] the host
by default set to "localhost"
*/
function conDb($dbUser, $dbPass, $dbName, $dbHost = "localhost")
{
//checking parameters
if($dbName && $dbUser)
{
$this->dbName = $dbName;
$this->dbUser = $dbUser;
$this->dbPass = $dbPass;
$this->dbHost = $dbHost;
}
else
{
return $this->error(1);
}
//connecting to the data base
$this->conHolder = @mysql_pconnect($this->dbHost, $this->dbUser, $this->dbPass);
mysql_select_db($this->dbName, $this->conHolder)
or die( $this->error(2) );
}//Function conDb()
##############################
/* void getImage(str tableName, str imageField, str idfield, int idnum)
***## gets the iamge from data base ##***
-parameters :
--> the first : the name of the table that hold the image data
--> the second : the name of the colmun that contain the image
--> the third : the name of the colmun that contain the image id in the table
--> the fourth : the id of the image that you want to get it from the data base
*/
function getImage($tableName, $imageField, $idField, $idNum)
{
//checking the parameters
if ($tableName && $imageField && $idField && $idNum)
{
$this->tableName = $tableName ;
$this->imageField = $imageField ;
$this->idField = $idField ;
$this->idNum = $idNum ;
}
else
{
return $this->error(3);
}
//quering the data base to get the image
$dbQuery = "SELECT ".$this->imageField.
" FROM ".$this->tableName.
" WHERE ".$this->idField." = ".$this->idNum.
" LIMIT 1";
$this->result =@ mysql_query($dbQuery, $this->conHolder);
if (!$this->result)
{
return $this->error(4);
}
//fetching the results
$this->imageData = mysql_fetch_assoc($this->result);
//freeing the results and closing the connection with the db
mysql_free_result($this->result);
mysql_close($this->conHolder) ;
}//Function getImage()
##############################
/* void showImage()
***## print the image to the screen ##***
*/
function showImage()
{
if(!isset($this->imageData))
return $this->error(5);
//reseting the image to it's normal encoding
$this->char = explode(" ",$this->imageData[$this->imageField]);
$this->image = "";
foreach($this->char as $key=>$value)
{
$this->image .=chr($value);
} //foreach
print $this->image;
}//Function showImage()
##############################
/* void addImage(str imagePath, str tableName, str imagefield, str typefield)
****## add's an image to the data base ##***
-parameters :
--> the first : the image path
--> the second : the name of the table that hold the image data in
the data base
--> the third : the column where you will save the image
*/
function addImage($imagePath ,$tableName, $imageField)
{
//checking the parameters
if ($tableName && $imageField && $imagePath)
{
$this->tableName = $tableName ;
$this->imageField = $imageField ;
$this->imagePath = $imagePath ;
}
else
{
return $this->error(6);
}
//checking if the file exists or not
if ( !file_exists($this->imagePath) )
{
return $this->error(7);
}
//opening the image file for reading
$fileHandle = @fopen($this->imagePath,"r");
if (!$fileHandle)
return $this->error(8);
$this->dataHolder ="";
//reading the file and converting data type
do
{
$this->dataHolder = fread($fileHandle,1);
$this->ASCIIimage .= ord($this->dataHolder)." ";
}
while(!feof($fileHandle));
//querying data base to add the image
$dbQuery = "INSERT INTO ".$this->tableName." (".$this->imageField.")".
" VALUES ('".$this->ASCIIimage."')";
$this->result =@ mysql_query($dbQuery, $this->conHolder);
if (!$this->result)
{
return $this->error(4);
}
mysql_close($this->conHolder);
// printing succeed message
print "the image have been added to the data base ...";
}//Function addImage()
/* void error(int id)
***## print the error massages to the screen ##***
*/
function error($id)
{
//declaring the errors in array
$errors[1] = "<br>error 1 : Function conDb -> there is missed parameters
<br> » the first : the data base user name
<br> » the second : the data base password for that user
<br> » the third : the data base name";
$errors[2] = "<br>error 2 : faild to select the data base";
$errors[3] = "<br>error 3 : Function getImage -> there is missed parameters
<br> » the first : the table name
<br> » the second : the image field in the data base
<br> » the third : the image id field in the data base
<br> » the fourth : the image id";
$errors[4] = "<br>error 4 : faild in querying the data base";
$errors[5] = "<br>error 5 : Function showImage -> you should use the function getImage first ";
$errors[6] = "<br>error 6 : Function addImage -> there is missed parameters
<br> » the first : the image path
<br> » the second : the table name in the data base
<br> » the third : the image field in the data base";
$errors[7] = "<br>error 7 : can\'t fined the image in that place , check the image path";
$errors[8] = "<br>error 8 : can\'t open the image for reading";
//the errors holder
$this->error = "<font color=#FF0000>".$errors[$id]."</font>";
//printing the error message
print $this->error;
}//Function error()
##############################
# End of the Class #
# Shref_ImgInDb #
##############################
}//Class Shref_ImgInDbو بلغنى إذا كان فى اى bug و العياذ بالله
ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
اه نسيت اقولك لمة تحفظ الصورة فى قاعدة البيانات بتخلى سعة الحقل كبيرة
يعنى مثلا خلى نوع الحقل blob او اى حاجة اكبر من كدة لو عايز يعنى
تم تعديل هذه المشاركة بواسطة a_shref في 30 يونيو 2005 في 08:17
مشكور جدا
بس انا كنت عايز اعمل browse واختار صورة من على الهارد ويتم حفظها في قاعدة البيانات
اه انت مقلتش كدة فى الأول
بس كدة انت محتاج تعمل كود uploading بحيث ان انت ترفع الملف للسيرفر
و فى نفس العملية دى حتعرف المسار الى انت حفظت فيه الصورة او الملف هو مثلا $path
و حتدخله فى الكلاس زى مموجود عندك فى المثال الى فى الملف على انه the file path
هذا الموضوع مغلق.
