I have a php script to upload nad delete photos, which mostly works. However, if I upload a photo and then delete it, when I try to upload it again I get an error message, “Warning: unlink(…/gallery/Sunset.jpg) [function.unlink]: No such file or directory in C:\wamp\www\LiquidBox\admin\add_delete_photos.php on line 30”
What am I doing wrong?
[php]<?php
//error_reporting(E_ALL ^ E_NOTICE);
$var="";
$path="";
if(isset($_POST[‘upload’]))
{
$uploadDir="…/gallery/";
$fileName = $_FILES[‘file’][‘name’];
$tmpName = $_FILES[‘file’][‘tmp_name’];
$fileSize = $_FILES[‘file’][‘size’];
$fileType = $_FILES[‘file’][‘type’];
$filePath = $uploadDir.$fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}
}
if ($_GET)
{
$var=$_GET[“var”];
$path="…/gallery/".$var;
unlink($path);
$path="";
}
?>
Admin Control Panel
<div class="centre">
<h2>Add Photos to Gallery</h2>
<form action="" method="post" name = "upload" onsubmit="return validate_upload ( );" enctype="multipart/form-data">
<table>
<tr><td><input type="file" name="file" id="file" /></td></tr>
<tr><td><input type="submit" name="upload" value="submit" /></td></tr>
</table>
</form>
<h2>Delete Photos from Gallery</h2>
<form action="" onclick="return confirm_delete()" name "delete" method="get">
<table>
<?php $count = "0";
$getDir = opendir("../gallery/");
while($filename = readdir($getDir)) {
if ($filename[0]!= "." && $filename[0]!= ".." && $filename!= "Thumbs.db") {
echo "<tr><td><a href='../gallery/".$filename."'>$filename</a></td><td>";?>
<a style="float:right; padding-left: 10px;"href="?var=<?php echo $filename;?>">delete</a></td></tr>
<?php
$count++;
}
$var="";
}
?>
</table>
</form>
</div>