My dropzone form
<div align="center">
<button class="btn btn-success" id="add" onclick='add_img()'>Adicionar imagens <i class="lni-check-mark-circle"></i></button>
</div>
<span id="menos_img"></span>
<span style="display: none;" id="mais_img">
<div class="m-auto">
<form action="clients_images_update.php" style="min-height: 0px;" method="POST" class="dropzone">
<input type="hidden" name="clients_id" value="<?php echo $id; ?>">
<input type="hidden" name="users_id" value="<?php echo $_SESSION["user"]["id"]; ?>" />
</form>
<div align="center">
<br>
<button class="btn btn-success" onClick="window.location.reload();">Atualizar <i class="lni-check-mark-circle"></i></button>
<button class="btn btn-secondary" onClick="cancela();">Cancelar <i class='lni-cross-circle'></i></i></button>
</div>
</div>
</span>
My upload file:
if(!empty($_FILES)) {
$fileName = $_FILES['file']['name'];
$source_path = $_FILES['file']['tmp_name'];
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$targetFile = $id."_".$fileName;
//$targetFile = $id."_".strtotime("now").$fileName;
$target_path = "img/clients/".$targetFile;
$array["filename"] = $targetFile;
$array["main"] = (int)($db->query("SELECT * FROM images WHERE clients_id = :clients_id;", array("clients_id" => $id), false) == 0);
$array["clients_id"] = $id;
if(move_uploaded_file($source_path, $target_path)) {
$db->insert("images", $array);
}
$db->insert("log", array("action" => "image", "inserted_on" => date("Y-m-d H:i:s"), "users_id" => $_POST["users_id"], "clients_id" => $id));
}
I want to upload the files to 2 folders. I've tried to do the same process, but to store in the 2 folder but the upload is not done. I tried, added this code excerpt with the code made above but it doesn't upload
if(!empty($_FILES)) {
$fileName = $_FILES['file']['name'];
$source_path = $_FILES['file']['tmp_name'];
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$targetFile = $id."_".$fileName;
//$targetFile = $id."_".strtotime("now").$fileName;
$target_path = "img/clients/thumbs/".$targetFile;
if(move_uploaded_file($source_path, $target_path)) {
}
}
The complete code I tried (relative to the upload of the images) is this:
if (!empty($_FILES)) {
$fileName = $_FILES['file']['name'];
$source_path = $_FILES['file']['tmp_name'];
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$targetFile = $id."_".$fileName;
//$targetFile = $id."_".strtotime("now").$fileName;
$target_path = "img/clients/".$targetFile;
$target_path2 = "img/clients/thumbs/".$targetFile;
$array["filename"] = $targetFile;
$array["main"] = (int)($db->query("SELECT * FROM images WHERE clients_id = :clients_id;", array("clients_id" => $id), false) == 0);;
$array["clients_id"] = $id;
if (move_uploaded_file($source_path, $target_path)) {
$db->insert("images", $array);
}
image::resize($source_path, "img/clients/thumbs/".$targetFile, 100, 100);
$db->insert("log", array("action" => "image", "inserted_on" => date("Y-m-d
H:i:s"), "users_id" => $_POST["users_id"], "clients_id" => $id));
if(move_uploaded_file($source_path, $target_path1)) {
echo "Success";
}
}
First, let's see what you're doing wrong:
// You're moving the original file right here, this is fine
if(move_uploaded_file($source_path, $target_path)) {
$db->insert("images", $array);
}
// This won't work, because you're trying to access orginal file (moved already)
image::resize($source_path, "img/clients/thumbs/".$targetFile, 100, 100);
$db->insert("log", array("action" => "image", "inserted_on" => date("Y-m-d
H:i:s"), "users_id" => $_POST["users_id"], "clients_id" => $id));
// Now you're trying to move again the original file
if(move_uploaded_file($source_path, $target_path1)) {
echo "Success";
}
Then, the logic is simplier:
First: If there is a file, move it to it's destination and keep it without modifications
Second: Create the thumb and save it in its corresponding folder, you don't need to copy, because the file is already there
if(!empty($_FILES)) {
$fileName = $_FILES['file']['name'];
$source_path = $_FILES['file']['tmp_name'];
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$targetFile = $id."_".$fileName;
$target_path = "img/clients/".$targetFile;
$array["filename"] = $targetFile;
$array["main"] = (int)($db->query("SELECT * FROM images WHERE clients_id = :clients_id;", array("clients_id" => $id), false) == 0);;
$array["clients_id"] = $id;
// Move original file
if(move_uploaded_file($source_path, $target_path)) {
$db->insert("images", $array);
// Create thumb only if the file was moved, otherwhise you'll get errors
// Your source is the file moved, not the one on temp folder
image::resize($target_path, "img/clients/thumbs/".$targetFile, 100, 100);
$db->insert("log", array("action" => "image", "inserted_on" => date("Y-m-d
H:i:s"), "users_id" => $_POST["users_id"], "clients_id" => $id));
}
}