如何使用php脚本将图像上载到数据库?
这是我上传的代码。。但是它不起作用。。我使用了一个文件获取内容函数。。 上传图像如何使用php脚本将图像上载到数据库?,php,mysql,database,image,Php,Mysql,Database,Image,这是我上传的代码。。但是它不起作用。。我使用了一个文件获取内容函数。。 上传图像 </head> <body> <form action="upload1.php" method="POST" enctype="multipart/form-data"> File: <input type="file" name="image"/> <input type="submit" value="Upload im
</head>
<body>
<form action="upload1.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"/>
<input type="submit" value="Upload image" />
</form>
<?php
//connect to the database
$con = mysql_connect("localhost","root", "");
if(!$con)
{
die('Could not connect to the database:' . mysql_error());
echo "ERROR IN CONNECTION";
}
mysql_select_db("imagedatabase", $con);
//file properties
echo $file = $_FILES['image']['tmp_name'];
echo '<br />';
if(!isset($file))
echo "Please select an image";
else
{
$image = file_get_contents($_FILES['image']['tmp_name']);
echo $image_name = addslashes($_FILES['image']['name']); echo '<br \>';
echo $image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE)
echo "That's not an image";
else
{
$insert = mysql_query("INSERT INTO images (image) VALUES ($image)",$con);
if(!$insert)
echo "Problem uploding the image. Please check your database";
else
{
$last_id = mysql_insert_id();
echo "Image Uploaded. <p /> Your image: <p /><img src=display.php? id=$last_id>";
}
}
}
mysql_close($con);
?>
</body>
</html>
文件:
在数据库中存储图像是个坏主意。在数据库中存储指向它的路径,通过.htaccess关闭带有图像的目录,并在硬盘上使用它
为什么不应在数据库中存储文件?
如果使用DB存储图像,您将拥有:
慢查询
灾难性索引的大小
桥梁高荷载phpmysql
编辑照片时出现问题(您需要获取图像、修改某些内容
然后再次插入所有数据
将文件从一个地方传输到另一个地方的问题
关于StackOverflow的新问题«如果没有,如何处理文件
文件,但是字符串»
在数据库中存储图像是个坏主意。在数据库中存储指向它的路径,通过.htaccess关闭带有图像的目录,并在硬盘上使用它
为什么不应在数据库中存储文件?
如果使用DB存储图像,您将拥有:
慢查询
灾难性索引的大小
桥梁高荷载phpmysql
编辑照片时出现问题(您需要获取图像、修改某些内容
然后再次插入所有数据
将文件从一个地方传输到另一个地方的问题
关于StackOverflow的新问题«如果没有,如何处理文件
文件,但是字符串»
将图像直接上传到数据库不是一个好主意。而是将照片上传到文件夹中,然后将照片名称插入数据库,然后在需要时随时调用它。如果需要,可以尝试以下代码
要使代码为您工作,您必须遵循以下步骤:
在代码内部,将“your_photo”替换为输入的名称(在这种情况下,我想应该是“image”)
创建一个将上载图像的文件夹,然后在->$newname=“support/images/profile/”中进行更改,并在此处写入图像文件夹名称
编写正确的数据库查询。请记住,图像名称将自动创建,并且名称将保留在此变量->$image\u name中。将名称插入数据库时,只需使用$image\u name作为值
上载脚本:
<?
// If Everything is good- process the form - write the data into the database
$photo=$this->input->post('your_photo');
if($photo==NULL){$image_name='0';}// if no photo is selected the default value of the photo would be 0
//photo upload starts
$errors=0;
if($_FILES['your_photo']){
$image=$_FILES['your_photo']['name'];
if($image) {
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext; }
//reads the name of the file the user submitted for uploading
$image=$_FILES['your_photo']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['your_photo']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
$msg="Sorry! Unknown extension. Please JPG,JPEG,PNG and GIF only ";
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['your_photo']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size < MAX_SIZE*1024)
{
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="support/images/profile/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['your_photo']['tmp_name'], $newname);
if (!$copied)
{
$msg="Sorry, The Photo Upload was unsuccessfull!";
$errors=1;
}
}
else
{
$msg="You Have Exceeded The Photo Size Limit";
$errors=1;
}
}}}
/*Image upload process ends here- If any problem occurs it will display error message via the $msg,
otherwise it will upload the image to the image folder. To insert the photo into database $image_name has been used*/
}
if(($_FILES['your_photo'])&& ($errors))/* If any photo is selected and any problem occurs while uploading it will
display an error message, otherwise transfer the data to Mod_addstudent model */
{
echo $msg;
}
else {
//Insert into database.Just use this particular variable "$image_name" when you are inserting into database
$sql="INSERT INTO your_table (field1, your_image_field) VALUES ('','$image_name')";
}
?>
直接将图像上传到数据库不是一个好主意。而是将照片上传到文件夹中,然后将照片名称插入数据库,然后在需要时随时调用它。如果需要,可以尝试以下代码
要使代码为您工作,您必须遵循以下步骤:
在代码内部,将“your_photo”替换为输入的名称(在这种情况下,我想应该是“image”)
创建一个将上载图像的文件夹,然后在->$newname=“support/images/profile/”中进行更改,并在此处写入图像文件夹名称
编写正确的数据库查询。请记住,图像名称将自动创建,并且名称将保留在此变量->$image\u name中。将名称插入数据库时,只需使用$image\u name作为值
上载脚本:
<?
// If Everything is good- process the form - write the data into the database
$photo=$this->input->post('your_photo');
if($photo==NULL){$image_name='0';}// if no photo is selected the default value of the photo would be 0
//photo upload starts
$errors=0;
if($_FILES['your_photo']){
$image=$_FILES['your_photo']['name'];
if($image) {
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext; }
//reads the name of the file the user submitted for uploading
$image=$_FILES['your_photo']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['your_photo']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
$msg="Sorry! Unknown extension. Please JPG,JPEG,PNG and GIF only ";
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['your_photo']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size < MAX_SIZE*1024)
{
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="support/images/profile/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['your_photo']['tmp_name'], $newname);
if (!$copied)
{
$msg="Sorry, The Photo Upload was unsuccessfull!";
$errors=1;
}
}
else
{
$msg="You Have Exceeded The Photo Size Limit";
$errors=1;
}
}}}
/*Image upload process ends here- If any problem occurs it will display error message via the $msg,
otherwise it will upload the image to the image folder. To insert the photo into database $image_name has been used*/
}
if(($_FILES['your_photo'])&& ($errors))/* If any photo is selected and any problem occurs while uploading it will
display an error message, otherwise transfer the data to Mod_addstudent model */
{
echo $msg;
}
else {
//Insert into database.Just use this particular variable "$image_name" when you are inserting into database
$sql="INSERT INTO your_table (field1, your_image_field) VALUES ('','$image_name')";
}
?>
在上载过程中,您应该将文件保存在某个文件夹中,并将文件名保存在数据库中,以便以后您可以从数据库中调用文件名并将其链接为要下载的超链接。我使用以下代码将图像上载到名为files的文件夹中,并将文件名保存在数据库中。最后,文件名在变量$newname中
if ($_FILES['file']['name']) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
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"] < 500000)
&& in_array($extension, $allowedExts)
) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$ext = end(explode(".", $_FILES["file"]["name"]));
$filename = current(explode(".", $_FILES["file"]["name"]));
$newname = $filename . '_' . time() . '.' . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],
"files/" . $newname);
}
} else {
echo "<div class='alert alert-success'>Image type or size is not valid.</div>";
}
}
if($\u文件['file']['name'])){
$allowedExts=数组(“gif”、“jpeg”、“jpg”、“png”);
$temp=explode(“.”,$_文件[“文件”][“名称”]);
$extension=end($temp);
如果(($_文件[“文件”][“类型”]=“图像/gif”)
||($_文件[“文件”][“类型”]=“图像/jpeg”)
||($_文件[“文件”][“类型”]=“图像/jpg”)
||($_文件[“文件”][“类型”]=“图像/pjpeg”)
||($_文件[“文件”][“类型”]=“图像/x-png”)
||($_文件[“文件”][“类型”]=“图像/png”))
&&($_文件[“文件”][“大小”]<500000)
&&in_数组($extension,$allowedExts)
) {
如果($\u文件[“文件”][“错误”]>0){
回显“返回代码:”.$\u文件[“文件”][“错误”]。
”;
}否则{
$ext=end(分解(“.”,$_文件[“文件”][“名称]);
$filename=current(分解(“.”,$_文件[“文件”][“名称]);
$newname=$filename.'.''.time().$ext;
移动上传的文件($文件[“文件”][“tmp文件名”],
“文件/”$newname);
}
}否则{
echo“图像类型或大小无效。”;
}
}
在上载过程中,您应该将文件保存在某个文件夹中,并将文件名保存在数据库中,以便以后您可以从数据库中调用文件名并将其链接为要下载的超链接。我使用以下代码将图像上载到名为“文件”的文件夹中,并将文件名保存在数据库中。最后,文件名在变量$newname中
if ($_FILES['file']['name']) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
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"] < 500000)
&& in_array($extension, $allowedExts)
) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$ext = end(explode(".", $_FILES["file"]["name"]));
$filename = current(explode(".", $_FILES["file"]["name"]));
$newname = $filename . '_' . time() . '.' . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],
"files/" . $newname);
}
} else {
echo "<div class='alert alert-success'>Image type or size is not valid.</div>";
}
}
if($\u文件['file']['name'])){
$allowedExts=数组(“gif”、“jpeg”、“jpg”、“png”);
$temp=explode(“.”,$_文件[“文件”][“名称”]);
$extension=end($temp);
如果(($_文件[“文件”][“类型”]=“图像/gif”)
||($_文件[“文件”][“类型”]=“图像/jpeg”)
||($_文件[“文件”][“类型”]=“图像/jpg”)
||($_文件[“文件”][“类型”]=“图像/pjpeg”)
||($_文件[“文件”][“类型”]=“图像/x-png”)
||($_文件[“文件”][“类型”]=“图像/png”))
&&($_文件[“文件”][“大小”]<500000)
&&in_数组($extension,$allowedExts)
) {
如果($\u文件[“文件”][“错误”]>0){
回显“返回代码:”.$\u文件[“文件”][“错误”]。
”;
}否则{
$ext=end(分解(“.”,$_文件[“文件”][“名称]);
$filename=current(分解(“.”,$_文件[“文件”][“名称]);