0

I am not using drupal as front end. Instead I am using drupal as backend. I want to upload a file into drupal files folder. Normal upload.php which we use is giving me an undefined $file error. I am trying using AJAx POST request. In normal php it worked for me. I used the same php function inside drupal 8 controller. It is giving me an $file undefined error eventhough i used drupal/entitity/file.

<!DOCTYPE html>
        <html>
        <body>

        <p class="statusMsg"></p>
        <form enctype="multipart/form-data" id="fupForm" >
           <input id="sortpicture" type="file" name="sortpic" />
           <button id="upload">Upload</button>
        </form>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
        $('#upload').on('click', function() {
            var file_data = $('#sortpicture').prop('files')[0];   
            var form_data = new FormData();                  
            form_data.append('file', file_data);
            alert(form_data);                             
            $.ajax({
                url: 'http://localhost/firstu_dr/web/file/upload', // point to server-side PHP script 
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(php_script_response){
                    alert(php_script_response); // display response from the PHP script, if any
                },
                        error: function(xhr, status, error) {
                  var err = eval("(" + xhr.responseText + ")");
                  alert(err.Message);
                }
             });
        });
        </script>

        </body>

My controller code :-

namespace Drupal\file_upload\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\file\Entity;



class FileUpdateController extends ControllerBase {
  public function file_upload(Request $request) {


      if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    }
    return ['#markup' => 'file upload logics'];
  }

  }

The error:-

Notice: Undefined index: file in Drupal\file_upload\Controller\FileUpdateController->file_upload() (line 18 of modules/custom/file_upload/src/Controller/FileUpdateController.php).

  • 1
    Have you inspected the contents of $request->files? – Clive Jan 14 at 10:55
  • no i didnt tried... will check and update now – Rifas Ali Jan 15 at 4:51
  • getting an error Recoverable fatal error: Object of class Symfony\Component\HttpFoundation\FileBag could not be converted to string in Drupal\file_upload\Controller\FileUpdateController->file_upload() (line 18 of modules/custom/file_upload/src/Controller/FileUpdateController.php). – Rifas Ali Jan 15 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.