在数据库中上传图片时文件名太长

时间:2019-02-07 08:30:58

标签: php file-upload

我写了一行代码来上传数据库中的图像,但是,尝试上传图像会给我这个错误

  

文件名太长

以下是我将代码上传到数据库的代码:

if($_SERVER['REQUEST_METHOD']=="POST")
    {
      $pid          = rand(1000,9000);
      $title        = $_POST['title'];
      $descpt       = $_POST['description'];
      $push         = isset($_POST['send_push']) ? $_POST['send_push'] : "";
      $feature_image = array();
      $fy           = $_POST['fy'];

      if(empty($title) || empty($descpt) || empty($fy))
      {
          array_push($this->errors, MEND_FIELD_ERROR);
          return;
      }

      if(!empty($_FILES['feature_image']['name'][0]))
      {
          $image = $_FILES['feature_image'];
          $allowed_ext = array('jpeg','jpg','png','pdf','docx');
          $allowed_size = 20000000;

          foreach($image['name'] as $pos=>$image_name)
          {
              $dir = "./cdn/uploads/notice/".$title;      
              $tmp = $image['tmp_name'][$pos];
              $img_size = $image['size'][$pos];
              $img_error = $image['error'][$pos];
              $img_ext = explode('.', $image_name);
              $img_name = $img_ext[0];
              $img_ext = strtolower(end($img_ext));


              if(in_array($img_ext, $allowed_ext))
              {
                  if($img_size <= $allowed_size)
                  {
                    if(!file_exists($dir))
                    {
                        mkdir($dir);
                    }
                    $image_new_name = $img_name.'$$'.uniqid('', true).'.'.$img_ext;

                    $upload_destination = $dir.'/'.$image_new_name;
                    if(move_uploaded_file($tmp, $upload_destination))
                    {
                        array_push($feature_image, $image_new_name);
                    }
                    else
                    {
                        array_push($this->errors, $img_error);
                        return;
                    }

                  }
              }
              else
              {
                array_push($this->errors, $img_ext.' is not an allowed file extension.');
                return;
              }
          }
      }

      $s_feature_image = json_encode($feature_image, JSON_UNESCAPED_UNICODE);

      $statement = $this->db->prepare("INSERT INTO `notice` (`pid`,`title`,`descpt`,`date`,`photo`,`fy`)
      VALUES (?,?,?,?,?,?)");         
      if($statement->execute([$pid,$title,$descpt,DAT, $s_feature_image, $fy]))
      {
        if($push == "checked")
        {

            $descpt = strip_tags($descpt);
            $tek = array("message"=>$descpt,"title"=>$title);

            $tokens = $this->getTokens();
            $this->push_notification($tokens,$tek);

        }
        ExitThis::send_to(URL.'notice?id='.$pid);

      }
      else
      {
          array_push($this->errors, DATABASE_ERROR);
          return;
      }
    }

是因为权限问题还是其他原因?如果是这样,是什么导致我这个问题,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:-1)

这是我将文件上传到服务器并将文件名和扩展名保存到数据库中的方式。

<?php

include 'connection.php';

$id = $_POST['id'];
$imgFile = $_FILES['photo']['name'];
$tmp_dir = $_FILES['photo']['tmp_name'];
$imgSize = $_FILES['photo']['size'];

$folder = 'images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$img = rand(1000, 1000000) . "." . $imgExt;
// allow valid image file formats
if (in_array($imgExt, $valid_extensions)) {
    // Check file size '5MB'
    if ($imgSize < 5000000) {
        move_uploaded_file($tmp_dir, $folder . $img);
    } else {
        $errMSG = "Sorry, your file is too large.";
    }
} else {
    $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}

$query = mysqli_query($con, "UPDATE `profile` SET `photo` = '$img' WHERE `id` = '$id'");

if ($query) {
	echo "<script>alert('Profile Updated'); window.location ='index.php?data=profile' </script>";
} else {
    echo "<script>alert('Failed'); window.location ='index.php?data=profile' </script>";
}

?>

希望这会有所帮助。 干杯。