图像上传名称未插入数据库

时间:2016-02-09 13:03:22

标签: php

我这样的html代码我有图像名称图像名称的问题没有插入数据库表我在哪里犯了错误我不知道在php.ini file_upload已经在我仍然面临文件上传的问题..

<form action="category.php" method="post" enctype="multipart/form-data">
            <input type="hidden" value="{$category_id}" name="category_id">
              <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputEmail1">Category Name</label>
                  <input type="text" class="form-control" name="category_nm" id="category_nm" placeholder="Enter Category Name" value="{$category_nm}" required>
                </div>
                <div class="form-group">
                  <label for="cat_img">File input</label>
                  <input type="file" id="cat_img" name="cat_img">
                  <p class="help-block">Please Select Category Image. (Image Type :- JPG, JPEG, PNG )</p>
                </div>
              </div><!-- /.box-body -->
              <div class="box-footer">
                <button type="submit" class="btn btn-primary" name="submit">Submit</button>
              </div><!-- /.box-footer-->
            </form>

我的php文件上传代码是这样的

if(isset($_REQUEST['submit']))
        {
          $category_nm = isset($_POST['category_nm']) ?  $_POST['category_nm'] : '';
          $cat_img     = isset($_POST['cat_img']) ?  $_POST['cat_img'] : '';

          $insertCat = "INSERT INTO category(category_nm,cat_img) 
                                 VALUES('".mysql_real_escape_string ($category_nm)."',
                                          '".mysql_real_escape_string ($cat_img)."')";
        $insertCatRes = mysql_query($insertCat);
        if($insertCatRes)
        {
          $target_dir = "_cat_img/";
          $target_file = $target_dir . basename($_FILES["cat_img"]["name"]);
          $uploadOk = 1;
          $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
          // Check if image file is a actual image or fake image
          if(isset($_POST["submit"])) 
          {
            $check = getimagesize($_FILES["cat_img"]["tmp_name"]);
            if($check !== false) 
            {
              echo "File is an image - " . $check["mime"] . ".";
              $uploadOk = 1;
            } 
            else
            {
              echo "File is not an image.";
              $uploadOk = 0;
            }
          }

          // Check file size
          if ($_FILES["cat_img"]["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 
          {
            if (move_uploaded_file($_FILES["cat_img"]["tmp_name"], $target_file)) 
            {
              echo "The file ". basename( $_FILES["cat_img"]["name"]). " has been uploaded.";
            }
            else
            {
              echo "Sorry, there was an error uploading your file.";
            }
          }
        }
        else
        {
           //header("Location:category.php");   
        }

      }

1 个答案:

答案 0 :(得分:1)

文件的发布时间$_FILES不是$_POST

更改

$cat_img = isset($_POST['cat_img']) ? $_POST['cat_img']: '';

$cat_img = isset($_FILES['cat_img']['name']) ? $_FILES['cat_img']['name'] : '';