Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to upload multiple image but getting error "failed to upload image"

help me asap

What I have tried:

<?php
include('../pages/connect.php');
require_once('../doctorlogin/resize_image.php');

class Upload {
  
    function __construct() {

    }

  function upload_img(){
    if(isset($_POST['submit'])){
      global $mysqli;
      $msg = '';  
      $max_upload_size = (int)(ini_get('upload_max_filesize'));
      if (count($_FILES["img_files"]) > 0) {
        $folderName = "uploads/";

        for ($i = 0; $i < count($_FILES["img_files"]["name"]); $i++) {
          $file_name = $_FILES["img_files"]["name"][$i];
          $file_type = $_FILES["img_files"]["type"][$i];
          $file_size = $_FILES["img_files"]["size"][$i];
          if( isset($file_name) && $file_name != "") {
            //if($this->file_size($file_size, $max_upload_size)){
              if($this->file_extension($file_type)){
                $filename = time() . '_' . $file_name;
                $filepath = $folderName . $filename;
                if (!move_uploaded_file($_FILES["img_files"]["tmp_name"][$i], $filepath)) {
                  $msg .= "<p class='msg_error'>Failed to upload " . $filename . ".</p>";
                } else {
                  $sql = "INSERT INTO upload_img (img_name) VALUES ('$file_name') ";
                  $result = $mysqli->query($sql);
                  $msg .= "<p class='msg_success'>" . $filename . " uploaded successfully.</p>";

                  $magicianObj = new imageLib($filepath);
                  $magicianObj->resizeImage(100, 100);
                  $magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);
                }
              }else{
                $msg .= "<p class='msg_error'>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
              }
            //}else{
            //  $msg .= "<p class='msg_error'>Uploaded image size is too large, please upload image less then ".intval($max_upload_size)."Mb.</p>";
            //}
          }
        }
      } else {
        $msg .= "<p class='msg_error'>Please upload atleast one image file.</p>";
      }
    }
    return $msg;
  }

  function file_extension($filetype,$type=array()){
    $ext_arr = array( 'jpg' => 'image/jpeg',
                       'png' => 'image/png',
                       'gif' => 'image/gif'
                       );
    if(!empty($type)){
        $ext_arr = array_merge($ext_arr,$type);
    }
    $return =false;
    // Allow certain file formats
    if(array_search($filetype, $ext_arr)) {
        $return =true;
    }
    return $return;
  }

  function file_size($filesize, $max_upload_size){
      // Check file size
      $return = false;
      if ($filesize < $max_upload_size*1000000) {
        $return = true;
      }
      return $return;
    }

}


Getting error failed to upload
Posted
Updated 11-Apr-17 22:57pm
Comments
Richard MacCutchan 12-Apr-17 4:20am    
You need to do some debugging to find out what is causing the failure.

1 solution

You should check for errors first and report them to know what happened:
$file_err = $_FILES["img_files"]["error"][$i];
if($file_err != UPLOAD_ERR_OK){
    $msg .= "File upload failed. Code " . $_FILES["img_files"]["error"][$i];
}
See also PHP: Handling file uploads - Manual[^].

If move_uploaded_file() fails even when there was no upload error a warning is issued. Because you did not got such warning, it seems that the file upload was not successful.

Otherwise check the destination file name:
Does the directory exist, has the script (account running the web server) write access to the directory, and is $filename a valid file name (does not contain reserved characters)?
Note also that $folderName must have a trailing slash when used here:
PHP
$filepath = $folderName . $filename;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900