CodeIgniter Forums
Image upload Problem - image not uploading - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Image upload Problem - image not uploading (/showthread.php?tid=70702)



Image upload Problem - image not uploading - conxeptbox - 05-17-2018

Hi,

I am new to this Forum. We have a new client for maintenance and while shifting their server, there are some problem in the upload functions. On the old server application works fine.

Here is the code which have been used for:

Code:
view:index.php:

$('#upload-button').click(function(e){
      e.preventDefault();
      var name = $(".fileinput-filename1").html();
      $('#multiple_upload_form').ajaxForm({
          beforeSubmit:function(e){
              $('.uploading').show();
          },
          success:function(e){
              $('.uploading').hide();
              $('.fileinput .close').click();
              $('#upload-alert').show();
              $('#upload-alert').removeClass('alert-error').addClass('alert-success');
              $('#upload-alert p').html('<strong>Success!</strong> File has been succesfully upload.');
              dataTable.draw();
          },
          error:function(e){
              $('#upload-alert').show();
              $('#upload-alert').removeClass('alert-success').addClass('alert-error');
              $('#upload-alert p').html('<strong>Error!</strong> Something Wrong.');
              alert(e);
          }
      }).submit();
  });


Assets.php (controller):

 <?php namespace App\Controllers\Admin;

use CodeIgniter\Controller;
use App\Models\Admin\AssetModel;
use App\Models\Admin\AssetCategoryModel;

class Asset extends Controller
{
    public function index()
    {
          isProtected();

        if(isPostBack()){

               $model = new AssetModel();

               $filters['name']    = $this->request->getPost('name'); 
               $filters['category']    = $this->request->getPost('category');  

               $order = $this->request->getPost('order');

               $start    = $this->request->getPost('start'); 
               $length    = $this->request->getPost('length');

               $list = $model->getList($filters,$order,$start, $length);
               $totalResults = $model->getTotalResults($filters);
             
               $requestData= $_REQUEST;
               $json_data = array(
                 "draw"            => intval($requestData['draw']),
                 "recordsFiltered" => $totalResults,
                 "recordsTotal"    => $totalResults,
                 "data"            => $list
                 );
               echo json_encode($json_data);exit();
          }
          else
          {
               $model = new AssetCategoryModel();
               $data['categories'] = $model->getList();
               return admin_render("asset/index",$data);
          }
    }

     public function popup($fieldName='asset_upload')
    {
          isProtected();

        if(isPostBack()){

               $model = new AssetModel();

               $filters['name']    = $this->request->getPost('name'); 
               $filters['category']    = $this->request->getPost('category');  

               $order = $this->request->getPost('order');

               $start    = $this->request->getPost('start'); 
               $length    = $this->request->getPost('length');

               $list = $model->getList($filters,$order,$start, $length);
               $totalResults = $model->getTotalResults($filters);
             
               $requestData= $_REQUEST;
               $json_data = array(
                 "draw"            => intval($requestData['draw']),
                 "recordsFiltered" => $totalResults,
                 "recordsTotal"    => $totalResults,
                 "data"            => $list
                 );
               echo json_encode($json_data);exit();
          }
          else
          {
               $model = new AssetCategoryModel();
               $data['categories'] = $model->getList();
               $data['fieldName']  = $fieldName;
               return view("backend/asset/popup",$data);
          }
    }

     public function uploadImage()
    {
          isProtected();
    
        if(isPostBack()){
               $model = new AssetModel();

               $files    = $this->request->getFiles();
               $category = $this->request->getPost('category');
               $model->fileUpload($category,$files->all()['images']);  
          }
    }


     public function edit($id)
    {
          isProtected();

          $model = new AssetCategoryModel();
          $data['categories'] = $model->getList();

          $model = new AssetModel();
          $data['asset'] = $model->find($id);
          //print_array($data['categories']);exit();
    
          $rules = [
                       'name' => 'required|min_length[3]',
                   ];

          if(isPostBack()){
              $data["name"] = $this->request->getPost('name'); 
              $data["asset_category_id"] = $this->request->getPost('category');
              $data["updated_at"] = current_date();

              if($this->validate($rules))
              {
                   if($model->update($id,$data)){
                        $data['asset']['asset_category_id'] = $data["asset_category_id"];
                        set_flashdata("success","Asset Category has been succesfully saved!");
                        //redirect(site_url("admin/asset/categories"));
                   }
                   else
                        set_flashdata("error","Something Wrong!");
              }
          }
          $data["flashdata"]  = get_flashdata();
         
          $data['validation'] = $validation = \Config\Services::validation();
          $data['errors']      = $validation->getErrors();

          return admin_render("asset/edit",$data);
    }

     public function delete($id)
    {
          isProtected();

          if(isPostBack()){
              $model = new AssetModel();
              $data = $model->find($id);
              if($data)
              {
                    if(file_exists(upload_path($data['image'])))
                         unlink(upload_path($data['image']));
                   
                    $model->delete($id);
                    return $this->response->setStatusCode(200, 'OK');
               }
          }
          return $this->response->setStatusCode(404, 'Nope. Not here.');
    }
    

     public function categories()
    {
          isProtected();
          if(isPostBack()){

               $model = new AssetCategoryModel();

               $filters['name']    = $this->request->getPost('name');  

               $start    = $this->request->getPost('start'); 
               $length    = $this->request->getPost('length');

               $list = $model->getList($filters,NULL,$start, $length);
               $totalResults = $model->getTotalResults($filters);
             
               $requestData= $_REQUEST;
               $json_data = array(
                 "draw"            => intval($requestData['draw']),
                 "recordsFiltered" => $totalResults,
                 "recordsTotal"    => $totalResults,
                 "data"            => $list
                 );
               echo json_encode($json_data);exit();
          }
          else
          {
               return admin_render("asset/cat-index");
          }
    }

     public function catAdd()
    {
          isProtected();

          $rules = [
                       'name' => 'required|min_length[3]',
                   ];

          if(isPostBack()){
              $data["name"] = $this->request->getPost('name');  
              $data["description"] = $this->request->getPost('description');
              $data["created_at"] = current_date();

              if($this->validate($rules))
              {
                   $model = new AssetCategoryModel();
                   $model->save($data);
                   set_flashdata("success","Asset Category has been succesfully created!");
                   redirect(site_url("admin/asset/categories"));
              }
          }
         
          $data["flashdata"]  = get_flashdata();
         
          $data['validation'] = $validation = \Config\Services::validation();
          $data['errors']      = $validation->getErrors();

          return admin_render("asset/cat-add",$data);
    }

     public function catEdit($id)
    {
          isProtected();

          $model = new AssetCategoryModel();
          $data['cat'] = $model->find($id);
          //print_array($data['cat']);exit();
    
          $rules = [
                       'name' => 'required|min_length[3]',
                   ];

          if(isPostBack()){
              $data["name"] = $this->request->getPost('name');  
              $data["description"] = $this->request->getPost('description');
              $data["updated_at"] = current_date();

              if($this->validate($rules))
              {
                   if($model->update($id,$data)){
                        set_flashdata("success","Asset Category has been succesfully saved!");
                        //redirect(site_url("admin/asset/categories"));
                   }
                   else
                        set_flashdata("error","Something Wrong!");
              }
          }
          $data["flashdata"]  = get_flashdata();
         
          $data['validation'] = $validation = \Config\Services::validation();
          $data['errors']      = $validation->getErrors();

          return admin_render("asset/cat-edit",$data);
    }
     public function catDelete($id)
    {
          isProtected();

          if(isPostBack()){
              $model = new AssetCategoryModel();
              $data = $model->find($id);
              if($data)
              {
                    $model->delete($id);
                    return $this->response->setStatusCode(200, 'OK');
               }
          }
          return $this->response->setStatusCode(404, 'Nope. Not here.');
    }

}

Could somebody please help me to resolve the issue. No error code shown, only uploading error message shown on the page. I have checked php version, server configuration as well. everything is fine. I have attached screenshot of the error.

Thanks in advance.


RE: Image upload Problem - image not uploading - php_rocs - 05-17-2018

@conxeptbox,

Did you check to make sure that the directory where the upload is going has the correct permissions (on the new server)?


RE: Image upload Problem - image not uploading - conxeptbox - 05-17-2018

(05-17-2018, 10:55 AM)php_rocs Wrote: @conxeptbox,

Did you check to make sure that the directory where the upload is going has the correct permissions (on the new server)?

Thank you for your quick response. I checked all permissions are fine. All directories and sub-directories have write permission. Is there any issue possible with the server?

Thanks again for quick help!


RE: Image upload Problem - image not uploading - InsiteFX - 05-18-2018

Also check your php.ini file.

memory_limit = 32M

these two below should always be the same size.

upload_max_filesize = 32M
post_max_size = 32M

These setting's can effect any file upload type's.


RE: Image upload Problem - image not uploading - conxeptbox - 05-19-2018

(05-18-2018, 03:27 AM)InsiteFX Wrote: Also check your php.ini file.

memory_limit = 32M

these two below should always be the same size.

upload_max_filesize = 32M
post_max_size = 32M

These setting's can effect any file upload type's.

Hi InsiteFX,

Thanks for your response. This was not set as you defined, but after changing the values, we still facing the same issue. Is there any other possible issue which help uploading the assets files to the server.

We have checked each and every bit of code, nothing suspicious. Maybe we missed something.

Thanks again for quick help.

Conxeptbox


RE: Image upload Problem - image not uploading - scalla - 05-19-2018

(05-19-2018, 03:57 AM)It might be better to do a var_dump to check the return response ,that would help to figure out what\s wrong conxeptbox Wrote:
(05-18-2018, 03:27 AM)InsiteFX Wrote: Also check your php.ini file.

memory_limit = 32M

these two below should always be the same size.

upload_max_filesize = 32M
post_max_size = 32M

These setting's can effect any file upload type's.

Hi InsiteFX,

Thanks for your response. This was not set as you defined, but after changing the values, we still facing the same issue. Is there any other possible issue which help uploading the assets files to the server.

We have checked each and every bit of code, nothing suspicious. Maybe we missed something.

Thanks again for quick help.

Conxeptbox



RE: Image upload Problem - image not uploading - rajeshgoyalg - 05-19-2018

Check mime-type of the uploaded file on the server and set that mime-type in config -> mimes.php.


RE: Image upload Problem - image not uploading - InsiteFX - 05-19-2018

You would need to change,

upload_max_filesize = 32M
post_max_size = 32M

To the largest size of the files you are uploading.
See what the file sizes are then set them up.


RE: Image upload Problem - image not uploading - conxeptbox - 05-21-2018

(05-19-2018, 05:02 AM)scalla Wrote:
(05-19-2018, 03:57 AM)It might be better to do a var_dump to check the return response ,that would help to figure out what\s wrong conxeptbox Wrote:
(05-18-2018, 03:27 AM)InsiteFX Wrote: Also check your php.ini file.

memory_limit = 32M

these two below should always be the same size.

upload_max_filesize = 32M
post_max_size = 32M

These setting's can effect any file upload type's.

Hi InsiteFX,

Thanks for your response. This was not set as you defined, but after changing the values, we still facing the same issue. Is there any other possible issue which help uploading the assets files to the server.

We have checked each and every bit of code, nothing suspicious. Maybe we missed something.

Thanks again for quick help.

Conxeptbox

Thanks again for your response. I am getting the error in javascript written in view file, so i put 'console.log(e);'  instead of var_dump(); Here is the output of console.log(e); :-


  1. {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}

    1. abort:ƒ (a)
      1. arguments:null
      2. caller:null
      3. length:1
      4. name:"abort"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:4
      8. [[Scopes]]:Scopes[3]
    2. always:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"always"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    3. complete:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"add"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    4. done:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"add"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    5. error:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"add"
      5. prototype:
        1. constructor:ƒ ()
        2. __proto__:Object
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    6. fail:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"add"
      5. prototype:
        1. constructor:ƒ ()
          1. arguments:null
          2. caller:null
          3. length:0
          4. name:"add"
          5. prototype:{constructorƒ}
          6. __proto__:ƒ ()
          7. [[FunctionLocation]]:jquery.min.js:2
          8. [[Scopes]]:Scopes[3]
        2. __proto__:Object
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    7. getAllResponseHeaders:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"getAllResponseHeaders"
      5. prototype:
        1. constructor:ƒ ()
        2. __proto__:Object
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:4
      8. [[Scopes]]:Scopes[3]
    8. getResponseHeader:ƒ (a)
      1. arguments:null
      2. caller:null
      3. length:1
      4. name:"getResponseHeader"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:4
      8. [[Scopes]]:Scopes[3]
    9. overrideMimeType:ƒ (a)
      1. arguments:null
      2. caller:null
      3. length:1
      4. name:"overrideMimeType"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:4
      8. [[Scopes]]:Scopes[3]
    10. pipe:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"then"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    11. progress:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"add"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    12. promise:ƒ (a)
      1. arguments:null
      2. caller:null
      3. length:1
      4. name:"promise"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    13. readyState:4
    14. responseText:"<!doctype html>↵<html>↵<head>↵ <meta charset="UTF-8">↵ <meta name="robots" content="noindex">↵↵ <title>Whoops!</title>↵↵ <style type="text/css">↵ body { height: 100%; background: #fafafa; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: #777; font-weight: 300; margin: 0; padding: 0; } h1 { font-weight: lighter; letter-spacing: 0.8; font-size: 3rem; color: #222; margin: 0; } h1.headline { margin-top: 20%; font-size: 5rem; } .text-center { text-align: center; } p.lead { font-size: 1.6rem; } .container { max-width: 75rem; margin: 0 auto; padding: 1rem; } .header { background: #85271f; color: #fff; } .header h1 { color: #fff; } .header p { font-size: 1.2rem; margin: 0; line-height: 2.5; } .header a { color: rgba(255,255,255,0.5); margin-left: 2rem; display: none; text-decoration: none; } .header:hover a { display: inline; } .footer .container { border-top: 1px solid #e7e7e7; margin-top: 1rem; text-align: center; } .source { background: #333; color: #c7c7c7; padding: 0.5em 1em; border-radius: 5px; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; margin: 0; } .source span.line { line-height: 1.4; } .source span.line .number { color: #666; } .source .line .highlight { display: block; background: #555; color: #fff; } .source span.highlight .number { color: #fff; } .tabs { list-style: none; list-style-position: inside; margin: 0; padding: 0; margin-bottom: -1px; } .tabs li { display: inline; } .tabs a:link, .tabs a:visited { padding: 0rem 1rem; line-height: 2.7; text-decoration: none; color: #a7a7a7; background: #f1f1f1; border: 1px solid #e7e7e7; border-bottom: 0; border-top-left-radius: 5px; border-top-right-radius: 5px; display: inline-block; } .tabs a:hover { background: #e7e7e7; border-color: #e1e1e1; } .tabs a.active { background: #fff; } .tab-content { background: #fff; border: 1px solid #efefef; } .content { padding: 1rem; } .hide { display: none; } .alert { margin-top: 2rem; display: block; text-align: center; line-height: 3.0; background: #d9edf7; border: 1px solid #bcdff1; border-radius: 5px; color: #31708f; } ul, ol { line-height: 1.8; } table { width: 100%; overflow: hidden; } th { text-align: left; border-bottom: 1px solid #e7e7e7; padding-bottom: 0.5rem; } td { padding: 0.2rem 0.5rem 0.2rem 0; } tr:hover td { background: #f1f1f1; } td pre { white-space: pre-wrap; } .trace a { color: inherit; } .trace table { width: auto; } .trace tr td:first-child { min-width: 5em; font-weight: bold; } .trace td { background: #e7e7e7; padding: 0 1rem; } .trace td pre { margin: 0; } .args { display: none; } </style>↵</head>↵<body>↵↵ <div class="container text-center">↵↵ <h1 class="headline">Whoops!</h1>↵↵ <p class="lead">We seem to have hit a snag. Please try again later...</p>↵↵ </div>↵↵</body>↵↵</html>↵"
    15. setRequestHeader:ƒ (a,b)
      1. arguments:null
      2. caller:null
      3. length:2
      4. name:"setRequestHeader"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:4
      8. [[Scopes]]:Scopes[3]
    16. state:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"state"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    17. status:500
    18. statusCode:ƒ (a)
      1. arguments:null
      2. caller:null
      3. length:1
      4. name:"statusCode"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:4
      8. [[Scopes]]:Scopes[3]
    19. statusText:"Internal Server Error"
    20. success:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"add"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    21. then:ƒ ()
      1. arguments:null
      2. caller:null
      3. length:0
      4. name:"then"
      5. prototype:{constructorƒ}
      6. __proto__:ƒ ()
      7. [[FunctionLocation]]:jquery.min.js:2
      8. [[Scopes]]:Scopes[3]
    22. __proto__:
      1. constructor:ƒ Object()
      2. hasOwnProperty:ƒ hasOwnProperty()
      3. isPrototypeOf:ƒ isPrototypeOf()
      4. propertyIsEnumerable:ƒ propertyIsEnumerable()
      5. toLocaleString:ƒ toLocaleString()
      6. toString:ƒ toString()
      7. valueOf:ƒ valueOf()
      8. __defineGetter__:ƒ __defineGetter__()
      9. __defineSetter__:ƒ __defineSetter__()
      10. __lookupGetter__:ƒ __lookupGetter__()
      11. __lookupSetter__:ƒ __lookupSetter__()
      12. get __proto__:ƒ __proto__()
      13. set __proto__:ƒ __proto__()




conxeptbox.


RE: Image upload Problem - image not uploading - InsiteFX - 05-21-2018

Did you try to set and send the Json Headers in your Controller?