php - 上传图片并添加mysql链接

标签 php mysql image content-management-system

我正在为我的网站创建一个 CMS,在我的管理页面中,我有一个添加页面,可以向我的网站添加新内容 LOCATED HERE并添加了一些表单字段。

其中 2 个是:

图像 URL(文本框)和上传图像(选择文件按钮)

当我填写所有字段并使用 IMAGE URL 选择图像并点击添加文章时,它工作正常,我的表单将保存到我的数据库中,然后显示在我的网站上。

当我填写所有字段并使用上传图像选择图像并点击添加文章时,它会将图像添加到我的 cpanel 中选择的文件夹中,但不会添加到数据库中。

我的问题是:如何将其添加到数据库中?并将新图像位置保存到数据库的图像字段中?

我在将上传文件按钮添加到我的页面时遵循了本教程。

请不要向我显示如何执行此操作的链接,因为我已经阅读过它们,但在将其添加到我的代码中时我遇到了困难。

我的add.php代码是这样的。

<?php

session_start();

include_once('../include/connection.php');

if (isset($_SESSION['logged_in'])){
  if (isset($_POST['title'], $_POST['content'])) {
        $title   = $_POST['title'];
        $content = nl2br($_POST['content']);
        if (!empty($_POST['image']))
        {
            $image = $_POST['image'];
        }
        else
        {
            $image = $_POST['imageupload'];

            if (isset($_FILES['imageupload']))
            {
              $errors = array();
              $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

              $file_name = $_FILES['imageupload'] ['name'];
              $file_ext = strtolower (end (explode ('.', $file_name)));
              $file_size = $_FILES['imageupload'] ['size'];
              $file_tmp = $_FILES['imageupload'] ['tmp_name'];

              if (in_array ($file_ext, $allowed_ext) === false) {
                     $errors[] = 'File extension not allowed';
              }

              if ($file_size > 2097152) {
                     $errors[] = 'File size must be under 2mb';
              }

              if (empty($errors)) {
                     if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
                           echo 'File uploaded';
                           $image = $file_name;
              }
              }else{
                    foreach ($errors as $error)
                    echo $error, '<br />';
              }

            }
        }
        $link     = $_POST['link'];
        $category = $_POST['category'];
        $brand    = $_POST['brand'];


if (empty($title) or empty($content)) {
         $error = 'All Fields Are Required!';
}else{
 $query = $pdo->prepare('INSERT INTO mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES(?, ?, ?, ?, ?, ?)');
 $query->bindValue(1, $title);
 $query->bindValue(2, $content);
 $query->bindValue(3, $image);
 $query->bindValue(4, $link);
 $query->bindValue(5, $category);
 $query->bindValue(6, $brand);



     $query->execute();
    header('location: index.php');
}

}
          ?>
    <?php

if (isset($_FILES['Filedata']))
{
// And if it was ok
    if ($_FILES['Filedata']['error'] !== UPLOAD_ERR_OK)
    exit('Upload failed. Error code: ' . $_FILES['image']['error']);

    $filename = $_FILES['Filedata']['name'];
    $targetpath    = "../img/news/" . $filename; //target directory relative to script location

    $copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);
}
?>

<html>
<head>
<title>Add Article</title>
<link rel="stylesheet" href="../other.css" />
</head>

<body>
<div class="container">
<a href="index.php" id="logo"><b>&larr; Back</b></a>

<br />

<div align="center">
<h4>Add Article</h4>

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data">

<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input name="imageupload" type="file" id="image" placeholder="Imageupload" />
<input type="text" name="image" placeholder="Image" /><br /><br />
<input type="link" name="link" placeholder="Link" /><br /><br />
<input type="category" name="category" placeholder="Category" /><br /><br />
<input type="category" name="brand" placeholder="Brand" /><br /><br />
<input type="submit" value="Add Article" />

</form>
</div>
</div>
</body>
</html>


<?php
}else{
       header('location: index.php');
}

?>

请帮忙。

最佳答案

if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
                       echo 'File uploaded';                          
           $image = '/images/'.$filename;//try updating the line like this

关于php - 上传图片并添加mysql链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539573/

相关文章:

php - Google Calendar API v3 无法添加事件 php

PHP/MySQL 在数据库中交换位置 + JavaScript (jQuery)

php - Silverstripe - 遍历模板中的非关联数组

javascript - AngularJs - 保存在数据库中时减小图像大小

javascript - 如何使用 Jquery/Javascript 将我的一个文件夹中的所有图像加载到我的网页中

php - Composer - 更改它在环境中使用的 PHP 版本

php - 选择用户参与的所有即将发生的事件

php - MySQL数据库设计——签到系统

mysql - 使用mysql时 `join`是否每次都生成笛卡尔积?

image - kendo ui imagebrowser默认图像文件夹