0

I am building admin backend for my blog and I am trying to upload file with the code below, But I am getting 500 Internal Server Error nginx/1.13.2. I have check nginx_error log and it says ailed (13: Permission denied), client: 127.0.0.1, server: , request: "POST. I know it is something to do with permission from the error log but how to I allow the permission. I have search and couldn't find any good answer that I can apply. thanks all. Below is my php code

     $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

      $expensions= array("jpeg","jpg","png");

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }

      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }

Thanks for you time and help :)

Samuel Sam
  • 47
  • 7
  • Can you `cd` to your upload directy with your Nginx user (may be www-data) ? If you can't `cd` to that directory, you'll have to grant nginx user to access that folder. You may take a look at this post [link](https://stackoverflow.com/questions/25774999/nginx-stat-failed-13-permission-denied/25776092#25776092) to know how to do. – S. Bureau Sep 19 '18 at 12:49
  • @ S. Bureau. Yes I am able to cd from terminal to the upload directory. – Samuel Sam Sep 20 '18 at 12:38
  • Humm, can you tell which user nginx is running with (you should find the information in [Nginx.conf](http://nginx.org/en/docs/ngx_core_module.html#user) ? And could you share the rights on your local folder ? This could help. – S. Bureau Sep 20 '18 at 12:47
  • it is .. user sam staff; how can I share the right on the folder. I am new to this – Samuel Sam Sep 20 '18 at 13:01
  • Could you share the rights of the folder you're trying to upload your image to ? Btw, in order to go on, you can create a `try{move_uploaded_file(...)} catch{$e}` block to get more precisions over the internal error. Finally, I would try to upload the image in another folder with absolute path (with all rights for your current user). – S. Bureau Sep 20 '18 at 13:09

0 Answers0