Я так понимаю проблема в
if(isset($_FILES['image']))
. Почему проверка работает неправильно, и выводит false, даже если $_FILES['image']) не пуст?
index.php
<?php
include "product.php";
include "config.php";
if(isset($_SESSION['error'])){
echo $_SESSION['error'];
}
if(isset($_SESSION['result'])){
echo $_SESSION['result'];
}
?>
<form method="POST" action="add.php">
<p><label for="title">Название товара</label><br>
<input type="text" id="title" name="title">
</p>
<p><label for="price">Цена товара</label><br>
<input type="number" id="price" name="price">
</p>
<p><label for="description">Описание товара</label><br>
<textarea name="description" id="description" cols="30" rows="3"></textarea>
</p>
<p><label for="category">Категория товара</label><br>
<input type="text" id="category" name="category">
</p>
<p><label for="image">Картинка товара</label><br>
<input id="image" type="file" name="image">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
</p>
<input type="submit" value="Добавить товар">
</form>
add.php
<?php
include "config.php";
function is_valid_type($filename){
$valid_types = array("jpeg", "png", "jpg", "svg");
$file_type = end(explode(".", $filename));
if (in_array($file_type, $valid_types)){
return true;
}
else{
return false;
}
}
if(isset($_POST['title'])){
include "product.php";
$new_product = new product;
$new_product->title = $_POST['title'];
$new_product->price = $_POST['price'];
$new_product->description = $_POST['description'];
$new_product->category = $_POST['category'];
if(isset($_FILES['image'])){
$image = $_FILES['image'];
if(is_valid_type($image['name'])){
$TARGET_PATH .=$image['name'];
if(move_uploaded_file($image['tmp_name'], $target_path)){
$new_product->img = $TARGET_PATH;
$new_product->add_product($link, $new_product);
$_SESSION['result']= "Картинка успешно добавлена";
$_SESSION['result'] .= "<br>" . $target_path;
$_SESSION['error'] = null;
header("Location: index.php");
}
else{
$_SESSION['result'] = 'Картинка не загружена';
}
}
else{
$_SESSION['error'] = "Тип картинок не подходит <br>";
$_SESSION['result'] = null;
}
}
else{
$_SESSION['result'] = 'Картинка не загружена';
$new_product->img = null;
$new_product->add_product($link, $new_product);
header("Location:index.php");
}
}
?>
product.php
<?
include "db.php";
include "config.php";
class product{
public $id, $title, $price, $description, $category, $img;
public function add_product($link,$new_product){
$sql = "INSERT INTO products (title, price, description, category, img) VALUES ('$new_product->title', $new_product->price, '$new_product->description', '$new_product->category', '$new_product->img')";
$result = mysqli_query($link, $sql) or die ("Невозможно вставить данные в базу: " . mysqli_error($link));
if($result){
$_SESSION['result'] .= "<br> Товар добавлен <br>";
}
else if(!$result){
$_SESSION['result'] .= "<br> Товар не добавлен <br>";
}
}
}
?>
config.php
<?php
session_start();
// Путь к папке с каринками
$TARGET_PATH = "images/products/";
global $target_path;
?>