【问题标题】:PHP / MySQL: Failed to save image to folder at server but link URL is updated MySQL databasePHP / MySQL:无法将图像保存到服务器上的文件夹,但链接 URL 已更新 MySQL 数据库
【发布时间】: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/…

标签: php mysql


【解决方案1】:

您检查数据库是否更新,但不检查图像是否保存到服务器。

move_uploaded_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.";
  }
}

当 PHP 无法上传文件时,它会抛出警告。如果您在调试时打开警告,它应该会告诉您它不工作的原因,您将能够更好地解决问题。

将这些行放在脚本的顶部以显示所有警告和错误:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

【讨论】:

  • 由于 $ServerURL 没有声明,代码应该放在哪里?
  • 我已更新代码以包含您的所有代码。该代码块旨在替换您的 if($sql) 块......在这里它被组装了。
  • 我收到了这个错误,错误!!无法将文件上传到服务器。
  • 这是您编写的脚本的错误。您需要打开 PHP 警告才能查看为什么它没有上传。或者您将需要学习如何使用 try/catch 块,以便捕获错误。您是否在代码顶部添加了 ini_seterror_reporting 行?
猜你喜欢
  • 1970-01-01
  • 2020-06-09
  • 2014-02-25
  • 2017-10-10
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
相关资源
最近更新 更多