将图片网址保存到Mysql数据库 [英] Save image url to Mysql Database
问题描述
我有一个名为"mysqlproject"的数据库,其中包含两个表.我感兴趣的表之一就是标记,并在数据库中这样声明:
I have a database named 'mysqlproject' that contains two tables. One of the tables that i am interesting in is named markers and declared in database like this :
CREATE TABLE IF NOT EXISTS `markers` (
`id` int(11) NOT NULL,
`TitleEvent` varchar(60) NOT NULL,
`Description` varchar(80) NOT NULL,
`lat` float(10,6) NOT NULL,
`lng` float(10,6) NOT NULL,
`type` varchar(255) NOT NULL,
`status` varchar(30) NOT NULL,
`EventUserName` varchar(60) NOT NULL,
`PhotosEvent` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=73 ;
其中一列是PhotosEvent
,我要在其中保存照片的网址.因此,我通过javascript文件捕获了照片,并将其通过ajax发送到php文件"camera.php"
One of the columns is PhotosEvent
that in there i want to save photo's url. So i capture a photo through a javascript file and i am sending it through ajax to a php file "camera.php"
ajax发送过程为:
The ajax sending procedure is:
$.ajax({
type: "POST",
url: "camera.php",
data: {
imgBase64: dataURL
}
}).done(function( respond ) {
// you will get back the temp file name
// or "Unable to save this image."
console.log(respond);
});
因此,我正在使用POST方法将图像发送到文件"camera.php".在php文件中,我正在获取该图像并将其首先保存在我的本地服务器"中的"EventImages"文件夹中.好吧.. 但是我只想将图像的URL保存在"PhotosEvent"字段中的数据库中,而仅保存在活动ID所在的原始位置.
So i am sending with method POST the image to the file "camera.php". In php file i am taking that image and saving it first in my "local server" in folder "EventImages". Ok that works .. But i want to save the url of the image in database in the field "PhotosEvent" only in raw where the active id is located..
连接数据库:
$connection = mysql_connect('localhost', 'root', '');
if (!$connection) //Success $Connection with server returns 1
{
die("Database Connection Failed" . mysql_error());
}
else
{
//Connection with server established
}
// Try Connection with mysql Database
$select_db = mysql_select_db('mysqlproject');
if (!$select_db) //Success $Connection with Database returns 1
{
die("Database Selection Failed" . mysql_error());
}
else
{
//Connection with Database established
}
将图像以.png格式保存到文件夹(有效),并尝试将url保存到数据库中(无效):
Save image to folder as .png (works) and attemp saving url to database (don't work):
if ( isset($_POST["imgBase64"]) && !empty($_POST["imgBase64"]) ) {
define('UPLOAD_DIR', 'EventImages/');
// get the dataURL
$dataURL = $_POST["imgBase64"];
// the dataURL has a prefix (mimetype+datatype)
// that we don't want, so strip that prefix off
$parts = explode(',', $dataURL);
$data = $parts[1];
// Decode base64 data, resulting in an image
$data = base64_decode($data);
// create a temporary unique file name
$file = UPLOAD_DIR . uniqid() . '.png';
// write the file to the upload directory
$success = file_put_contents($file, $data);
// return the temp file name (success)
// or return an error message just to frustrate the user (kidding!)
print $success ? $file : 'Unable to save this image.';
$results = mysql_query("INSERT INTO markers (PhotosEvent) WHERE id ='".$_SESSION['id']."'VALUES('$dataURL')");
}
我的问题是保存图像URL-数据库路径.我怎样才能做到这一点?预先感谢..
My problem is saving the image url - path to database. How can i do this? Thanks in advance..
推荐答案
找到了!
define('UPLOAD_DIR', 'EventImages/');
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
$result=mysql_query("SELECT id FROM markers ORDER BY id DESC LIMIT 1");
while ($row = mysql_fetch_array($result)){
$LastInsertID=$row['id'];
}
$results = mysql_query("UPDATE markers SET PhotosEvent='".$file."' WHERE id = '".$LastInsertID."'");
现在它可以正常工作了..我对表Markers中的最后一个已知ID进行伪造,然后将存储在变量$ file中的图像的url保存在数据库中!!我也像以前一样将图像保存到文件夹EventImages中!
now it works ok.. I am fetcing the last known id that is in table Markers and i save the image's url that is stored in variable $file in the database!! I also save the image to folder EventImages as i did before!!
这篇关于将图片网址保存到Mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!