Reputation: 21
first of all I'm sorry for using Google translate. I don't know much about these things. As a result of my Google research, I learned that I could delete the old image with unlink, but it didn't work. The code below loads an image into the database and file but does not delete the old image.
if ($_FILES['fotodort']['tmp_name'] != "")
{
$boyutdort = $_FILES['fotodort']['sizedort'];
$tip = mime_content_type($_FILES['fotodort']['tmp_name']);
if($tip!="image/jpeg" && $tip!="image/jpg" && $tip!="image/png" && $tip!="application/pdf"){
echo 'Dosya Jpeg veya Png yada Gif , PDF formatında olmalı';
}elseif ($boyutdort > (1024 * 1024 * 30)) {
echo 'Dosya 10MB den büyük olamaz.';
} else {
$tipdort = $_FILES['fotodort']['type'];
$isimdort = $_FILES['fotodort']['name'];
$uzantidort = explode('.', $isimdort);
$uzantidort = $uzantidort[count($uzantidort) - 1];
$randomdort = rand();
$dosyadort = $_FILES['fotodort']['tmp_name'];
$foto_urldort = $randomdort . "." . $uzantidort;
copy($dosyadort, '../assets/images/admin-img/' . $foto_urldort);
if (file_exists("../assets/images/admin-img/".$foto_urldort)) {
unlink("../assets/images/admin-img/".$foto_urldort);
}
$settings = $dbh->prepare("UPDATE general_settings SET foto_dark = '".$foto_urldort."' WHERE id=1 ");
$settings->execute();
}
}
I tried to integrate the code I found from a different system into this code, but I could not succeed.
if (file_exists("../assets/images/admin-img/".$fotodort)) {
unlink("../assets/images/admin-img/".$fotodort);
}
I would be grateful if you could help with this. There are more than 20 image upload fields. I will update all of them with the method I will learn from you. It will also work if you teach a different code structure that will do the same operations.
Upvotes: 1
Views: 113
Reputation: 21
I found a solution and the system works fine. Do you check if block sequences are in the right place?
if ($_FILES['fotodort']['tmp_name'] != "") {
$boyutdort = $_FILES['fotodort']['sizedort'];
$tip = mime_content_type($_FILES['fotodort']['tmp_name']);
if($tip!="image/jpeg" && $tip!="image/jpg" && $tip!="image/png" && $tip!="application/pdf"){
echo 'Dosya Jpeg veya Png yada Gif , PDF formatında olmalı';
}elseif ($boyutdort > (1024 * 1024 * 30)) {
echo 'Dosya 10MB den büyük olamaz.';
} else {
$tipdort = $_FILES['fotodort']['type'];
$isimdort = $_FILES['fotodort']['name'];
$uzantidort = explode('.', $isimdort);
$uzantidort = $uzantidort[count($uzantidort) - 1];
$randomdort = rand();
$dosyadort = $_FILES['fotodort']['tmp_name'];
$foto_urldort = $randomdort . "." . $uzantidort;
copy($dosyadort, '../assets/images/admin-img/' . $foto_urldort);
echo "
<script>
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 6000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({
icon: 'success',
title: 'Başarı ile güncellendi.'
})
</script> ";
$settings = $dbh->prepare("UPDATE general_settings SET foto_dark = '".$foto_urldort."' WHERE id=1 ");
$settings->execute();
if (file_exists('../assets/images/admin-img/'.$_POST['eskiresimsil']) && $dosyadort) {
unlink('../assets/images/admin-img/'.$_POST['eskiresimsil']);
}
}
}
I wrote the old image name before the submit button:
<input type="hidden" name="eskiresimsil" value="<?=$ayar['foto_dark'];?>">
Upvotes: 1
Reputation: 902
try this
if ($_FILES['fotodort']['tmp_name'] != "") {
$size = $_FILES['fotodort']['sizedort'];
$type = mime_content_type($_FILES['fotodort']['tmp_name']);
if($type!="image/jpeg" && $type!="image/jpg" && $type!="image/png" && $type!="application/pdf"){
echo 'Dosya JPEG/JPG, PNG, yada PDF formatında olmalı';
}elseif ($boyutdort > (1024 * 1024 * 30)) {
echo 'Dosya 10MB den büyük olamaz.';
} else {
$dosyadort = $_FILES['fotodort']['tmp_name'];
$name = rand().$_FILES['fotodort']['extension'];
copy($dosyadort, '../assets/images/admin-img/' . $name);
$upload = $dbh->prepare("UPDATE general_settings SET foto_dark ='".$foto_urldort."' WHERE id=1");
$upload->execute();
if (file_exists("../assets/images/admin-img/".$fotodort) && $dosyadort) {
unlink("../assets/images/admin-img/".$fotodort);
}
}
}
Upvotes: 0