【问题标题】:Image upload php mysql图片上传php mysql
【发布时间】:2016-07-20 00:55:06
【问题描述】:

我正在尝试上传 2 张图片并将每个文件的文件名保存到 MySQL 数据库。我尝试了不同的选择,但我无法让它工作。

在我的 MYSQL 表中,文件名的字段名称称为 file_name varchar 500

我可以毫无问题地将其他表单数据保存到我的数据库中。 但是我无法上传图片。

以下是我在网络表单中的图片部分。

这里是 FORM 操作区

<form method="post" action="php/smartprocess.php" enctype="multipart/form-data" id="smart-form">

我的图片上传部分如下

smartprocess.php 部分是

 <?php 

if (!isset($_SESSION)) session_start(); 
if(!$_POST) exit;

require 'database.php';

include dirname(__FILE__).'/settings/settings.php';
include dirname(__FILE__).'/functions/emailValidation.php';

$TechName = strip_tags(trim($_POST["TechName"]));
$FullAssembly = strip_tags(trim($_POST["FullAssembly"]));
$Notes = strip_tags(trim($_POST["Notes"]));
$SignedDate = strip_tags(trim($_POST["SignedDate"]));
$captcha = strip_tags(trim($_POST["captcha"]));




 try {

       $q = "INSERT INTO tportal (TechName, FullAssembly, Notes, SignedDate)

             VALUES (:TechName, :FullAssembly, :Notes, :SignedDate)";


                     $query = $conn -> prepare($q);

                     $results = $query -> execute(array(

                      ":TechName" => $TechName,
                      ":FullAssembly" => $FullAssembly,

                      ":Notes" => $Notes,
                      ":SignedDate" => $SignedDate,

                      ));

                      if ($conn->query($q)) {

                          $errors = array();

                      echo '<div class="alert notification alert-success">Problem has accured please try again.</div>';
                     //Javascript alert top
                     /*echo "<script type= 'text/javascript'>alert('Issue adding data');</script>";*/

                     }
                     else{
                      echo '<div class="alert notification alert-success">Your message has been sent successfully!</div>';
                     //Javascript alert top
                     /*echo "<script type= 'text/javascript'>alert('Data not successfully Inserted. $PocketCond');</script>";*/

                     }

                      $conn = null;
                     }
                     catch(PDOException $e)
                     {
                     echo $e->getMessage();
                     }


                     ?>
                 <?php

                if(isset($_POST["captcha"])){
                if (!$captcha) {
                    $errors[] = "You must enter the captcha code";
                } else if (($captcha) != $_SESSION['gfm_captcha']) {
                $errors[] = "Captcha code is incorrect";
    }
}

?>



            <div class="section">
                    <label for="file1" class="field-label"> 
                        Upload another image - <span class="small-text   fine-grey"> (ONLY JPG : PNG : PDF) </span> 
                    </label>
                    <label class="field prepend-icon file">
                        <span class="button btn-primary"> Choose File </span>
                        <input type="file" class="gui-file" name="image"  id="file1" 
                        onChange="document.getElementById('uploader1').value = this.value;">
                        <input type="text" class="gui-input" id="uploader1" placeholder="no file selected" readonly>
                        <span class="field-icon"><i class="fa fa-upload"></i></span>
                    </label>
                </div><!-- end  section -->

非常感谢您的帮助和时间。 真诚的,

【问题讨论】:

  • 我想讲到这一点,但是我之前没有上传过文件。我已将以下内容添加到我的 smartprocess.php 但文件甚至没有上传到 uploads/ 文件夹。忘记将文件名存储到Mysql
  • if(is_uploaded_file($_FILES['image']['tmp_name'])){ $folder = "uploads/"; $file = basename($_FILES['image']['name']); $full_path = $folder.$file; if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {

标签: php mysql image file-upload


【解决方案1】:

使用 php 的$_FILES 变量来帮助您上传文件。 php中有一个简单的文件上传教程here

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 2013-03-27
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2017-06-05
    • 2015-01-17
    • 2013-03-02
    • 1970-01-01
    相关资源
    最近更新 更多