使用PHP和MySql上传和删除图片
我正在建立一个页面,允许我上传和删除图片。此外,我可以查看该页面上的图片。现在,我可以上传图片,图片将存储在我的C:drive文件夹中,记录将上传到MySql。但是,我无法删除图片并在页面上查看图片。我不知道原因是什么。我想问题可能是图片的命名和文件位置 当我上传图片时,C:驱动器中的命名将根据时间更改为名称。MySql中记录的命名将保持不变 我不知道如何将这两个图像转换为相同的名称,并在MySql中提供一个允许我显示图像的路径,以便删除我的图片 我的页面代码如下:使用PHP和MySql上传和删除图片,php,mysql,image,Php,Mysql,Image,我正在建立一个页面,允许我上传和删除图片。此外,我可以查看该页面上的图片。现在,我可以上传图片,图片将存储在我的C:drive文件夹中,记录将上传到MySql。但是,我无法删除图片并在页面上查看图片。我不知道原因是什么。我想问题可能是图片的命名和文件位置 当我上传图片时,C:驱动器中的命名将根据时间更改为名称。MySql中记录的命名将保持不变 我不知道如何将这两个图像转换为相同的名称,并在MySql中提供一个允许我显示图像的路径,以便删除我的图片 我的页面代码如下: <div clas
<div class="container">
<?php
include "config.php";
if (isset($_FILES['file'])) {
//////
if ($_FILES["file"]["error"] > 0) {
echo "Error: :{$_FILES["file"]["error"]}<br>";
} else {
$allowdExts = ["jpg", "jpeg", "gif", "png"];
$filenameStrArr = explode(".", $_FILES["file"]["name"]);
$extension = strtolower(end($filenameStrArr));
////-----------------------------
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp = $_FILES['file']['tmp_name'];
///-------------------------------
$allowedTypes = ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg"];
$fileType = $_FILES["file"]["type"];
$sizeLimit = 2000;
$fileSize = $_FILES["file"]["size"] / 1024; //divide to get size in kb
if (in_array($extension, $allowdExts) &&
in_array($fileType, $allowedTypes) &&
($fileSize <= $sizeLimit)) {
$distDir = "../uploads/";
$distFilename = "pic_" . time() . ".{$extension}";
$upload = $distDir . "/" . $distFilename;
//$distFinal = "upload/" . $_FILES["myFile"]["name"];
move_uploaded_file($_FILES['file']['tmp_name'], $upload);
}
}
/*
$file = '../uploads/' . $_FILES['file']['name'];
$upload = move_uploaded_file($tmp, $file); */
if ($upload) {
$add = $db->prepare("insert into upload values('',?)");
$add->bindParam(1, $name);
if ($add->execute()) {
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Success!</strong> File upload successful to database.
</div>
<?php
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Failed!</strong> File upload unsuccessful to database.
</div>
<?php
}
} else {
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Sorry!</strong> File upload unsuccessful .
</div>
<?php
}
}
?>
<form method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="file">Upload File</label>
<input type="file" id="file" name="file">
<p class="help-block">This will be the picture you upload.</p>
</div>
<button type="submit" class="btn btn-default">Upload</button>
</form>
<p><br/></p>
<div class="row">
<?php
if (isset($_GET['remove'])) {
$img = $_GET['remove'];
$id = $_GET['id'];
$remove = unlink('uploads'.$img);
if ($remove) {
$rmv = $db->prepare("delete from upload where id='$id'");
if ($rmv->execute()) {
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Success!</strong> File removed from directory and database .
</div>
<?php
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Failed!</strong> File failed erased from database.
</div>
<?php
}
} else {
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Error!</strong> File is not erased from directory.
</div>
<?php
}
}
$stmt = $db->prepare("select * from upload");
$stmt ->execute();
while ($row = $stmt->fetch()) {
?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img style="height:200px;"src="uploads<?php echo$row['foto'] ?>" alt="<?php echo$row['foto'] ?>" title="<?php echo$row['foto'] ?>">
<div class="caption text-center">
<p><a href="?remove=<?php echo$row['foto'] ?> &id<?php echo$row['id'] ?>" class="btn btn-danger" role="button">Delete</a></p>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
1。您应该使删除查询更易于保存。
$rmv = $db->prepare("delete from upload where id=:file_id");
$rmv->bindValue(':file_id', $id, \PDO::PARAM_INT);
2。然后,您可能忘记了删除文件行中的斜杠。
$rmv = $db->prepare("delete from upload where id=:file_id");
$rmv->bindValue(':file_id', $id, \PDO::PARAM_INT);
改变
$remove = unlink('uploads'.$img);
到
我还建议您只从数据库中读取文件路径,而不是通过get请求。您已经获得了文件的id,对吗?使用文件id作为主键,而不是文件名
3。在您试图显示图像的行中也有一个/缺失
改变
“alt=”“title=”“>
到
“alt=”“title=”“>
您使用的是哪台服务器?将图像保留在项目文件夹的本地位置,并确保为每个图像指定了正确的路径。您可以将完整图像路径存储在数据库中,也可以在脚本中定义静态路径,并附加从数据库检索到的每个文件名
<img style="height:200px;"src="uploads<?php echo$row['foto'] ?>" alt="<?php echo$row['foto'] ?>" title="<?php echo$row['foto'] ?>">
<img style="height:200px;"src="uploads/<?php echo$row['foto'] ?>" alt="<?php echo$row['foto'] ?>" title="<?php echo$row['foto'] ?>">