上传和删除图片php sql

时间:2011-06-23 14:02:58

标签: php mysql image-uploading unlink

确定,

我已经构建了一个插入图片页面(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”。你知道我会把它插入代码吗?

再次感谢您的帮助,

JD