将图像保存在mysql数据库的文件夹和路径中 [英] save image in folder and path in mysql database
问题描述
< form method =postenctype =multipart / form-data >
< input type =textid =titleplaceholder =Project Title/>< br />
< input type =textid =vurlplaceholder =如果您有任何有关项目的视频,请在此处输入您的视频网址路径style =width:435px;/>< br /> ;
< label for =file>档案名称:< / label>
< input type =filename =fileid =file/>< br>
< input type =buttonname =submitvalue =Submitid =update/>
< / form>点击提交数据存储在数据库中,并显示使用Ajax调用
这是我的js代码:
$ $ $ $ $ $ $ $ $ $ $$ update $ click $ function $ $ $ $ $ $ $ );
e.preventDefault();
var ttle = $(#title)。val();
alert(ttle);
var text = $( val();
var(); $ $ b alert(vurl);
var dataString ='param ='+ text +'& param1 ='+ vurl +'& param2 ='+ ttle +'& param3 ='+ img;
$ .ajax({
type:'POST',
data:dataString,
url:'insert.php',
成功:函数(id){
alert (id);
window.location =another.php?id =+ id ;;
}
});
});
这里我使用insert.php存储数据并使用another.php
显示,来图像部分我不明白如何将图像存储在文件夹和路径中的数据库,我的意思是我有点困惑,以整合在insert.php中的代码
insert.php
$ host =localhost;
$ username =root;
$ password =;
$ db_name =geny;
$ tbl_name =project_details;
$ b mysql_connect($ host,$ username,$ password)或死(无法连接);
mysql_select_db($ db_name)或死(无法选择DB);
$ name = $ _POST ['param'];
$ video = $ _POST ['param1'];
$ title = $ _POST ['param2'];
$ sql =INSERT INTO $ tbl_name(title,content,video_url)VALUES('$ title','$ name','$ video');
if(mysql_query($ sql)){
echo mysql_insert_id();
} else {
echoCan not Insert;
$ b $ p
$ b 如果我分开,图像存储在文件夹中。
如果我分开,那么表单代码是:
< form action =upload_file.phpmethod =post
enctype =multipart / form-data>
< label for =file>文件名:< / label>
< input type =filename =fileid =file>< br>
< input type =submitname =submitvalue =Submit>
< / form>
upload_file.php:
<?php
$ allowedExts = array(gif,jpeg,jpg,png);
$ extension = end(explode(。,$ _FILES [file] [name]));
if((($ _FILES [file] [type] ==image / gif)
||($ _FILES [file] [type] ==图片/ jpeg)
||($ _FILES [file] [type] ==image / jpg)
||($ _FILES [file] [type ] ==image / pjpeg)
||($ _FILES [file] [type] ==image / x-png)
||($ _FILES [file ] [type] ==image / png))
&&&($ _FILES [file] [size]< 50000)
&& in_array ($ extension,$ allowedExts))
{
if($ _FILES [file] [error]> 0)
{
echoReturn Code: 。 $ _FILES [file] [error]。 <峰; br> 中;
}
else
{
echoUpload:。 $ _FILES [file] [name]。 <峰; br> 中;
回显类型:。 $ _FILES [file] [type]。 <峰; br> 中;
回声大小:。 ($ _FILES [file] [size] / 1024)。 kB< br>;
回显临时文件:。 $ _FILES [file] [tmp_name]。 <峰; br> 中;
$ b $ if(file_exists(C:/ wamp / www / WebsiteTemplate4 / upload /。$ _FILES [file] [name]))
{
echo $ _FILES [file] [name]。 已经存在。 ;
$ $ b $ move_uploaded_file($ _ FILES [file] [tmp_name],
C:/ wamp / www / WebsiteTemplate4 / upload / 。$ _FILES [file] [name]);
// echoStored in:。 上传/。 $ _FILES [ 文件] [ 名称];
$ tmp =C:/ wamp / www / WebsiteTemplate4 / upload /。 $ _FILES [ 文件] [ 名称];
echo $ tmp;
echoInvalid file;
}
?>
这个工作完美...
我的问题是如何将这段代码整合到insert.php中...
请帮助我...
解决方案从这个链接参考如何使用Ajax上传图像。它会帮助你。
http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html
I have a form like this:
<form method="post" enctype="multipart/form-data">
<input type="text" id="title" placeholder="Project Title"/><br />
<input type="text" id="vurl" placeholder="If You have any video about project write your video url path here" style="width:435px;"/><br />
<textarea id="prjdesc" name="prjdesc" rows="20" cols="80" style="border-style:groove;box-shadow: 10px 10px 10px 10px #888888;"placeholder="Please describe Your Project"></textarea>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /><br>
<input type="button" name="submit" value="Submit" id="update"/>
</form>
On click submit the data is storing in database and displaying using Ajax call
this is my js code:
$("#update").click(function(e) {
alert("update");
e.preventDefault();
var ttle = $("#title").val();
alert(ttle);
var text = $("#prjdesc").val();
var vurl = $("#vurl").val();
var img = $("#file").val();
alert(vurl);
var dataString = 'param='+text+'¶m1='+vurl+'¶m2='+ttle+'¶m3='+img;
$.ajax({
type:'POST',
data:dataString,
url:'insert.php',
success:function(id) {
alert(id);
window.location ="another.php?id="+id;;
}
});
});
here i am storing data using insert.php and displaying using another.php
but when coming to the image part i dont understand how to store image in folder and path in db, i mean i am bit confused to integrate code in insert.php
insert.php
$host="localhost";
$username="root";
$password="";
$db_name="geny";
$tbl_name="project_details";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$name = $_POST['param'];
$video = $_POST['param1'];
$title = $_POST['param2'];
$sql="INSERT INTO $tbl_name (title, content, video_url) VALUES ('$title','$name','$video')";
if(mysql_query($sql)) {
echo mysql_insert_id();
} else {
echo "Cannot Insert";
}
if i do separate then the image is storing in folder..
if i do separate then the form code is:
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
upload_file.php:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("C:/wamp/www/WebsiteTemplate4/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"C:/wamp/www/WebsiteTemplate4/upload/" . $_FILES["file"]["name"]);
// echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
$tmp = "C:/wamp/www/WebsiteTemplate4/upload/" . $_FILES["file"]["name"];
echo $tmp;
}
}
}
else
{
echo "Invalid file";
}
?>
this is working perfectly...
my question is how to integrate this code in insert.php...
please help me...
解决方案 Take reference from this link how to upload image using ajax. It will help you.
http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html
这篇关于将图像保存在mysql数据库的文件夹和路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!