新的帖子表格,上传图片文件到数据库

时间:2014-11-10 08:33:04

标签: php mysql image forms upload

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

有一次,它将post_titlepost_text插入数据库,然后我再次将其破坏。但我最大的问题在于post_img不会上传到数据库,也不会对assests / 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.";
            }
        }
    }

我确实试过谷歌...但此时不成功。

谢谢大师:3!

1 个答案:

答案 0 :(得分:0)

  1. 确保您对上传到的文件夹拥有正确的权限(0777)。
  2. 如果你这样做,请确保你没有在upload_max_filesizepost_max_size上传超过你的限制,只需在php.ini中更改
  3. 确保临时文件夹的路径在php.ini
  4. 中有效upload_tmp_dir