[英]new post form with upload picture file to database
So, I am kind of stuck. 所以,我有点卡住了。 I am trying to make a form, that adds new posts to database with uploaded pictures.
我正在尝试制作一个表单,将新帖子添加到带有上传图片的数据库中。
At one point, it inserted the post_title
and post_text
to database, then i broke it again. 曾经,它将
post_title
和post_text
插入到数据库中,然后我再次将其post_text
。 But my biggest problem lies in the post_img
wont upload to database and neither to assests/images folder. 但是我最大的问题在于
post_img
不会上传到数据库,也不post_img
传到assests / images文件夹。
this is my newarticle_index.php
这是我的
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>
and my controller newarticle.php
和我的控制器
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.";
}
}
}
I did try google... but unsuccessful at this time. 我确实尝试了google ...,但目前未成功。
Thank you gurus :3! 谢谢大师:3!
upload_max_filesize
and post_max_size
if you do, just change it inside the php.ini upload_max_filesize
和post_max_size
,只要在php.ini中更改它 upload_tmp_dir
inside the php.ini upload_tmp_dir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.