【发布时间】:2015-01-06 12:15:46
【问题描述】:
所以,我有点卡住了。我正在尝试制作一个表格,将新帖子添加到带有上传图片的数据库中。
在某一时刻,它把post_title 和post_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