【问题标题】:upload image to folder and insert path to database and text files in php将图像上传到文件夹并在 php 中插入数据库和文本文件的路径
【发布时间】:2016-07-15 09:35:50
【问题描述】:
<?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 和 GIF 文件。我已经尝试解决了很多次,但我自己找不到解决方案,请提出正确的解决方法。

【问题讨论】:

  • .txt 文件不在您允许的扩展数组中...
  • 你上传的是哪个文件?
  • 发生了什么?问题解决了吗?
  • 问题依然存在

标签: php image-uploading


【解决方案1】:

你需要改变

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

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

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

你总是返回 false 值并且你得到了那个错误

【讨论】:

  • 谢谢大家,我只是这样改变了,但仍然说对不起,只允许 JPG、JPEG、PNG 和 GIF 文件。
  • 你要上传的文件名是什么??
  • 上一个是上传图片的输入表单,我的文件类型是。 jpg在我的电脑里
  • echo $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // 获取图片扩展名并粘贴其值
【解决方案2】:

只需在你的数组中添加txt 扩展名

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

【讨论】:

    【解决方案3】:

    像这样在您的代码中添加 .txt 扩展名,

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

    希望它会有所帮助。

    【讨论】:

      【解决方案4】:

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

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

      它会起作用的。

      【讨论】:

        【解决方案5】:
        if(in_array($imgExt,$valid_extensions))     
        $valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt');
        

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

        【讨论】:

          【解决方案6】:

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

          <?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】:

            据我说,

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

            改成

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

            2) 正如 @Saty 回答的那样,您需要在

            中交换您的参数

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

            作为

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

            第一个参数为Search Value,第二个参数为Search Array

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-09-25
              • 1970-01-01
              • 2023-03-28
              • 2020-06-23
              • 1970-01-01
              • 1970-01-01
              • 2014-02-25
              • 1970-01-01
              相关资源
              最近更新 更多