Create folder and upload file during the same insert

2

I want to insert a story together with the image file in a single post, the problem is that I would like to create a folder with the news ID where the image will be stored. Is it happening that the insert is running plus the upload, not some light or easier way to do that?

$imagem = $_FILES['imagem'];

$sqlInsert = "INSERT INTO noticias (titulo,categoria,conteudo) VALUES (:titulo,:categoria,:conteudo)";
$stmt = DB::prepare($sqlInsert);
$stmt->bindParam("titulo", $titulo);
$stmt->bindParam("categoria", $categoria);
$stmt->bindParam("conteudo", $conteudo);
$stmt->execute();

$ultimoid = DB::lastInsertId();
$dir = "../imagens/$ultimoid";
$pasta = mkdir("$dir", 0775);
if(is_dir("$dir")){
  if($arquivo != "none") {
    if (copy($_FILES['imagem']['tmp_name'], $dir . $_FILES['imagem']['name'])) {
    $arquivo1 = $_FILES['imagem']['name'];
    $sqlInsert = "UPDATE noticias SET imagem=:imagem WHERE idnoticia=$ultimoid";
    }
}

Remembering that I did a summary of the code only with the parts that I believe is where it is giving problem, any doubt put the whole code

    
asked by anonymous 18.02.2015 / 20:50

1 answer

2

You have put in the HTML of the attribute: enctype="multipart/form-data" , it is necessary to upload via POST.

Here's an example:

<form method="post" action="salvar-alguma-coisa.php" enctype="multipart/form-data">
    <input type="file" name="arquivo">
    <input type="submit" value="enviar">
</form>

JSFiddle

    
19.02.2015 / 01:01