Search code examples
phpformsfileobjecthidden

how to post an entire object hidden in a form in php


I have an object which is a file in origin and i would like to send this object in a post form.

When i try this :

{{ Form::open(array('url' => 'mgmtUser/upload_img_crop/', 'method' => 'POST', 'files' => true)); }}
    <input hidden type="file" name="file" value="<?php $file; ?>">
    <?= Form::hidden('x', '', array('id' => 'x')) ?>
    <?= Form::hidden('y', '', array('id' => 'y')) ?>
    <?= Form::hidden('w', '', array('id' => 'w')) ?>
    <?= Form::hidden('h', '', array('id' => 'h')) ?>
    <?= Form::submit('Crop it!') ?>
    <?= Form::close() ?>

The post result is only : ["file"]=> NULL

Why and how I can get all the object, should I encode, serialize...? I am on laravel 4. $file is :

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(11) "3bb13ad.jpg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(8017) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(14) "/tmp/phpVGS9Cu" ["fileName":"SplFileInfo":private]=> string(9) "phpVGS9Cu" }


Solution

  • If you are actually trying to send the $file Object through the form, you'll need to serialize it into some file format (e.g. write to a .txt file) and then upload that through the input.

    It looks like the $file Object contains attributes related to a file you may be trying to upload instead though, not the actual PHP file handle object. If this is the case, you should just change the below line

    <input hidden type="file" name="file" value="<?php $file; ?>">
    

    To be

    <input hidden type="file" name="file" value="<?php $file.originalName; ?>">
    

    EDIT

    If you need to work with the file handle on after the form submit, try this:

    <input hidden type="file" name="file" value="<?php echo json_encode($file) ?>">
    

    Then when you are on the receiving end, use

    $file = json_decode($_POST['file']); 
    

    to access the Object.