【问题标题】:new post form with upload picture file to database将图片文件上传到数据库的新帖子表单
【发布时间】:2015-01-06 12:15:46
【问题描述】:

所以,我有点卡住了。我正在尝试制作一个表格,将新帖子添加到带有上传图片的数据库中。

在某一时刻,它把post_titlepost_text 插入到数据库中,然后我又把它弄坏了。但我最大的问题在于post_img 不会上传到数据库,也不会上传到assets/images 文件夹。

这是我的newarticle_index.php

添加新文章

        <!-- Form Name -->
        <legend>Form Name</legend>

        <!-- Text input-->
        <div class="control-group">
            <label class="control-label" for="textinput">Title</label>
            <div class="controls">
                <input id="textinput" name="data[post_title]" type="text" placeholder="Pealkiri" class="input-xlarge">

            </div>
        </div>

        <!-- Textarea -->
        <div class="control-group">
            <label class="control-label" for="textarea"> Description</label>
            <div class="controls">
                <textarea id="textarea" name="data[post_text]" placeholder="Write something good"></textarea>
            </div>
        </div>

        <!-- Picture upload -->
        <div class="row">
            <div class="col-md-12">
                <div class="pull-right">
                    <div class="btn-group btn-group-lg">
                        <button class="btn btn-default" id="btn-photo-upload">Add</button>
                    </div>
                </div>

                <form id="uploadForm" method="post" enctype="multipart/form-data" style=" display: none">
                    <input type="file" name="data[post_img]" id="text-photo-upload" class="file-upload"/>
                </form>
                <script>
                    $('#btn-photo-upload').click(function (event) {
                        $('#text-photo-upload').click();
                    });
                    //capture selected filename
                    $('#text-photo-upload').change(function (click) {
                //  $('#file-name').val(this.value);
                        $('form#uploadForm').submit();
                    });
                </script>
            </div>
        </div>



    </fieldset>
</form>

和我的控制器newarticle.php

  function index_upload()
    {
        $f = isset($_FILES['post_img']) ? $_FILES['post_img'] : false;
        if (!$f) {
            __('upload failed');
            //  return false;
        }
        $target_dir = "assets/images/" . basename($f["name"]);
        $uploadOk = 1;
        // Check if file already exists
        if (file_exists($target_dir . $f["name"])) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($f['size'] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($f["tmp_name"], $target_dir)) {
                $post['post_title']=basename($f["name"]);
                $post['post_img']=basename($f["name"]);
                $post['post_text']=basename($f["name"]);
                insert('post', $post);
                echo "The file " . basename($f["name"]) . " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }

我确实尝试过 google...但此时没有成功。

谢谢大师:3!

【问题讨论】:

    标签: php mysql image forms upload


    【解决方案1】:
    1. 确保您拥有上传到的文件夹的正确权限 (0777)。
    2. 确保上传的内容不超过upload_max_filesizepost_max_size 中的限制,如果这样做,只需在 php.ini 中进行更改
    3. 确保临时文件夹的路径在 php.ini 中是有效的upload_tmp_dir

    【讨论】:

    • 检查并如您所写。仍然不会按照我的要求去做。
    猜你喜欢
    • 2015-05-27
    • 2017-03-13
    • 1970-01-01
    • 2015-08-09
    • 2019-02-13
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多