upload image to folder and insert path to database and text files in php

我的梦境 提交于 2019-12-24 17:43:32

问题


<?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.";     
  }
?>

I have this code to upload image and text files but it say Sorry, only JPG, JPEG, PNG & GIF files are allowed. I've tried to solve this many times but i can't find solution on my own, please suggest the correct way of doing this.


回答1:


You need to change

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

To

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

in_array need first parameter as search value and second parameter as your array

You always return false value and you got that error




回答2:


just add txt extension in ur aarray

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



回答3:


Add .txt extension in your code like this,

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

Hope it will help.




回答4:


You just correct your code in this line:

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

it will work.




回答5:


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

You will need to modify code with this code...




回答6:


This code will insert image in a folder and path in database:

<?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";
?>*



回答7:


According To Me,

1) You wrote text files is also getting uploaded. But, no text files extension added to $valid_extensions.

Change To

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

2) As @Saty answered, you need to swap your parameter in

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

as

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

First parameter act as Search Value and Second Parameter as Search Array.



来源:https://stackoverflow.com/questions/38392740/upload-image-to-folder-and-insert-path-to-database-and-text-files-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!