Ошибка вставки файла PHP в таблицу SQL Server - PullRequest
0 голосов
/ 22 октября 2019

Я пытаюсь вставить изображение в таблицу SQL Server. Поле имеет тип носителя. Файлы для загрузки должны быть либо PDF, Word или изображения. Фрагмент выдает эту ошибку:

Array ([0] => Array ([0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2]] => [Microsoft] [Драйвер ODBC 17 для SQL Server] [SQL Server] Неверный синтаксис рядом с «jpg». [Message] => [Microsoft] [Драйвер ODBC 17 для SQL Server] [SQL Server] Неверный синтаксис рядом с «jpg»'.))

Вот код.

<?php
include 'core/init.php';
$user_id  = $_SESSION['uid'];
$user     = $userObj->userData($uid);


if(isset($_POST["submit"])) {

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        //echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else { 
    $temp = explode(".", $_FILES["fileToUpload"]["name"]);
    $newfilename = round(microtime(true)) . '.' . end($temp);
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $newfilename))
    {
        $path = $target_dir.$newfilename;
        $type = $imageFileType;
        $data = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        $file =$_FILES["fileToUpload"]["name"];
        $table='[Consolidated HR GRANTS PAYROLL 2018].[dbo].[FSD TEST$att]';
        $r=$target_dir.$file;
        if(!get_magic_quotes_gpc())
            {

                $t = addslashes($r);

            }
        ***$sql=
        "INSERT INTO $table
                   ([No_],
                   [Attachment])

             VALUES
                   ('3'                  
                   ,$t)";***





        $stmt=sqlsrv_query( $conn, $sql);
        if($stmt){
                sqlsrv_commit($conn);
                //echo "Transaction committed.<br />";
               // $userObj->redirect('grantappsubmit.php');

        } else {
            die(print_r( sqlsrv_errors(), true));
                sqlsrv_rollback($conn); 
                file_put_contents("commiterror.txt", $commiterror, FILE_APPEND);
        }


        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";    
    } else{
        echo "Sorry, there was an error uploading your file.";
    }

   }

}
?>


<!DOCTYPE html>
<html>
<head>
    <title>
        Grant Application
    </title>
    <script>assets/js/apply_function.js</script>
    <script>assets/js/main.js</script>
    <link rel = "icon" href = "assets/img/favicon.ico"  type = "image/x-icon">
    <link href="assets/css/style.css" rel="stylesheet" />
    <body class="body">
    <!-- Load an icon library to show a hamburger menu (bars) on small screens -->   
    <div class="topnav" id="myTopnav">
        <a href="home.php" class="active">Home</a>
        </a>
    </div>   
    <div class="container">
    <form action="" method="post" enctype="multipart/form-data">

    <div class="row">
  <h1>
    Grant Proposal
   </h1><br> 

  </select>

  </div>  


    </div>
    <div class="row">
        <div class="col-25">
          <label for="grant">Budget</label>
        </div>
        <div class="col-75">
          <input type="text" id="grantBudget" name="grantBudget" placeholder="Grant budget.." >
        </div>

    <div class="row">

      </div>
      <div class="row">
        <div class="col-25">
          <label for="grant">Grant Proposal</label>
        </div>
        <div class="col-75">
          <textarea id="grantproposal" name="grantproposal" placeholder="Grant to.." style="height:200px"></textarea>
        </div>
        <div class="row">

      </div>

    <div>
    <label for="grant">Attach Supporting Documents</label>
    <br>
     <input type="file" placeholder='cc' name="fileToUpload" accept="image/*">
     <input type="file" value='cc' name="pic" accept="image/*">
     <input type="file" value='cc' name="pic" accept="image/*">
    </div>


    <input type="submit" value="Submit" name="submit">
</form>

</body>
</html>
...