使用文件路径更新SQL表

时间:2016-12-19 23:11:00

标签: php mysql pdo

我使用文件上传PHP将文件从用户输入表单获取到正确的路径:

@connect((store) => {
  return {
    ...store
  }
})
export default class SomeComponent extends React.Component {
...

处理<form method="post" action="form_action1.php" enctype="multipart/form-data"> <div class="form-group"> <label for="attachment1">Add file(s)</label> <input type="file" class="form-control-file" id="attachment1" aria-describedby="fileHelp" name="attachment1"> <input type="file" class="form-control-file" id="attachment2" aria-describedby="fileHelp" name="attachment2"> <input type="file" class="form-control-file" id="attachment3" aria-describedby="fileHelp" name="attachment3"> <small id="fileHelp" class="form-text text-muted">PDFs, Word Docs or images (PNG, JPG, etc.) Maximum 16 MB. </small> </div> </form> 的PHP脚本部分是:

$_FILES

文件上传到正确的位置,但不会将路径上传到SQL表。有人告诉我,我的脚本需要刷新查询,但它让我不知所措。

另外,我有多个文件上传区域。我可以<?php $servername = "localhost"; $username = "jobs_usr1"; $password = "XXXXXXXX"; try { $conn = new PDO("mysql:host=$servername;dbname=jobs_users", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; if(isset($_POST['submit']) && !empty($_POST['submit'])) { $job_name=$_POST['job_name']; $comments=$_POST['comments']; $attachment1=$_POST['attachment1']; $attachment2=$_POST['attachment2']; $attachment3=$_POST['attachment3']; $due_date=$_POST['due_date']; $requestor=$_POST['requestor']; $req_email=$_POST['req_email']; $property=$_POST['property']; } if(isset($_FILES['attachment1'])){ $errors= array(); $file_name = $_FILES['attachment1']['name']; $file_size = $_FILES['attachment1']['size']; $file_tmp = $_FILES['attachment1']['tmp_name']; $file_type = $_FILES['attachment1']['type']; $file_ext=strtolower(end(explode('.',$_FILES['attachment1']['name']))); $expensions= array("jpeg","jpg","png","doc","pdf"); if(in_array($file_ext,$expensions)=== false){ $errors[]="extension not allowed, please choose a JPEG, PNG, PDF or DOC file."; } if ( ! $errors ) { move_uploaded_file($file_tmp,"uploads/".$file_name); echo "Success"; }else{ print_r($errors); } } 进行array吗?

1 个答案:

答案 0 :(得分:0)

这从未得到回答,但我终于得到了答案。

所以这是:

首先 - 我的表格:

log4j.xml

然后我的php文件来处理输入:

<form method="post" action="form_action1.php" enctype="multipart/form-data">
   <div class="form-group">
        <label for="attachment1">Add file</label>
        <input type="file" class="form-control-file" id="attachment1" aria-describedby="fileHelp" name="attachment1">
   </div>

   <div class="form-group">
        <button type="submit" class="btn btn-primary" value="submit" name="submit" id="submit">Submit</button>
   </div>
</form>

所以我在这里做的是说<?php $servername = "localhost"; $username = "jobs_usr1"; $password = "XXXXXXX"; try { $conn = new PDO("mysql:host=$servername;dbname=jobs_users", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; if(isset($_FILES['attachment1'])){ $errors= array(); $file_name = $_FILES['attachment1']['name']; $file_size = $_FILES['attachment1']['size']; $file_tmp = $_FILES['attachment1']['tmp_name']; $file_type = $_FILES['attachment1']['type']; $file_ext=strtolower(end(explode('.',$_FILES['attachment1']['name']))); $expensions= array("jpeg","jpg","png","doc","pdf","docx","tif","tiff","zip","xls","xlsx","ppt","pptx","numbers"); if(in_array($file_ext,$expensions)=== false){ $errors[]="extension not allowed, please choose a different file"; } if ( ! $errors ) { move_uploaded_file($file_tmp,"uploads/".$file_name); echo "Success"; echo $file_name; }else{ print_r($errors); } } $sql = "INSERT INTO `jobs_canjobs`(`attachment1`) VALUES ('$file_name')"; $conn->exec($sql); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } $conn = null; ?> 意味着输入中有文件,我分配if(isset($_FILES['attachment1'])){,然后我使用$file_name然后将变量放入我的插入声明。

这对我有用。