Php 正在从文件路径中删除图像
我有一个仪表板,用户可以登录并上传自己的个人资料图片,保存到个人资料中。这会将图像移动到正确的文件夹,并将其正确插入数据库 直到最近,当我注意到图像消失时,它一直工作正常。在inspect控制台中,我注意到图像上出现了Php 正在从文件路径中删除图像,php,Php,我有一个仪表板,用户可以登录并上传自己的个人资料图片,保存到个人资料中。这会将图像移动到正确的文件夹,并将其正确插入数据库 直到最近,当我注意到图像消失时,它一直工作正常。在inspect控制台中,我注意到图像上出现了404notfound错误,因此我检查了文件路径内部,图像不再在那里(因此是404)。用户根本没有删除图像的脚本,只能上传 profile.php: <p><b>Profile Picture: </b> <?php $pictu
404notfound
错误,因此我检查了文件路径内部,图像不再在那里(因此是404)。用户根本没有删除图像的脚本,只能上传
profile.php:
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)){
echo "<img src='profiles/no-image.png' width='100' height='100' >";
} else {
echo "<img src='profiles/".$row['imagePath']."' width='100' height='100' >";
};
?>
<form action="scripts/edit-picture.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="edit-picture" value="Upload"/>
</p>
</form>
profiles
image1.jpg
image2.jpg
profile.php
scripts
edit-image.php
是否有人在通过
move\u uploaded\u file
将图像移动到文件夹中后,遇到图像实际上从文件夹中消失的情况,如有任何帮助或指导,将不胜感激。解决图像上传的条件,不要覆盖现有图像文件:
<?php
require 'db.php';
session_start();
$uploadDir = '../profiles/';
// if edit-picture has been clicked on, run this if statement
if (isset($_POST[ 'edit-picture' ])) {
$studentID = $_SESSION[ 'studentID' ];
// Creating 4 different variables using the $_FILES global variable to get data about the image that
// you can view data about a file by doing: print_r($image);
$fileName = $_FILES[ 'image' ][ 'name' ];
$tmpName = $_FILES[ 'image' ][ 'tmp_name' ];
$fileSize = $_FILES[ 'image' ][ 'size' ];
$fileType = $_FILES[ 'image' ][ 'type' ];
$filePath = $uploadDir . $fileName;
// The below doesn't work as it assigns different value to folder and in db for image name
// $filePath = md5($file_name . microtime()) . substr($fileName , -5, 5);
if (file_exists($filePath)) {
header("Location: ../profile.php?img=errorFileNameExists");
exit();
} // Checking file size - working
else if ($_FILES[ "image" ][ "size" ] > 5000000) {
header("Location: ../profile.php?img=errorFileSizeError");
exit();
}
$info = getimagesize($tmpName);
// empty $info - not known image
if (empty($info)) {
header("Location: ../profile.php?img=errorFileTypeError");
exit();
} // This is to show any errors that may occur if the connection fails, this helps with error checking.
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
header("Location: ../profile.php?img=errorFileRelocate");
exit;
}
else if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$stmt = $conn->prepare("INSERT INTO `profileImage` (`imagePath`, `studentID`)
VALUES ( ?, ?) ON DUPLICATE KEY UPDATE `imagePath` = VALUES (`imagePath`) ");
$stmt->bind_param("si", $fileName, $studentID);
$stmt->execute() or die("Failed to insert image into the database");
header("Location: ../profile.php?img=successImageUploaded");
exit();
}
}
?>
映像不能从文件系统中消失,我怀疑您是否会收到任何关于您的问题的反馈。但是,您是否有其他脚本可以让用户删除其图片?如果是这样的话,可能里面有错误,不应该删除的文件被删除了。这是一个ideaHi@Pierre我知道,我只是很困惑,没有一个脚本让他们删除他们的图像。你确定图像真的被上传和移动了吗?您是否能够重现该问题?@Pierre是的,图像被移动到文件夹路径中,即$uploadDir='../profiles/'
并在表profileImage
中显示为新行,如果您确定图像已上载(使用FTP浏览器检查),则问题不在代码中;也许你的主机的服务器出了大问题,不得不将你的文件恢复到以前的日期我确实尝试过,但是当我尝试上传一张不同的照片时,我收到了“FileNameExists”的消息(即使我尝试上传的新图像的文件名不同),可能是的顺序,如果位于错误的位置,则不确定。。。。
<?php
require 'db.php';
session_start();
$uploadDir = '../profiles/';
// if edit-picture has been clicked on, run this if statement
if (isset($_POST[ 'edit-picture' ])) {
$studentID = $_SESSION[ 'studentID' ];
// Creating 4 different variables using the $_FILES global variable to get data about the image that
// you can view data about a file by doing: print_r($image);
$fileName = $_FILES[ 'image' ][ 'name' ];
$tmpName = $_FILES[ 'image' ][ 'tmp_name' ];
$fileSize = $_FILES[ 'image' ][ 'size' ];
$fileType = $_FILES[ 'image' ][ 'type' ];
$filePath = $uploadDir . $fileName;
// The below doesn't work as it assigns different value to folder and in db for image name
// $filePath = md5($file_name . microtime()) . substr($fileName , -5, 5);
if (file_exists($filePath)) {
header("Location: ../profile.php?img=errorFileNameExists");
exit();
} // Checking file size - working
else if ($_FILES[ "image" ][ "size" ] > 5000000) {
header("Location: ../profile.php?img=errorFileSizeError");
exit();
}
$info = getimagesize($tmpName);
// empty $info - not known image
if (empty($info)) {
header("Location: ../profile.php?img=errorFileTypeError");
exit();
} // This is to show any errors that may occur if the connection fails, this helps with error checking.
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
header("Location: ../profile.php?img=errorFileRelocate");
exit;
}
else if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$stmt = $conn->prepare("INSERT INTO `profileImage` (`imagePath`, `studentID`)
VALUES ( ?, ?) ON DUPLICATE KEY UPDATE `imagePath` = VALUES (`imagePath`) ");
$stmt->bind_param("si", $fileName, $studentID);
$stmt->execute() or die("Failed to insert image into the database");
header("Location: ../profile.php?img=successImageUploaded");
exit();
}
}
?>