php – move_uploaded_file saves to database, but doesn't save image to folder! Someone help

Question:

<h1>Upload de Arquivos</h1>

<form name="enviarImagem" action="enviar.php" method="POST" enctype="multipart/form-data">
Arquivo<br> <input type="file" name="img" value=""><br><br>
<input type="submit" value="Enviar" name ="enviar">





<?php



$name = $_FILES["img"]["name"];
$temp = $FILES["img"]["tmp_name"];

/*var_dump($name);
var_dump($temp);*/

$banco = new mysqli("localhost", "root", "", "projeto");

$sql = "INSERT INTO imagem (nome) VALUES ('{$name}')";

$banco->query($sql);
$banco->close();


move_uploaded_file($temp, "./imagens/".$name);

header("Location: upload.php");

?>

Answer:

hello, try it like this:

File Upload

Archive

<?php

$name = $_FILES["img"]["name"];
$temp = $_FILES["img"]["tmp_name"];

/*var_dump($name);
var_dump($temp);*/

$banco = new mysqli("localhost", "root", "", "projeto");

$sql = "INSERT INTO imagem (nome) VALUES ('{$name}')";

$banco->query($sql);
$banco->close();

$dir = "./imagens";

@mkdir($dir);


move_uploaded_file($temp, ".$dir."/".$name);

header("Location: upload.php");

?>

Your error must be due to not informing the correct path to the image… Try putting @mkdir to create the folder and see where it was created… That way you can adjust until creating the folder in the correct place!

Scroll to Top