您好,我操作很简单,我有这段代码可以上传产品的图片,但是该图片未保存在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数据库一样为空。
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">×
</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
不明白我对路径做了一些更改,但我还是一样。
答案 0 :(得分:0)
您需要正确设置表单的格式,主要是<form>
标签:
<!-- needs "multipart/form-data" -->
<form action="add.php" method="post" enctype="multipart/form-data">
以下是有关其他实现的建议;忽略您是否不在意
除其他问题外,您还需要确保使用正确的权限创建了将文件保存到的文件夹,需要对mysql查询进行参数化,并且需要确保您的php ini中有足够的内存:
我建议您使用mysqli的OOP版本或改用PDO。绑定更直接。
我还建议使用根文件夹配置文件来获取常量和一致性:
/config.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");
/includes/db.php
<?php
# Create an OOP connection instead of a functional connection
$conn = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
创建一个观察者以包含您的动作侦听器
/includes/functions/imageObserver.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