PHP/MySQL:无法将图像保存到服务器上的文件夹,但链接URL已更新到MySQL数据库
我目前创建了一个只允许用户上传照片的系统。如果用户想要更改已上载的照片,则可以将其替换 当前代码显示此函数中没有错误。在MySQL数据库中更新了URL。但问题是图像不会更新。以下是我目前的代码: 更新_photo_before.phpPHP/MySQL:无法将图像保存到服务器上的文件夹,但链接URL已更新到MySQL数据库,php,mysql,Php,Mysql,我目前创建了一个只允许用户上传照片的系统。如果用户想要更改已上载的照片,则可以将其替换 当前代码显示此函数中没有错误。在MySQL数据库中更新了URL。但问题是图像不会更新。以下是我目前的代码: 更新_photo_before.php <?php require_once '../../../../config/configPDO.php'; $report_id = $_POST['report_id']; $last_insert_id = null;
<?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']);
}
?>
检查数据库是否更新,但不检查映像是否保存到服务器
move\u upload\u file()
在成功时返回true
,在失败时返回false
。因此,您可以在代码中对$sql执行类似的测试
<?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";
// Check if image is uploaded to folder
if(move_uploaded_file($_FILES['uploadFile']['name'], $imgPath) === true) {
// If upload succeeds, then update database
$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();
// Check if database updated
if (!$sql){
echo "Error!! Database not updated.";
} else {
// Success!
header("Location: view_task.php?report_id=".$_POST['report_id']);
}
} else {
// Could not upload file
echo "Error!! Could not upload file to server.";
}
}
if($sql){
检查数据库是否已更新,如果已更新,则上载图像。可能您希望先上载图像,然后更新数据库?然后您可以检查sql是否工作并反转图像上载?您需要确保文件夹具有上载图像的正确权限(也许这就是它失败的原因)仔细检查位于$imgPath
的文件夹的权限。运行php的用户将需要对该位置的写入权限。@JeffVdovjak您能帮我更新上面的代码吗?URL在数据库中更新,但照片在服务器上更新folder@AlexBarker如何检查?@PeterSondak检查您的文件夹权限取决于这取决于您访问服务器的方式。如果您使用FTP或webFTP,或者您使用的命令行将有所不同。PHP图像上载权限信息可以在以下问题中找到:由于$ServerURL未声明,因此将代码放在何处?我已更新代码以包含您的所有代码。代码块旨在替换您的If($sql)
block…在这里组装。我遇到了这个错误,error!!无法将文件上载到服务器。这是您编写的脚本中的错误。您需要打开PHP警告以查看它不上载的原因。或者您需要学习如何使用try/catch块以便捕获错误。您是否添加了ini集
和error_报告
代码顶部的行?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);