如何在PHP中获取表单图片数据

问题描述 投票:0回答:1

这是我的HTML,用于发布带图片的广告。

<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>

这是我的轮廓仪功能

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

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

这是我的网址

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

这就是输出

Notice: Undefined index: image in F:\Xento_OLX_API's\Application\Controllers\OLX\COlxUserController.php on line 171
NULL
php html twig
1个回答
1
投票

你的HTML中没有表单声明标签。如果你想把表单发送到服务器上,如果你要把文件发送到服务器上,你应该使用适当的表单标签和enctype。

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

你可以了解更多关于HTML表格的信息 此处

如果你使用framework,你必须遵循文档,因为你将无法访问全局的$_FILES。

薄型框架

$uploadedFiles = $request->getUploadedFiles();

Laravel框架

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

0
投票

试试这个既能保存又能获取图像数据。

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>
© www.soinside.com 2019 - 2024. All rights reserved.