上传和删除图像php sql

发布于 2024-11-16 23:43:58 字数 1337 浏览 2 评论 0原文

好的,

我已经构建了一个插入图像页面(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 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文