Can't upload the same file twice

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="";

}
?>

Add Delete Photographs

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>
[/php]

Hi,

could it be you try to overwrite a file that’s already there?
[php]
if ( file_exists( $uploadDir.$fileName ) )
{ // Depending on what you want to do you either
// discard the old file or rename the new one

unlink( $uploadDir.$fileName ); // discard the old file
$filePath = $uploadDir.$fileName;

$parts = pathinfo( $fileName ); // rename the new file
$newFileName = $parts['fileName']. '_'. date( 'd-m-Y' ). '.'. $parts['extenison'];
$filePath = $uploadDir.$newFileName; 
// Comment one out

}
$result = move_uploaded_file($tmpName, $filePath);
[/php]

Good luck,
O.

Thanks Ojoshiro for your response.
Sorry, I realise that I haven’t made the title of this post very clear.
I upload picture “Bluehills.jpg”, no problems, I then upload picture “Winter.jpg”, no problems.
I upload “Bluehills.jpg” again, no problems. I delete file “Bluehills.jpg”, no problems. I delete “Winter.jpg”, no problems. When I upload “Bluhills.jpg” again, it uploads but with error message, “Warning: unlink(…/gallery/Winter.jpg) [function.unlink]: No such file or directory in C:\wamp\www\LiquidBox\admin\add_delete_photos.php”.
If I try to upload “Winter.jpg” again, I get no errors but the file doesn’t upload. In the address bar the file deleted filename is still present as if it’s trying to delete the file “Winter.jpg” which doesn’t exist.
http://localhost/LiquidBox/admin/add_delete_photosOLD.php?var=Winter.jpg

I’ve tried to add the line “if ( file_exists( $uploadDir.$fileName ) )” for the delete section of code with no joy yet.
I’ll keep trying…

Found the problem. The delete variable returned the $_GET function needed to be cleared before the next upload. Thanks again Ojoshiro for your input. For anyone else with a similar problem the following code works.
[php]
if(isset($_POST[‘upload’]))
{
//clear ‘delete’ variable from previous delete operation
$_GET[“delete”]=NULL;

$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;
}

}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service