Member Avatar for RonKevinT.Manuela

I am able to delete the file from the database but not the file found on a folder associated with it...is there somethign wrong?

<?php include("../includes/mysqliconnect.php"); include("../includes/config.php"); if(isset($_GET['delpost'])){ $delpho = $mysqli->query("SELECT * FROM photos where imageID=$imageID"); $run=mysqli_fetch_array($delpho); $imageName=$run['imageName']; $dir = '/uploads'; unlink($dir.'/'.$imageName); $stmt = $db->prepare('DELETE FROM photos WHERE imageID = :imageID'); $stmt->execute(array(':imageID' => $_GET['delpost'])); header('Location: index.php'); } ?> <html> <head> <title>Album Gallery</title> <script src="js/jquery-1.11.0.min.js"></script> <script src="js/lightbox.min.js"></script> <link href="css/lightbox.css" rel="stylesheet" /> <link rel='stylesheet' href='style.css'> <link href="/sics/css/bootstrap.css" rel="stylesheet"> <link href="/sics/css/simple-sidebar.css" rel="stylesheet"> <link rel="stylesheet" href="/sics/css/bootflat.css"> <link rel="stylesheet" type="text/css" href="/sics/css/MainPanel.css"> <script language="JavaScript" type="text/javascript"> /*The javascript confirm function, once confirmed a command window.location.href is ran which will redirect the page, in this case it goes to viewpost.php again but appends ?delpost= and the id of the post to be deleted, which in turn will execute a php function.*/ function delpost(id, title) { if (confirm("Are you sure you want to delete '" + title + "' photo?")) { window.location.href = 'view.php?delpost=' + id; } } </script> </head> <body> <div id='body'> <?php include('title_bar.php'); include('../includes/navbar.php');?> <div id='container'> <?php $album_id= $_GET['id']; $query = $mysqli->query("SELECT url, imageID FROM photos where albumid=$album_id"); while ($run=mysqli_fetch_array($query)) { $url=$run['url']; $imageID=$run['imageID']; ?> <div id='view_box'> <a href='uploads/<?php echo $url; ?>' data-lightbox="image"> <br> <img src='uploads/<?php echo $url; ?>'></a> <p class="text-center"><a href="javascript:delpost('<?php echo $imageID;?>','<?php echo $url;?>')">Delete</a></p> </div> <?php } ?> <div class='clear'></div> </div> <a href="index.php"><button class="btn btn-primary">Back</button></a> </div> <!--FOOTER --> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="../js/bootstrap.min.js"></script> <!-- Bootflat js files --> <!-- Bootflat's JS files.--> <script src="../js/icheck.min.js"></script> <script src="../js/jquery.fs.selecter.min.js"></script> <script src="../js/jquery.fs.stepper.min.js"></script> </body> </html> 
Member Avatar for mark.suner

This is also my problem last week then I found this realpath function in php

$realpath = realpath('/xampp/htdocs/directoryName/images/uploads');
unlink($realpath.'/'.$imageName);

hope this help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.