我正在尝试将.jpg
图像插入到mysql表中,但它无效。
它显示以下错误消息:
警告:file_get_contents():文件名不能为空
如何解决?
这是我的代码:
<form action="image.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
<?php
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `images` ( image,image_name) VALUES ( '$image', '$image_name')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
?>
答案 0 :(得分:1)
首先,您应该检查您的图像列是否为BLOB类型!
我对你的SQL表一无所知,但如果我试着以自己的方式为例。
我们得到了字段id(int),image(blob)和image_name(varchar(64))。
所以代码看起来应该是这样的(假设ID始终是&#39; 1&#39;并且让我们使用这个mysql_query):
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
你在很多方面做错了。不要使用mysql函数 - 它们已被弃用!使用PDO或MySQLi。您还应该考虑在磁盘上存储文件位置。使用MySQL存储图像被认为是Bad Idea™。使用像图像这样的大数据处理SQL表可能会有问题。
此外,您的HTML表单超出了标准。它应该是这样的:
<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
答案 1 :(得分:1)
file_get_contents方法不用于保存jpg图像,它用于将文件内容读入字符串。因为它将使用内存映射技术,如果服务器支持此功能,则可以提高性能。因此,您应首先在根文件夹或根文件夹中的任何位置创建一个文件夹,然后设置保存jpg图像的路径。
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["image"]["name"]);
$extension = end($temp);
if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/jpg")
|| ($_FILES["image"]["type"] == "image/pjpeg")
|| ($_FILES["image"]["type"] == "image/x-png")
|| ($_FILES["image"]["type"] == "image/png"))
&& ($_FILES["image"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["image"]["error"] > 0) {
echo "Return Code: " . $_FILES["image"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["image"]["name"] . "<br>";
echo "Type: " . $_FILES["image"]["type"] . "<br>";
echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["image"]["name"])) {
echo $_FILES["image"]["name"] . " already exists. ";
} else {
$file_name = $_FILES["image"]["name"];
move_uploaded_file($_FILES["image"]["tmp_name"],
"upload/" . $file_name);
echo "Stored in: " . "upload/" . $file_name;
$sql = "INSERT INTO images ( image,image_name) VALUES ( '$image', '$file_name')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
}
}
} else {
echo "Invalid file";
}
?>
答案 2 :(得分:0)
检查以下代码:
--- --- HTML
<form enctype="multipart/form-data" action="add.php" method="POST">
Image: <input type="file" name="photo">
<br>
<input type="submit" value="Add">
</form>
- PHP -
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);
//This gets all the other information from the form
$image_name=mysql_real_escape_string(addslashes($_FILES['image']['name']));
$image = mysql_real_escape_string(addslashes(file_get_contents($_FILES['image']['tmp_name'])));
// Connects to your Database
mysql_connect("YOUR_HOST", "USERNAME", "PSSWORD") or die(mysql_error()) ;
mysql_select_db("YOUR_DATABASE") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `product_images` VALUES ('', '$image', '$image_name')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file."; }
?>