将上传图像的文件路径保存到MySQL数据库 [英] Saving Filepath Of Uploaded Image To MySQL Database
问题描述
为此,我经历了无数不同的帮助菜单和主题,但仍然遇到问题.我只想将上传图像的文件路径插入MySQL数据库.我尝试将图像传递给变量,然后使用查询将其推送到数据库,但是它不起作用.我的代码在下面,表格在上面,php在下面:
I have gone through countless different help menus and topics for this and still having problems. I simply want to insert the filepath of an uploaded image into a MySQL database. I have tried passing the image on to a variable and then using a query to push that to the database but it is not working. My code is below, form is on top, php is below:
<html>
<body>
<h1>test</h1>
<form action="insert.php" method="post" enctype="multipart/form-data">
Name <input type="text" name="name" /><br><br>
Description <input type="text" name="desc" /><br><br>
Price Low<input type="text" name="price_low" /><br><br>
Price High <input type="text" name="price_high" /><br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
3.Send this file: <input name="userfile" type="file" />
4.<input type="submit" value="Send File" /
<input type="submit" />
</form>
a
</body>
</html>
<html>
<?php
//upload image
$uploaddir = '';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
//end of upload image
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
$con = mysql_connect("localhost","admintest","gen");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test2", $con);
$sql="INSERT INTO products (name, description, price_low, price_high)
VALUES
('$_POST[name]','$_POST[desc]','$_POST[price_low]','$_POST[price_high]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
推荐答案
安全性问题和不建议使用的扩展名,您需要做的就是将文件名插入数据库.为此,请在数据库中添加一个文件名"字段,然后相应地调整插入查询:
Security issues and deprecated extension aside, all you need to do is insert the file name to the database. To do that, add a "filename" field to your database and then adjust your insert query accordingly:
INSERT INTO products (name, description, price_low, price_high, filename)
VALUES (:name, :desc, :price_low, :price_high, :filename)
此外,您的$uploaddir
变量为空,这些文件可能现在甚至都没有保存.要正确移动文件,请尝试以下操作:
Also, your $uploaddir
variable is empty, the files probably aren't even being saved anywhere at the moment. To move your files properly, try something like this:
$uploaddir = '/path/where/you/can/save/';
$rawFilename = $_FILES['userfile']['name'];
$extension = pathinfo($rawFilename, PATHINFO_EXTENSION);
$uploadfile = $uploaddir . md5($rawFilename) . '.' . $extension;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
此脚本假定您信任上载的内容,而md5函数只是清理"(如果可以这样称呼)文件名的一种快速简便的方法.
This script assumes you trust the uploaded content and the md5 function is just is just a quick and easy way to "sanitize" (if I can call it that) the file's name.
这篇关于将上传图像的文件路径保存到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!