在php中使用标题和详细信息插入照片标题和详细信息

问题描述:

我创建了一个页面,我可以上传图像文件,但我希望能够添加一些图片的细节并同时上传,但我没有任何想法我怎样才能从图片文本框中的细节上传到我的表格中。在php中使用标题和详细信息插入照片标题和详细信息

这里是我的index.php代码:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa-IR"> 
<head> 
<meta name="author" content="www.hamyarprojects.com" /> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Upload file</title> 
<link href="css/Style.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
<div id="mainframe" > 
    <div id="lp" class = "panel panel-primary"> 
     <div class = "panel-heading"> 
      <h3 class = "panel-title">Upload file test</h3> 
     </div> 
     <div class = "panel-body"> 
      <form action="Upload.php" method="post" enctype="multipart/form-data"> 
       <p id="btn_box"> 
       <input type="file" name="upload_image" /> 
       </p> 
       <p id="btn_box"> 
       <input type="submit" value="Upload Photo" name="submit"> 
       <input type="reset" value="Clear" name="reset"> 
       </p> 
      </form> 
     </div> 
    </div> 
</div> 
</body> 
</html> 

这里是upload.php代码

<link href="css/Style.css" rel="stylesheet" type="text/css"> 
<div class="main"> 
<?php 

if (!isset($_FILES['upload_image'])) { 
    echo '<p>Please select a image</p>'; 
} else { 
    try { 
     upload(); 
     echo '<p>Upload successfully</p>'; 
    } catch(Exception $e) { 
     echo '<h4>'.$e->getMessage().'</h4>'; 
    } 
} 

function upload() { 
    if (is_uploaded_file($_FILES['upload_image']['tmp_name']) 
     && getimagesize($_FILES['upload_image']['tmp_name']) != false 
    ) { 
     $size = getimagesize($_FILES['upload_image']['tmp_name']); 
     $type = $size['mime']; 
     $imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb'); 
     $size = $size[3]; 
     $name = $_FILES['upload_image']['name']; 
     $maxsize = 99999999; 

     if ($_FILES['upload_image']['size'] < $maxsize) { 
      $dbh = new PDO("mysql:host=localhost;dbname=bmc", 'root', ''); 
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      $stmt = $dbh->prepare("INSERT INTO imageblob (image_type, " 
       . "image, image_size, image_name) VALUES (? ,?, ?, ?)"); 

      $stmt->bindParam(1, $type); 
      $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB); 
      $stmt->bindParam(3, $size); 
      $stmt->bindParam(4, $name); 
      $stmt->execute(); 
     } else { 
      throw new Exception("size invalid"); 
     } 
    } else { 
     throw new Exception("invalid format !"); 
    } 
} 

</div> 
+0

你可能会思前想这一点。当您提交表单时,您可以访问'$ _FILES'中的文件,但也可以在'$ _POST'中将发布的数据放在表单中,然后访问它们以获取详细信息。 – nerdlyist

难道你不只是添加一个文本框

<input type="text" name="imageDetails" /> 

然后在你的代码捕获的方式文本框

$imageDetails = filter_input(INPUT_POST, "imageDetails", FILTER_SANITIZE_SPECIAL_CHARS); 

然后列添加到您的表imageBlob称为imageDetails

然后将查询更改为

$stmt = $dbh->prepare("INSERT INTO imageblob (image_type ,image, image_size, image_name, imageDetails) VALUES (? ,?, ?, ?, ?)"); 

然后添加绑定PARAM

$ stmt-> bindParam(5, $ imageDetails);

希望这有助于