图片未通过 PHP 上传到 mySql

Posted

技术标签:

【中文标题】图片未通过 PHP 上传到 mySql【英文标题】:image not uploading to mySql via Php 【发布时间】:2020-08-07 09:53:39 【问题描述】:

这个功能,看起来很标准,我真的没有一个活动按钮来上传到我的数据库中的列表?

if(isset($_POST['submit'])) //THis is how you upload a picture to a website
    $file = $_FILES['file'];
    print_r($file);

    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    //array for
    $allowed = array('jpg', 'jpeg', 'png', 'pdf');

    if(in_array($fileActualExt, $allowed))
        if($fileError === 0) 
            if($fileSize < 1000000) 
                $fileNameNew = uniqid ('',true). ".". $fileActualExt;
                $fileDestination = '_uploads/'. $fileNameNew;
                move_uploaded_file($fileTmpName);
                header("Location: index.php");
                echo "success";
            elseecho "Your file is too big hoe";
        
        elseecho "good try, youll have to upload again";
    
    elseecho "You cannot upload file of this type";


echo $_POST['value'];
//if $_POST == "yes"

$encodedStr = base64_encode('yes.txt'); //may need to encode inside of a seperate function
//may need to implement a search based on user input
//if search matches the stored access value, then decode the .txt document
echo $encodedStr; //cannot concat a string by itself, must make into a numeric value
$decodedStr = base64_decode ($encodedStr);
echo nl2br($decodedStr);

?>

上面的代码有什么不寻常的地方吗? php新手。

【问题讨论】:

这段代码没有必须显示上传按钮的部分(我理解你想要的),但是如果你发送一个包含这个脚本的地址的 Post 请求,它应该上传它。能否请您为我重新说明问题? 【参考方案1】:

试试这个来上传图片。

   //javascript
    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  class="img img-responsive" src="' + base_url + 'uploads/' + file.target_name + '"  /></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 file code 
<div class="form-group">
                                <div class="fileinput fileinput-new row mt-3" data-provides="fileinput" >
                                    <div id="image_div">
                                        <div class="form-group">
                                            <?php if (isset($blog_data['blog_image']))  ?>
                                                <input class="form" type="hidden" name="image" id="image" value="<?php echo isset($blog_data['blog_image']) ? $blog_data['blog_image'] : ''; ?>">
                                                <img src="<?php
                                                if ($blog_data['blog_image'] != '' && file_exists(FCPATH . 'uploads/blog/' . $blog_data['blog_image'])) 

                                                    echo base_url('uploads/blog/' . $blog_data['blog_image']);
                                                 else 

                                                    echo base_url() . '';
                                                
                                                ?>"  />
                                                 <?php  ?>
<!--<input class="form" type="hidden" name="image" id="image">-->
                                        </div>
                                    </div>
                                </div> 
                            </div>
                            
                            
                            
// controller code
$time_now = date('Y-m-d H:i:s');
                // upload blog image
                $blog_image = $this->input->post('image');
                if ($blog_image != '') 
                    $time = time();
                    if (file_exists(FCPATH . 'uploads/' . $blog_image)) 
                        $destination_path = FCPATH . 'uploads/blog/' . $time . '/';
                        if (!is_dir($destination_path)) 
                            mkdir($destination_path, 0777, TRUE);
                        
                        if (copy(FCPATH . 'uploads/' . $blog_image, $destination_path . $blog_image)) 
                            unlink(FCPATH . 'uploads/' . $blog_image);
                        
                    
                

【讨论】:

【参考方案2】:

试试这个来上传图片。

   //javascript
    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  class="img img-responsive" src="' + base_url + 'uploads/' + file.target_name + '"  /></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 file code 
<div class="form-group">
                                <div class="fileinput fileinput-new row mt-3" data-provides="fileinput" >
                                    <div id="image_div">
                                        <div class="form-group">
                                            <?php if (isset($blog_data['blog_image']))  ?>
                                                <input class="form" type="hidden" name="image" id="image" value="<?php echo isset($blog_data['blog_image']) ? $blog_data['blog_image'] : ''; ?>">
                                                <img src="<?php
                                                if ($blog_data['blog_image'] != '' && file_exists(FCPATH . 'uploads/blog/' . $blog_data['blog_image'])) 

                                                    echo base_url('uploads/blog/' . $blog_data['blog_image']);
                                                 else 

                                                    echo base_url() . '';
                                                
                                                ?>"  />
                                                 <?php  ?>
<!--<input class="form" type="hidden" name="image" id="image">-->
                                        </div>
                                    </div>
                                </div> 
                            </div>
                            
                            
                            
// controller code
$time_now = date('Y-m-d H:i:s');
                // upload blog image
                $blog_image = $this->input->post('image');
                if ($blog_image != '') 
                    $time = time();
                    if (file_exists(FCPATH . 'uploads/' . $blog_image)) 
                        $destination_path = FCPATH . 'uploads/blog/' . $time . '/';
                        if (!is_dir($destination_path)) 
                            mkdir($destination_path, 0777, TRUE);
                        
                        if (copy(FCPATH . 'uploads/' . $blog_image, $destination_path . $blog_image)) 
                            unlink(FCPATH . 'uploads/' . $blog_image);
                        
                    
                

            $blogInsertArray = array(
                
                'blog_image' => "$time" . "/" . $blog_image,
            );

【讨论】:

以上是关于图片未通过 PHP 上传到 mySql的主要内容,如果未能解决你的问题,请参考以下文章

图片未上传到php服务器ios 10 [关闭]

怎样用php实现上传图片到数据库

php 怎么把上传的图片路劲存到mysql数据库,然后再在页面读取出来?

无法在 php/mysql 中上传图片

在网站后台用户上传的图片如何获得图片路径存入数据库(mysql) (php解决) 急!!!

如何在 MySQL 中使用 Flutter 和 php 上传图片