PHP / MySQLの:IDの名前の変更$ _FILES [ 'イメージ'] [ '名前']ベース

ピーター・サンルーフ:

誰の助けに私は私の問題を解決するためにできますか?現在、私は成功した写真や機能をアップロードすることができ、システムを作成しました。しかし、データベースにしても、サーバのフォルダに保存した写真の名前は、写真の実際の名前です。

今、私はIDに基づいて、写真の名前を変更したいです。以下は、私のコードは次のようになります。

<?php

    require_once '../../../../config/configPDO.php';

    $report_id = $_POST['report_id'];

    $image = $_FILES['uploadFile']['name'];

    // image file directory
    $target = "../../../../images/upload/".basename($image);
    $ServerURL = "http://172.20.0.45/tgotworker_testing/images/upload/$image";

      // Prepare an insert statement
        $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();


          // Attempt to execute the prepared statement
          if($sql&&move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target)){
              // Records created successfully. Redirect to landing page
            echo "<script>alert('Saved')</script>";
            header("Location: view_task.php?report_id=".$_POST['report_id']);

              exit();
          } else{
              echo "Something went wrong. Please try again later.";
          }

?>
Tushar:

このコードを試してみてください

require_once '../../../../config/configPDO.php';

$report_id = $_POST['report_id'];
$image = $_FILES['uploadFile']['name'];

//set new name for upload image
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $report_id. '.' . end($temp);

$target = "../../../../images/upload/".$newfilename;
$ServerURL = "http://172.20.0.45/tgotworker_testing/images/upload/$newfilename";

// Prepare an insert statement
$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();


// Attempt to execute the prepared statement
if($sql&&move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target)){
     // Records created successfully. Redirect to landing page
     echo "<script>alert('Saved')</script>";
     header("Location: view_task.php?report_id=".$_POST['report_id']);
      exit();
} else{
          echo "Something went wrong. Please try again later.";
}

?>

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=25891&siteId=1