将图像上传到文件夹并在php中插入数据库和文本文件的路径

时间:2016-07-15 09:35:51

标签: php image-uploading

<?php
error_reporting( ~E_NOTICE );
require_once 'dbconfig.php';

if($_POST)
{
  $firstname = $_POST['firstname'];
  $lastname = $_POST['lastname'];
  $description = $_POST['description'];
  $election_name=$_POST['election_name'];
  $category_name=$_POST['category_name'];

  $imgFile = $_FILES['user_image']['name'];
  $tmp_dir = $_FILES['user_image']['tmp_name'];
  echo $imgSize = $_FILES['user_image']['size'];

  $upload_dir = 'user_images'; // upload directory

  $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

  // valid image extensions
  $valid_extensions = array('jpg', 'jpeg', 'png', 'gif'); // valid extensions

  // rename uploading image
  $photo = rand(1000,1000000).".".$imgExt;

  // allow valid image file formats
  if(in_array($valid_extensions,$imgExt)) {            

    // Check file size '5MB'
    if($imgSize > 5000000) { 
      move_uploaded_file($tmp_dir,"$upload_dir/$imgFile");
        try {

          $stmt = $db_con->prepare("INSERT INTO candidate(firstname,lastname,description,election_name,category_name,photo) VALUES(:ename, :edept, :esalary, :elect, :cat, :ima)");
          $stmt->bindParam(":ename", $firstname);
          $stmt->bindParam(":edept", $lastname);
          $stmt->bindParam(":esalary", $description);
          $stmt->bindParam(":elect", $election_name);
          $stmt->bindParam(":cat", $category_name);
          $stmt->bindParam(":ima", $photo);

          if($stmt->execute())
          {
              echo "Successfully Added";
          }
          else{
              echo "Query Problem";
          }   
        }
        catch(PDOException $e){
            echo $e->getMessage();
        }
    }
    else{
        echo "Sorry, your file is too large.";
    }
  }
  else {
      echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";     
  }
?>

我有这个代码上传图片和文本文件,但它说抱歉,只有JPG,JPEG,PNG&amp;允许GIF文件。我试图解决这个问题很多次,但我自己找不到解决办法,请提出正确的方法。

7 个答案:

答案 0 :(得分:1)

您需要更改

 if (in_array($valid_extensions, $imgExt)) {

 if (in_array($imgExt,$valid_extensions)) {

in_array需要第一个参数作为搜索值,第二个参数作为数组

您总是返回错误值而且您收到了该错误

答案 1 :(得分:0)

只需在你的aarray

中添加txt个扩展名即可
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif', 'txt');

答案 2 :(得分:0)

在您的代码中添加.txt扩展名,如下所示

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','.txt'); 

希望它会有所帮助。

答案 3 :(得分:0)

您只需更正此行中的代码:

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); // valid extensions

它会起作用。

答案 4 :(得分:0)

if(in_array($imgExt,$valid_extensions))     
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt');

您需要使用此代码修改代码...

答案 5 :(得分:0)

此代码将图像插入数据库中的文件夹和路径:

<?php
$grocery=$_POST['grocery'];
$connect=new mysqli("localhost","root","");
mysqli_select_db($connect,'go-web');
$target_dir = "..images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        die("File is not an image.");
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    die("Sorry, file already exists.");
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    die("Sorry, your file is too large.");
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
    die("Sorry, only JPG, JPEG & PNG files are allowed.");
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    die("Sorry, your file was not uploaded.");
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        die("Sorry, there was an error uploading your file.");
    }
    }
    $query=mysqli_query($connect,"INSERT INTO product VALUES ('$grocery','$target_file')");
    if($query)
    echo "Uploaded";
?>*

答案 6 :(得分:0)

据我说,

1)您写的文本文件也正在上传。但是,$valid_extensions没有添加任何文本文件扩展名。

更改为

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); // valid extensions

2) @Saty 回答时,您需要在

中交换参数

if (in_array($valid_extensions, $imgExt)) {

as

if (in_array($imgExt,$valid_extensions)) {

第一个参数充当Search Value,第二个参数充当Search Array