Rohit sharma
Rohit sharma

Reputation: 41

How to get form image data in PHP

This is my HTML to post an ad with image

<form id="product-form" name="myForm" action="{{ path_for( 'insert',{'user_id': userId} ) }}" method="GET" >
<table>
 <tr>
  {% for count in 1..10 %} 
   <td id="form-image{{loop.index}}" class="form-input-image">
     <input type="radio" name="cover-image" title="Cover Image" id="cover-image{{loop.index}}" class="cover-image" value="{{loop.index}}">
     <label for="file{{loop.index}}"> <img src="/OLX/Views/images/add_image.png" id="image{{loop.index}}" ></label>
     <input type="file"  accept="image/*" name="image" id="file{{loop.index}}" onchange="change({{loop.index}},event)" style="display: none;">
   </td>
  {% endfor %}
 </tr>
</table>
</form>

This is my Function of contoller

public function insertProduct( Request $request, Response $response, $intUserId ) {
        $arrmixInputData = $request->getParams();
        $intCoverImageNumber = $arrmixInputData["cover-image"];

             $filename = $_FILES['image'];
             var_dump($filename);
             die();
}

This is my URL

http://localhost:8888/Products/insertProduct/2?title=Title&category_id=1&price=12345&purchase_date=2020-04-25&description=SAssds&cover-image=1&image=WIN_20190705_18_22_39_Pro.jpg

This is the output

Notice: Undefined index: image in F:\Xento_OLX_API's\Application\Controllers\OLX\COlxUserController.php on line 171
NULL

Upvotes: 0

Views: 2105

Answers (2)

Gabriel
Gabriel

Reputation: 970

You do not have form declaration tag in your HTML. If you want to send form to server / If you have to send file to server you should use proper form tag and enctype

<form action="/action_page_binary.asp" method="post" enctype="multipart/form-data">
....... all your input tags here.
</form>

You can learn more about HTML Form here

If you use framework you must follow the documentation as you will not have access to global $_FILES

Slim Framework

$uploadedFiles = $request->getUploadedFiles();

Laravel Framework

$uploadedFiles = $request->file('photo');

Upvotes: 2

PHP Geek
PHP Geek

Reputation: 4033

Try this one to save and get image data both.

var image_uploader = new plupload.Uploader({
        runtimes: 'html5,flash,html4',
        browse_button: "image_uploader",
        container: "image_upload_container",
        url: base_url + 'Blog/upload_files',
        chunk_size: '1mb',
        unique_names: true,
        multi_selection: false,
        resize: {
            width: 300,
            height: 300
        },
        flash_swf_url: base_url + 'assets/js/plupload/js/Moxie.swf',
        silverlight_xap_url: base_url + 'assets/js/plupload/js/Moxie.xap',
        filters: {
            max_file_size: '10mb',
            mime_types: [
                {title: "Image files", extensions: "jpg,jpeg,png,pdf"}
            ]
        },
        init: {
            FilesAdded: function (up, files) {
                setTimeout(function () {
                    up.start();
                }, 1);
            },
            FileUploaded: function (up, file) {
                $("#image_div").html('<div class="form-group col-sm-6 col-md-offset-3 text-right">\n\
     <a href="javascript:;" onclick="remove_photo2()">\n\
<i class="fa fa-2x fa-times-circle"></i></a>\n\
<div class="form-group text-left"><input type="hidden" name="image" value="' + file.target_name + '">\n\
<img alt="" class="img img-responsive" src="' + base_url + 'uploads/' + file.target_name + '" height="100" width="100"/></div></div>');
                $("#image").val(file.target_name);
            },
        },
        UploadComplete: function () {
//            $('#image_div').unblock();
        },
        Error: function (up, err) {
//            $('#image_div').unblock();
            bootbox.alert(err.message);
        }

    });
    image_uploader.init();
    function remove_photo2(id) {
        $("#image_div").empty();
    }
//php

<!-- begin snippet: js hide: false console: true babel: false -->

<div class="fileinput fileinput-new row mt-3" data-provides="fileinput" >
                                    <div id="og_image_div">
                                        <div class="form-group">
                                            <?php if (isset($blog_data['og_image'])) { ?>
                                                <input class="form" type="hidden" name="og_image" id="og_image" value="<?php echo isset($blog_data['og_image']) ? $blog_data['og_image'] : ''; ?>">
                                                <img src="<?php
                                                if ($blog_data['og_image'] != '' && file_exists(FCPATH . 'uploads/blog/' . $blog_data['og_image'])) {

                                                    echo base_url('uploads/blog/' . $blog_data['og_image']);
                                                } else {

                                                    echo base_url() . '';
                                                }
                                                ?>" height="70px" width="100px"/>
                                                 <?php } ?>
<!--<input class="form" type="hidden" name="image" id="image">-->
                                        </div>
                                    </div>
                                </div>

Upvotes: 0

Related Questions