图像未保存在文件夹中

uqcuzwp8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(416)

你好,我有一个crud操作,我有这个代码上传一个产品的形象,但形象不是保存在mysql或文件夹主机。

?php
 include("includes/db.php");
 $msg = "";
 if (isset($_POST['submit'])) {
 $target = "images/".basename($_FILES['poza']['name']);
 $image = $_FILES['poza']['name'];
 $nume = $_POST['nume'];
 $editor = $_POST['editor'];
 $pozitie = $_POST['pozitie'];
 $pret = $_POST['pret'];
 $cat = $_POST['cat'];
 $sql = "INSERT INTO produse (poza, titlu, descriere,pozitie,pret,categorie) 
 VALUES ('$image','$nume','$editor', '$pozitie','$pret','$cat')";
 mysqli_query($conn,$sql);

  // mut poza in folder

 if (move_uploaded_file($_FILES['poza']['tmp_name'], $target)) {
  $msg = "<div class='alert alert-success alert-dismissible' role='alert'>
                <button type='button' class='close' data- 
     dismiss='alert'>×</button>
                <div class='alert-icon'>
                 <i class='icon-check'></i>
                </div>
                <div class='alert-message'>
                  <span><strong>Success!</strong> Produsul a fost adaugat! 
          </span>
                </div>
              </div>";
      }else{
         $msg = "<div class='alert alert-danger alert-dismissible' 
           role='alert'>
               <button type='button' class='close' data- 
                    dismiss='alert'>×</button>
                <div class='alert-icon'>
                 <i class='icon-close'></i>
                </div>
                <div class='alert-message'>
                  <span><strong>Eroare!</strong> Produsul nu a fost salvat! 
                  </span>
                </div>
              </div>";
        }
            header('location: index.php');
       }

       ?>

目标是我的路径文件夹,但在我的文件夹图像是空的像mysql数据库相同的emtpy。
html在模态boostrap上,下面是代码:

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

  <div class="modal-content">
    <div class="modal-header">
       <button type="button" class="close" data-dismiss="modal">&times; 
        </button>
        <h4 class="modal-title">titlu</h4>
        </div>
       <div class="modal-body">
          <form class="" action="add.php" method="post">
            <div class="form-group">
             <label for="">descriere</label>
               <textarea name="editor" class="ckeditor" id="continut" 
                rows="8" cols="80" value=""><?php echo $row['continut'];?> 
               </textarea>
         <label for="">poza</label>
          <input type="file" name="poza" value="">
          <label for="">Titlu</label>
                  <input type="text" name="nume" value="">
                  <label for="">Pozitie</label>
                  <input type="text" name="pozitie" value="">
                  <label for="">pret</label>
                  <input type="text" name="pret" value="">
                  <label for="">pret</label>
                  <select name="cat" id="cat">
                 <option value="">Selecteaza categoria</option>
                 <option value="1">ACE DE CUSUT</option>
                 <option value="2">ATA & PAMBLICA</option>
                 <option value="3">FERMOAR</option>
                 <option value="4">NASTURI</option>
                 <option value="5">ALTE ACCESORII</option>
         </select>
                <button type="submit" name="submit">Salveaza</button>
      </div>
    </form>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data- 
     dismiss="modal">Close</button>
  </div>
</div>

我不知道 t know how to check the array $_FILES i will try but i can 我不明白我做了一些改变的道路,我仍然是一样的。

sauutmhj

sauutmhj1#

您需要正确格式化表单,主要是 <form> 标签:

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

以下是对其他实施的建议;如果你不在乎就忽略
除此之外的其他问题您需要确保文件保存到的文件夹具有正确的权限,您需要参数化您的mysql查询,并且您需要确保上载的php ini中有足够的内存:
我建议您使用mysqli的oop版本,或者改用pdo。装订要直接得多。
我还建议使用根文件夹配置文件来实现常量和一致性:
/配置.php

<?php
define('DS', DIRECTORY_SEPARATOR);

# Sometimes defines are helpful

define('ROOT_DIR', __DIR__);

# Set your image folder

define('IMAGE_DIR', ROOT_DIR.DS.'images');

# Set an include folder

define('INCLUDES', ROOT_DIR.DS.'includes');

# Set an functions folder

define('FUNCTIONS', INCLUDES.DS.'functions');

# Start session

session_start();

# Add the database

require(INCLUDES.DS."db.php");

/包括/db.php

<?php

# Create an OOP connection instead of a functional connection

$conn = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

创建一个观察者来包含你的动作监听器
/包括/functions/imageobservator.php

<?php

# Inject the DB and make sure the system messages are passed

function imageObserver($conn, &$msg)
{
    # Stop if no action
    if(!isset($_POST['action']))
        return false;
    # Stop if action not set
    if($_POST['action'] != 'upload_image')
        return false;
    # Make sure the image folder is created
    if(!is_dir(IMAGE_DIR))
        mkdir(IMAGE_DIR, 0755, true);
    # You may want to do a file check here incase someone uploads something
    # they shouldn't

    # Set image name
    $image   = $_FILES['poza']['name'];
    # Set the path
    $target  = IMAGE_DIR.DS.$image;
    # Move the file
    if(move_uploaded_file($_FILES['poza']['tmp_name'], $target)) {
        extract($_POST);
        # Create the string, use "?" in place of values
        $query = $conn->prepare("INSERT INTO produse (`poza`, `titlu`, `descriere`, `pozitie`, `pret`, `categorie`) VALUES (?, ?, ?, ?, ?, ?)");
        # Bind the values to the statement question marks
        $query->bind_param('ssssss', $image, $nume, $editor, $pozitie, $pret, $cat);
        # Execute the query
        $query->execute();
        # You should also do an error check here

        $msg['success'] = true;
    }
    else
        $msg['error'] = true;
}

/whatever-this-page-is.php

<?php

# add the config

require(__DIR__.DIRECTORY_SEPARATOR."config.php");

# add the observer function

require(FUNCTIONS.DS."imageObserver.php");

# Set the messages array

$msg = [];

# Add our listener

imageObserver($conn, &$msg);

# Check if something actually took place, redirect if not

if(empty($msg)) {
    header('index.php');
    # You have to exit on this header
    exit;
}
?>

<div class='alert alert-success alert-dismissible' role='alert'>
    <button type='button' class='close' data-dismiss='alert'>×</button>
    <div class='alert-icon'>
        <i class='icon-check'></i>
    </div>
    <div class='alert-message'>
        <span><strong><?php echo (!empty($msg['success']))? 'Success' : 'Eroare' ?>!</strong> Produsul a fost <?php echo (!empty($msg['success']))? 'adaugat' : 'salvat' ?>!</span>
    </div>
</div>

你的表格需要有一个 enctype 作为 multipart/form-data 您应该输入一个操作名,因为您可以用这个键名执行其他表单提交 submit :

<form class="" action="add.php" method="post" enctype="multipart/form-data">
    <!-- make the action name here -->
    <input type="hidden" name="action" value="upload_image" />
    <input type="file" name="poza" />
    <input type="submit" name="submit" value="Upload Now!" />
</form>

最后,您应该了解示例在应用程序中使用了多少内存 php.ini 文件,可能还不够(这只是示例设置,您可能还可以):

upload_max_filesize = 50M
post_max_size = 50M 
max_execution_time = 3000
max_input_time = 3000
memory_limit = 64M

相关问题