我正在构建一个允许我上传和删除图片的页面。此外,我可以在该页面上查看图片。现在,我可以上传图片,图片将存储在我的C:盘文件夹中,记录将上传到MySql。但是,我无法删除图片并查看页面上的图片。我不确定这些的原因。我猜问题可能是图片的命名和文件位置。
当我上传图片时,C:盘中的命名将根据时间更改为名称。 MySql中记录的命名仍将保持不变。
我不确定如何将这两个图像转换为相同的名称,并在MySql中提供一个路径,允许我显示图像以删除我的图片。
我的页面代码如下:
<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>
答案 0 :(得分:1)
<强> 1。你应该删除你的删除查询。
<div class="nested-fields">
<li>
<%= f.label :name %>
<%= f.text_field :name%>
<%= link_to_remove_association 'Remover Dependente', f %>
</li>
</div>
<强> 2。然后,您可能忘记了删除文件行中的斜杠。
变化
<?php
include '../dbh.php';
session_start(); //THIS APPEARS TO BE MISSING
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE email='$email' AND pwd='$pwd'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result)) {
//You have Session no matter what for this user
$_SESSION['authenticated'] = false;
} else {
$_SESSION['authenticated'] = true;
$_SESSION['id'] = $row['id'];
}
header("Location: ../index.php");
?>
到
<?php
if($_SESSION['authenticated']) {
echo $_SESSION['id'];
} else {
echo "You are not logged in!";
}
?>
我还建议您,只从数据库中读取文件路径,而不是通过get请求。您已经获得该文件的ID了吗?使用文件ID作为主键,而不是文件名。
第3。您尝试显示图像时也会缺少/ 变化
$rmv = $db->prepare("delete from upload where id=:file_id");
$rmv->bindValue(':file_id', $id, \PDO::PARAM_INT);
到
$remove = unlink('uploads'.$img);