Błąd przy wrzucaniu zdjęcia

0

Witam
Mam problem z wrzuceniem pliku na mojej stronie. Podczas wykonywania skryptu wyskakuje mi błąd "Undefined index: image" w linijce z $_FILES. Przeglądałem już masę stron i nie mam pojęcia co jest nie tak :( mam nadzieje że pomożecie.

 if(isset($_POST['submit_post'])){
        $title = strip_tags($_POST['title']);
        $date = date('Y-m-d h:i:s');
            if($_FILES['image']['name']){
            $image_name = $_FILES['image']['name'];
            $image_tmp = $_FILES['image']['tmp_name'];
            $image_size = $_FILES['image']['size'];
            $image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
            $image_path = '../images/'.$image_name;
            $image_db_path = 'images/'.$image_name;
            
            if($image_size < 1000000){
                if($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'gif'){
                    if(move_uploaded_file($image_tmp, $image_path)){
                        $ins_sql = "INSERT INTO posts (title, description, image, category, status, date, author) VALUES ('$title', '$_POST[description]', '$image_db_path', '$_POST[category]', '$date', '$_SESSION[user]')";
                        if(mysqli_query($conn, $ins_sql)){
                            header('post_list.php');
                        } else{
                            $error = '<div class="alert alert-danger">The Query Was Not Working!</div>';
                        }
                    } else {
                        $error = '<div class="alert alert-danger">Sorry, Unfortunately Image host not been uploaded!</div>';
                    }
                    
                } else{
                    $error = '<div class="alert alert-danger">Image Format was not Correct!</div>';
                }
            } else{
                $error = '<div class="alert alert-danger">Image File Size is much bigger then Expect!</div>';
            }
        } else {
            $ins_sql = "INSERT INTO posts (title, description, category, status, date, author) VALUES ('$title', '$_POST[description]', '$_POST[category]', '$_POST[status]', '$date', '$_SESSION[user]')";
            if(mysqli_query($conn, $ins_sql)){
                header('post_list.php');
                } else{
                    $error = '<div class="alert alert-danger">The Query Was Not Working!</div>';
                }
        }
    }

a to mój formularz :

 <div class="form-group">
                      <label for="image">Upload an Image</label>
                      <input id="image" type="file" name="image" class="btn btn-primary">
                  </div>
0

Zamiast $_FILES['image'] użyj po prostu $_GET['image'] (lub $_POST['image'] zależnie od metody).

0

Mógłbym Ci powiedzieć co zrobić, ale też mógłbym zamiast ryby dać Ci wędkę. Więc proponuję zrobić prostego debuga i wpisać sobie w Twoim programie na początku

var_dump($_GET); / var_dump($_POST);
oraz
var_dump($_FILES);.

Dzięki temu zobaczysz co siedzi w tych zmiennych i będziesz łatwiej mógł ustalić przyczynę błędu.

0

Mógłby mi ktoś z tym pomóc ? :( siedzę już nad tym parę godzin i nie mam pojęcia czemu to nie chodzi :( przy var_dump($_FILES); wychodzi mi błąd że tablica jest pusta :(

1

I na pewno masz włączone w php.ini:

file_uploads = On

oraz w formularzu masz parametr enctype="multipart/form-data" ?

PS: możesz wykonać metodę phpinfo() i zobaczyć czy jest włączone file_uploads.

0
 <?php 
    session_start();
    
    include '../includes/db.php';
    if(isset($_SESSION['user']) && isset($_SESSION['password']) == true){
        $sel_sql = "SELECT * FROM users WHERE user_email = '$_SESSION[user]' AND user_password = '$_SESSION[password]'";
        if($run_sql = mysqli_query($conn, $sel_sql)){
            while($rows = mysqli_fetch_assoc($run_sql)){
                if(mysqli_num_rows($run_sql) == 1){
                    if($rows['role'] == 'admin'){
                    
                } else{
                    header('Location:../index.php');
                }
            } else{
                header('Location:../index.php');
            }
                }
        }
    } else {
        header('Location:../index.php');
    }

    
    $error = '';



    if(isset($_POST['submit_post'])){
        $title = strip_tags($_POST['title']);
        $date = date('Y-m-d h:i:s');
            if($_FILES['image']['name']){
            $image_name = $_FILES['image']['name'];
            $image_tmp = $_FILES['image']['tmp_name'];
            $image_size = $_FILES['image']['size'];
            $image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
            $image_path = '../images/'.$image_name;
			var_dump($_FILES);
            $image_db_path = 'images/'.$image_name;
            
            if($image_size < 1000000){
                if($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'gif'){
                    if(move_uploaded_file($image_tmp, $image_path)){
                        $ins_sql = "INSERT INTO posts (title, description, image, category, status, date, author) VALUES ('$title', '$_POST[description]', '$image_db_path', '$_POST[category]', '$date', '$_SESSION[user]')";
                        if(mysqli_query($conn, $ins_sql)){
                            header('post_list.php');
                        } else{
                            $error = '<div class="alert alert-danger">The Query Was Not Working!</div>';
                        }
                    } else {
                        $error = '<div class="alert alert-danger">Sorry, Unfortunately Image host not been uploaded!</div>';
                    }
                    
                } else{
                    $error = '<div class="alert alert-danger">Image Format was not Correct!</div>';
                }
            } else{
                $error = '<div class="alert alert-danger">Image File Size is much bigger then Expect!</div>';
            }
        } else {
            $ins_sql = "INSERT INTO posts (title, description, category, status, date, author) VALUES ('$title', '$_POST[description]', '$_POST[category]', '$_POST[status]', '$date', '$_SESSION[user]')";
            if(mysqli_query($conn, $ins_sql)){
                header('post_list.php');
                } else{
                    $error = '<div class="alert alert-danger">The Query Was Not Working!</div>';
                }
        }
    }

?>
<!DOCTYPE html>
<html lang="pl">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <title>Panel Admina</title>
<!-- if($_FILES['image']['name'] != ''){ -->
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    
    <!-- TinyMCE Editor -->
    <script src="//cloud.tinymce.com/stable/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' });</script>
    
  </head>
  
  

  <body>
      <?php include 'header.php' ?>
      <div style="width:50px; height:50px;"></div>
      
      <?php echo $error; ?>
      
      <?php include 'sidebar.php' ?>
      
      <div class="col-lg-10">
          <div class="page-header"><h1>New Post</h1></div>
          <div class="container-fluid">
              <form class="form-horizontal" action="new_post.php" method="post" enctype="multipart/form-date">
                  <div class="form-group">
                      <label for="image">Upload an Image</label>
                      <input id="image" type="file" name="image" class="btn btn-primary">
                  </div>
                  <div class="form-group">
                      <label for="title">Title</label>
                      <input id="title" type="text" name="title" class="form-control" required>
                  </div>
                  <div class="form-group">
                      <label for="category">Category</label>
                      <select id="category" name="category" class="form-control" required>
                         <option value="">Select Any Category</option>
                          <?php 
                            $sel_sql = "SELECT * FROM category";
                            $run_sql = mysqli_query($conn, $sel_sql);
                            while($rows = mysqli_fetch_assoc($run_sql)){
                                if($rows['category_name'] == 'home'){
                                    continue;
                                }
                                echo '<option value="'.$rows['category_name'].'">'.ucfirst($rows['category_name']).'</option>';
                            }
                          ?>
                      </select>
                  </div>
                  <div class="form-group">
                      <label for="description">Description</label>
                      <textarea id="description" name="description"></textarea>
                  </div>
                  <div class="form-group">
                      <label for="status">Status</label>
                      <select id="status" name="status" class="form-control">
                          <option value="publish">Publish</option>
                          <option value="draft">Draft</option>
                      </select>
                  </div>
                  <div class="form-group">
                      
                      <input type="submit" name="submit_post" class="btn btn-danger btn-block">
                  </div>
                  
              </form>
          </div>
      </div>
      
      <footer></footer>
      
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
      
  </body>
</html>
0

Gdzieś wyczytałem że należałoby dodać taką linijkę o maksymalnym rozmiarze

Więc zamiast

<input id="image" type="file" name="image" class="btn btn-primary">

daj

<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input id="image" type="file" name="image" class="btn btn-primary">

1 użytkowników online, w tym zalogowanych: 0, gości: 1