This is the function I use to upload the image for a user's task:
if (isset($_POST["button"])) {
$naziv = trim($_POST["naziv"]);
$kategorija = trim($_POST["kategorija"]);
$opis = trim($_POST["opis"]);
$ime = time()."_".$_FILES["slika"]["name"];
$target = "../slike/".$ime;
Zadatak::dodaj($naziv, $korisnik, $kategorija, $opis, $ime, $konekcija)) {
}
This works fine, I see the changes in the database.
This is the code that calls the function to delete and unlink the image:
<?php
require "../konekcija/konekcija.php";
require "../modeli/zadatak.php";
$zadatak = trim($_GET["zadatak"]);
$podaci = Zadatak::vratiPodatke($zadatak, $konekcija);
?>
[...]
<h4>Slika</h4>
<button onclick="obrisi();">Obrisi</button>
[...]
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
function obrisi() {
let zadatak = <?=$podaci->zadatakID?>;
let slika = "../slike/'<?=$podaci->slika?>'";
$.ajax({
url: '../funkcije/obrisiSliku.php',
data: {
zadatak: zadatak,
slika: slika
},
success: function (data) {
}
});
}
</script>
This is obrisiSliku.php
:
<?php
require "../konekcija/konekcija.php";
require "../modeli/zadatak.php";
$zadatak = trim($_GET["zadatak"]);
$slika = trim($GET["slika"]);
Zadatak::obrisiSliku($zadatak, $konekcija);
unlink($slika);
?>
I set the image as NULL in the image field and it stops displaying it for the task on the page, but the unlink function doesn't seem to be working. Any ideas on what the issue could be?
Edit:
This is the error log I get:
[21-Apr-2023 00:26:43 Europe/Berlin] PHP Notice: Undefined variable: GET in /Applications/XAMPP/xamppfiles/htdocs/taskmanager/funkcije/obrisiSliku.php on line 6
[21-Apr-2023 00:26:43 Europe/Berlin] PHP Notice: Trying to access array offset on value of type null in /Applications/XAMPP/xamppfiles/htdocs/taskmanager/funkcije/obrisiSliku.php on line 6
[21-Apr-2023 00:26:43 Europe/Berlin] PHP Warning: unlink(): No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/taskmanager/funkcije/obrisiSliku.php on line 9