0

I have written a line of codes to upload an image in the database, however, trying to upload image gives me this error

File name too long

Following is my code to upload an image to database:

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;
      }
    }

Is it because of permission issue or something else? If so, what is causing me this problem and how do I fix this?

1 Answers1

-1

this is how I upload the file into the server and save the file name + extension into the database.

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

?>

Hope this helps. Cheers.

M Ansyori
  • 429
  • 6
  • 21
  • I think you are missing the point. Read OP's question again and check the comments. – Rotimi Feb 07 '19 at 08:50
  • You are open to SQL injections with this code and possibly even more. Whilst the temp file won't be uploaded due to failing validation, you still insert it into the database, both `$imgExt` and `$id` are vulnerable. You should never trust client data – Second2None Feb 07 '19 at 08:53