上传和删除图像php sql
好的,
我已经构建了一个插入图像页面(uploader.php),但我有两个问题。
代码是:
<?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
Ok,
I have built an insert image page (uploader.php) but I have 2 issues.
The code is:
<?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>
So the first issue is that the information is not being entered into the database - the table has 4 fields - id(int), title(varchar), desc(varchar) and photo(varchar). Is it because the id field is not being specified?? This is simply the auto incremented primary key for the table.
The second issue is that the image that is being loaded contains spaces in it - for example, when uploading "test image.jpg" - I would like to incorporate a str_replace to create "testimage.jpg". Do you know where I would insert this into the code?
Thanks again for any help,
JD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
:)
http://www.tizag.com/phpT/fileupload.php
http://www.freewebmasterhelp.com/tutorials/phpmysql
http://php.net/manual/en/function.unlink.php
那应该做吧:)
:)
http://www.tizag.com/phpT/fileupload.php
http://www.freewebmasterhelp.com/tutorials/phpmysql
http://php.net/manual/en/function.unlink.php
That should do it :)