使用php将照片上载到Mysql数据库
我目前正在为我大学最后一年的项目制作一个网站,它需要一个照片上传功能。当前,当用户上载照片时,照片存储在远程服务器的文件夹中。我需要图像进入数据库,所以我想知道是否有人有任何建议,如何做到这一点,以及在下面的代码中放置代码以将上传的内容发送到数据库,我还需要它工作,当每个用户上传图像时,它们都会显示出来供所有人查看,而不是像现在这样,一次只显示一个图像,当页面刷新时,图像就会消失。希望一切都有意义,任何帮助都将不胜感激。多谢各位使用php将照片上载到Mysql数据库,php,html,mysql,uploading,Php,Html,Mysql,Uploading,我目前正在为我大学最后一年的项目制作一个网站,它需要一个照片上传功能。当前,当用户上载照片时,照片存储在远程服务器的文件夹中。我需要图像进入数据库,所以我想知道是否有人有任何建议,如何做到这一点,以及在下面的代码中放置代码以将上传的内容发送到数据库,我还需要它工作,当每个用户上传图像时,它们都会显示出来供所有人查看,而不是像现在这样,一次只显示一个图像,当页面刷新时,图像就会消失。希望一切都有意义,任何帮助都将不胜感激。多谢各位 <?php include_once("home_start
<?php include_once("home_start.php"); ?>
<h1>Upload your images here:</h1>
<div id="fileselect" style="border-bottom:thin #000000 solid; border- collapse:collapse">
<form id="frmSimple" action="home.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" id="filename" name="filename" size="10" /><br />
<input type="submit" id="submit" name="submit" value=" Upload " />
</form>
</div>
<div id="feedback">
<?php
// Determine whether a file was uploaded
if ($_FILES) {
// Put file properties into variables
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$tmp_name = $_FILES['filename']['tmp_name'];
// Determine whether file is png, jpg or other
switch($_FILES['filename']['type']) {
case 'image/jpeg': $ext = "jpg"; break;
case 'image/png': $ext = "png"; break;
//default: ext = ''; break;
}
//validate against file type
// if $ext is empty string (therefore null or false) image is not a jpg or png
if($ext){
// validate against file size
if($size < 1000000){
// Create a safe name for the file and store in a safe location
$n = "$name"; // Could add .$ext to enforce file type
$n = ereg_replace("[^A-Za-z0-9.]","",$n); // Remove all except alphanumeric characters and
$n = strtolower($n); // Convert to lower case (platform independence)
$n = "uploaded_images/$n"; // Add folder to force safe location
move_uploaded_file($tmp_name, $n); // Move to the safe location and give it the safe
echo "<p>Uploaded image '$name' as '$n': </p>";
echo "<img src='$n' />";
}
else echo "<p>'$name' is too big - 50KB max (50000 bytes).</p>";
}
else echo "<p>'$name' is an invalid file - only jpg and png accepted.</p>";
}
else echo "<p>No image has been uploaded.</p>";
?>
</div>
<?php include_once("home_end.php"); ?>
在此处上载您的图像:
选择要上载的文件:
我强烈建议不要这样做。相反,将照片存储在文件夹中,并从数据库中引用它们的位置(即指向它们在文件系统上的位置的字符串)
但是,如果您非常想将其存储在数据库中,则需要:
- 上传后获取文件内容
- 确保内容中没有任何与SQL冲突的字符(简单的解决方案是以某种方式对其进行编码;可能是base64?)
- 执行SQL插入
再一次-这是一个坏主意。不要这样做-将其保存到文件系统
此外,以下行:
move_uploaded_file($tmp_name, $n);
如果不检查文件类型或文件完整性,那么将shell上载到您的文件盒就变得很简单。一旦成功上载文件,就可以获得上载的图像路径
$file_type='jpg'; //if you are using more than one type write a switch case
$file_size = filesize($file);
$fp = fopen($file,'r');
$content = fread($fp,$file_size);
$content = addslashes($content); //content is a binary data of the image
fclose($fp);
将图像保存到数据库中。使用您想要的任何字段$file\u name、$file\u type、$file\u size和content编写插入查询。我假设您能够成功连接到数据库
mysql_query("INSERT INTO Images (id,file_name,file_type,file_size,content)
VALUES ('bird', 'jpg',340kb,'binarydata')");
该网站上还有太多其他问题:在几乎所有的使用情况下,将文件存储在数据库中都是一个坏主意。不要这样做。你可能还想更新你的代码,以填补你在网站上打开的各种安全漏洞,以及你正在使用的各种过时/不推荐的功能(ereg已经正式停止使用很长一段时间了)。谢谢你的回复,我一定会采纳这个建议,并尝试重新编写这段代码。