【发布时间】:2020-06-17 13:54:22
【问题描述】:
我目前创建了一个只允许用户上传照片的系统。已经上传的照片,如果用户想更换,可以更换。
当前代码显示该函数没有错误。在 MySQL 数据库中更新的 URL。但问题是图像不更新。以下是我当前的代码:
update_photo_before.php
<?php
require_once '../../../../config/configPDO.php';
$report_id = $_POST['report_id'];
$last_insert_id = null;
//Allowed file type
$allowed_extensions = array("jpg","jpeg","png");
//File extension
$ext = strtolower(pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION));
//Check extension
if(in_array($ext, $allowed_extensions)) {
$defID = "before_" . $report_id;
$imgPath = "images/$defID.png";
$ServerURL = "http://172.20.0.45/tgotworker_testing/android/$imgPath";
$query = "UPDATE ot_report SET photo_before = '$ServerURL', time_photo_before = GETDATE() WHERE report_id = :report_id";
$sql = $conn->prepare($query);
$sql->bindParam(':report_id', $report_id);
$sql->execute();
if ($sql){
move_uploaded_file($_FILES['uploadFile']['name'], $imgPath); //line 28
echo "<script>alert('Saved')</script>";
header("Location: view_task.php?report_id=".$_POST['report_id']);
}else{
echo "Error!! Not Saved";
}
} else {
echo "<script>alert('File not allowed')</script>";
header("Location: view_task.php?report_id=".$_POST['report_id']);
}
?>
我的文件夹图像位于 'tgotworker_testing' --> 'android' --> 'images'。
对于update_photo_before.php:
'tgotworker_testing' --> 'pages' --> 'dashboard' --> 'engineer' --> 'view_task' --> 'update_photo_before.php'
谁能解决我的问题?谢谢!
【问题讨论】:
-
if ($sql) {检查数据库是否已更新,如果已更新,则上传图像。也许你想先上传图片,然后更新数据库?然后你可以检查一下sql是否工作并反转图像上传?您需要确保该文件夹具有正确的权限才能上传图像(也许这就是它失败的原因) -
再次检查
$imgPath文件夹的权限。运行 php 的用户需要对该位置的写入权限。 -
@JeffVdovjak 你能帮我更新上面的代码吗? URL 在数据库中更新,但照片在服务器文件夹中没有更新
-
@AlexBarker 怎么查?
-
@PeterSondak 检查您的文件夹权限取决于您访问服务器的方式。如果您使用 FTP 或 webFTP,或者您使用的命令行会有所不同。 PHP图片上传权限信息可以在这个问题上找到:stackoverflow.com/questions/10990/…