It doesn’t get to save in the folder the picture that the user loads through the form. They get into the database, but in the folder that I specified no.

I beg you to help.

Here is the form

<form action="addnewpost.php" method="POST" enctype="multipart/form-data"> <fieldset> <div class="form-group"> <label for="Image">Image:</label> <input type="file" class="form-control" id="image" name="Image"/> </div> <input type="Submit" name="Submit" value="Add New Post" /> </fieldset> </form> 

Folder directory

Folder directory

and php code

 <?php if(isset($_POST["Submit"])){ $title = mysqli_real_escape_string($connection, $_POST["Title"]); $category = mysqli_real_escape_string($connection, $_POST["category"]); $post = mysqli_real_escape_string($connection, $_POST["Post"]); // тут время ничего интеречного date_default_timezone_get("Asia/Baku"); $current_time = time(); $date_time = strftime("%B-%d-%Y %H:%M:%S",$current_time); $admin = "Bulbasaur"; // тут работаем с картинкой $image = $_FILES["Image"]["name"]; // переменная для загрузки в папку upload картинку полученную из формы $target = "upload/".basename($_FILES["Image"]["name"]); if(empty($title)){ // скучная проверка $_SESSION["ErrorMessage"] = "Title can't be empty"; redirect_to("addnewpost.php"); } else if (strlen($title) < 4) { // еще одна скучная проверка $_SESSION["ErrorMessage"] = "Title should be at least 4 characters"; redirect_to("addnewpost.php"); } else { // скучная проверка базы данных все работает я проверял global $select; $Query = "INSERT INTO `admin_panel`(`datetime`,`title`,`category`,`author`,`image`,`post`) VALUES('$date_time','$title','$category','$admin','$image','$post')"; $execute = mysqli_query($connection, $Query); // тут перемещаем полученный файл в папку upload move_uploaded_file($_FILES["Image"]["tmp_files"], $target); if($execute){ $_SESSION["SuccessMessage"] = "Post added bruh"; redirect_to("addnewpost.php"); } else { $_SESSION["ErrorMessage"] = "Something went wrong.Try again bruh"; redirect_to("addnewpost.php"); } } } ?> 
  • one
    move_uploaded_file($_FILES["Image"]["tmp_name"], $target); This is tmp_name , not tmp_files . Provided everything else is correct, this is your decision. - Kosta B.

1 answer 1

It doesn’t get to save in the folder the picture that the user loads through the form. They get into the database, but in the folder that I specified no.

 move_uploaded_file($_FILES["Image"]["tmp_name"], $target); 

This is tmp_name , not tmp_files .

Provided everything else is correct, this is your decision.

It is possible to check if a move has occurred.

 $moved = move_uploaded_file($_FILES["Image"]["tmp_name"], $target); if($moved){ // make your changes } 

Dock move_uploaded_file

  • thank you very much - Demon __ ANT