我编写了一个脚本来使用 PHP 将图像上传到一个文件夹,该文件夹应该将文件名链接到 MySQL。
我想在漫长的一天之后我错过了一些我看不到的东西:(
上传.php
<form enctype="multipart/form-data" action="add.php" method="POST">
Photo: <input type="file" name="images"><br>
<input type="submit" value="Add">
</form>
添加.php
<?php
error_reporting(E_ALL);
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['images']);
//This gets all the other information from the form
$images=($_FILES['images']);
// Connects to your Database
mysql_connect("localhost", "root", "pass") or die(mysql_error()) ;
mysql_select_db("formular") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$images')") ;
//Writes the pictures to the server
if(move_uploaded_file($_FILES['images']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']). " 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.";
}
?>
视图.php
<?php
// Connects to your Database
mysql_connect("localhost", "root", "pass") or die(mysql_error()) ;
mysql_select_db("formular") or die(mysql_error()) ;
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
Echo "<img src=images/".$info['images'] ."> <br>";
}
?>
完全感谢