将图像上传到网络服务器并将路径存储在数据库中
Upload image to web server and store path in database
我正在尝试使用 php 将图像上传到我的网络服务器并将图像的路径存储在 mysql 数据库中,我无法执行此操作。任何帮助,将不胜感激。当我点击提交时,我可以看到一个空白的 php 页面..
post.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$name = $_POST['ctitle'];
$description = $_POST['cdesc'];
$image = $_FILES['image']['name'];
$filepath = "images/";
$filepath = $filepath . basename($image);
if(move_uploaded_file($image, $filepath)){
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$sql="INSERT INTO bbmp (dt,title,description,filename,img_path)
VALUES (NOW(), '$name','$description','$image','$filepath')";
mysql_select_db('bbmp',$con);
$retval = mysql_query( $sql, $con );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($con);
}
?>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Post your complaints here</h1>
<form method="POST" action="post.php">
<input type="name" name="ctitle"><br />
<textarea name="cdesc"></textarea><br />
<input type="file" name="image" ><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
sql
CREATE TABLE IF NOT EXISTS `bbmp` (
`id` int(100) unsigned NOT NULL AUTO_INCREMENT,
`dt` datetime NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`filename` varchar(255) NOT NULL,
`img_path` int(255) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
上传文件时需要有效的加密类型:enctype="multipart/form-data"
改为:
<form method="POST" action="post.php" enctype="multipart/form-data">
你也需要把变量改成这个,因为 $_FILES['image']['name'] 只是一个名字,所以把这个改成 运行 move_uploaded_file()函数
$image = $_FILES['image']['tmp_name'];
我正在尝试使用 php 将图像上传到我的网络服务器并将图像的路径存储在 mysql 数据库中,我无法执行此操作。任何帮助,将不胜感激。当我点击提交时,我可以看到一个空白的 php 页面..
post.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$name = $_POST['ctitle'];
$description = $_POST['cdesc'];
$image = $_FILES['image']['name'];
$filepath = "images/";
$filepath = $filepath . basename($image);
if(move_uploaded_file($image, $filepath)){
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$sql="INSERT INTO bbmp (dt,title,description,filename,img_path)
VALUES (NOW(), '$name','$description','$image','$filepath')";
mysql_select_db('bbmp',$con);
$retval = mysql_query( $sql, $con );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($con);
}
?>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Post your complaints here</h1>
<form method="POST" action="post.php">
<input type="name" name="ctitle"><br />
<textarea name="cdesc"></textarea><br />
<input type="file" name="image" ><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
sql
CREATE TABLE IF NOT EXISTS `bbmp` (
`id` int(100) unsigned NOT NULL AUTO_INCREMENT,
`dt` datetime NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`filename` varchar(255) NOT NULL,
`img_path` int(255) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
上传文件时需要有效的加密类型:enctype="multipart/form-data"
改为:
<form method="POST" action="post.php" enctype="multipart/form-data">
你也需要把变量改成这个,因为 $_FILES['image']['name'] 只是一个名字,所以把这个改成 运行 move_uploaded_file()函数
$image = $_FILES['image']['tmp_name'];