gpt4 book ai didi

php - 新的帖子表单,可将​​图片文件上传到数据库

转载 作者:行者123 更新时间:2023-11-29 23:27:34 24 4
gpt4 key购买 nike

所以,我有点陷入困境。我正在尝试制作一个表单,将带有上传图片的新帖子添加到数据库中。

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

和我的 Controller 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. 确保您对上传到的文件夹拥有正确的权限 (0777)。
  2. 确保上传的文件大小不超过 upload_max_filesizepost_max_size 的限制,如果超过,只需在 php.ini 中更改即可
  3. 确保 php.ini 中临时文件夹的路径有效 upload_tmp_dir

关于php - 新的帖子表单,可将​​图片文件上传到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26839635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com