行,
我已经建立了一个插入图像页面(uploader.php),但我有 2 个问题。
代码是:
<?php
$target = "images/test/";
$target = $target . basename( $_FILES['photo']['name']);
$title=$_POST['title'];
$desc=$_POST['desc'];
$pic=($_FILES['photo']['name']);
mysql_connect("dbhost", "dbuser", "dbpass") or die(mysql_error()) ;
mysql_select_db("dbname") or die(mysql_error()) ;
mysql_query("INSERT INTO `test` VALUES ('$title', '$desc', '$pic')") ;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Title: <input type="text" name="title"><br>
Description: <input type="text" name = "desc"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form>
所以第一个问题是信息没有被输入到数据库中——该表有 4 个字段——id(int)、title(varchar)、desc(varchar) 和 photo(varchar)。是因为没有指定 id 字段吗?这只是表的自动递增主键。
第二个问题是正在加载的图像中包含空格 - 例如,在上传“test image.jpg”时 - 我想合并一个 str_replace 来创建“testimage.jpg”。你知道我会把它插入代码的什么地方吗?
再次感谢任何帮助,
京东