phpformsinputfile-uploadsymfony6

How to upload image but NOT using form builder in Symfony 6?


I know there is a simple way to upload image in Symfony 6. In short - create form via form builder and then in the controller we just simply do:

$form = $this->createForm(MyClassType::class);

After form is submitted and valid we can get the image and (if we want to) move it:

if ($image = $form['img']->getData()) {
    $fileName = uniqid().'.'.$image->guessExtension();
    $image->move($imageDir, $fileName);
}

But in my app I can't do that. I've made plain form in html with input type=file. And I don't know how to access/move uploaded image that way. I can get only name of the file, not actuall image.

In my controller I created new object (which is in my model):

$uploadedData = new MyData(); $allData = $uploadedData->getMyData();

Where in method 'getMyData()' I'm checking which inputs (image is not required) have been filled:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_FILES['fileName'])) {
              $image = $_FILES['fileName'];
    } else $image = "";
}

and return outcome (submitted data) as an array.

if I check result in the controller via dd($allData); I get (only showing image info):

"image" => array:6 [▼
    "name" => "test.jpg"
    "full_path" => "test.jpg"
    "type" => "image/jpeg"
    "tmp_name" => "C:\xampp\tmp\php89C0.tmp"
    "error" => 0
    "size" => 3909421
  ]

So it seems like all went ok, right? But I can't access this image (or move it to the folder where it should be). I tried access this image doing this ($imageDir is just a path to the desired folder):

if ($image = $allData['image']['name']) {
    $fileName = uniqid().'.'.$image->guessExtension();
    $image->move($imageDir, $fileName);
} else $fileName = null;

I got error: Call to a member function guessExtension() on string. I tried all of the keys. I got error every time. Even tried that: $image = $allData['image']['name']->getData() Got error: Call to a member function getData() on string. Other combinations also failed.

In other words - seems like image was uploaded (if that's the case I wonder where it is...) but I can't access or move it. Method getData() seems working only when we create form via $form = $this->createForm(MyClassType::class);. But as I stated before - I can't create form that way here. If someone was wondering - my html form has needed attributes (method="POST" enctype="multipart/form-data").

Any ideas?


Solution

  • $allData['image']['name'] is a string. In your if statement, you assign that string to $image.

    if ($image = $allData['image']['name']) {
    

    You need to fix your if statement. This code should move your file:

    $uploadsDir = /path/to/uploads';
    $tempName = $allData['image']['tmp_name'];
    $name = basename($allData['image']['name']);
    move_uploaded_file($tempName, "$uploadsDir/$name");
    

    See the docs for move_uploaded_file here https://www.php.net/manual/en/function.move-uploaded-file

    If you need to do any manipulation to the image such as resizing or cropping etc you can try my image composer package here https://github.com/delboy1978uk/image