Hello I'm new to PHP, here's what I'm haveing trouble with. I'm using php to display a gallery in a directory and delete the images, but I get an error when I try to use the delete button and I don't know why or how to fix it, can anyone help?
Parse error: syntax error, unexpected $end in /delete.php on line 22

Here is the code on the page:

<form name="form1" method="post" action="delete.php">
<?php

$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type=CHECKBOX name=$file>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);

?>
<input type="submit" name="Delete" value="Delete">
</form>

And here is the delete.php code (line 22 is the end of the code ?>):

<?php
$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");

//We list the name of the files again, since the name of the checkbox is the same with the name of the file
while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

if(isset($_POST[$file])){
   $checkbox = $_POST[$file];
   if($checkbox == on) { //checkbox is selected
      //Delete the file
      if(!unlink($file)) die("Failed to delete file");
   }
}
?>

close your while statement

Thanks for replying Rob I no longer get an error, but now when I click on delete the page refreshes but the image is still there, any idea's?

Here's what the code looks like now"

<form name="form1" method="post">
<?php

$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type=CHECKBOX name=$file>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);

?>
<?php
$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");


while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

if(isset($_POST[$file])){
   $checkbox = $_POST[$file];
   if($checkbox == on) { 
      
      if(!unlink($file)) die("Failed to delete file");
   }
}}
?>
<input type="submit" name="Delete" value="Delete">
</form>

the problem is that when you post a variable name that includes a ".", the "." is automatically replaced with an "_". You can see that by putting print_r($_POST); at the top of your file.

I'm looking for a fix now.

moved the delete block to the top so the file is deleted before you fill the form with the current existing files. Included current url redirect to refresh after the post(just a personal preference) because I think is reflects a better experience for the user to not have to worry about reposting every time the user wants to refresh the page after a post. Changed the checkbox name to an array, and the script loops through all the checked items of the array.

<?php
$path = "test";

if(isset($_POST['file']) && is_array($_POST['file']))
{
	foreach($_POST['file'] as $file)
	{	
		unlink($path . "/" . $file) or die("Failed to delete file");
	}
	header("location: " . $_SERVER['REQUEST_URI']); //redirect after deleting files so the user can refresh without that resending post info message
}
?>
<form name="form1" method="post">
<?php

$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);

?>
<input type="submit" name="Delete" value="Delete">
</form>

Rob u are my hero, i got an error with the header though so after searching around the net and ending up with nothing i just deleted the line that had the header, which was on line 10 of the above post. So now the file unlinks perfectly, Thank You! But now when i reload the original page the pictures still show up, although when i check the directory the files are not there, any idea why the pictures still show up?

Here's what I have:

<?php
$path = "test";

if(isset($_POST['file']) && is_array($_POST['file']))
{
	foreach($_POST['file'] as $file)
	{	
		unlink($path . "/" . $file) or die("Failed to delete file");
	}
}
?>
<form name="form1" method="post">
<?php
$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);
?>
<input type="submit" name="Delete" value="Delete">
</form>

And in case deleting header("location: http://www.mysite.com/cp.php" . $_SERVER); is the cause of my current problem the error i get is:

Warning: Cannot modify header information - headers already sent by (output started at /public_html/cp.php:18) in /public_html/cp.php on line 68

That happens when you send anything to the browser before trying to redirect, even a blank line. So if your "<?php" starts on line 2, you will get an error if you try to redirect at any point in the script.

As far as the images still being there, the redirect probably will solve the problem because I think it is browser cache. You say the images aren't in the directory anymore but they still appear on the page right? ya browser cache. Try to get the redirect to work. If you can't get it to work, post your entire script because according to that error, you sent headers prior to the redirect, which means that you are not including something above this block of script, right?

Ya ur right it was the browser cache, i clued into that right after i posted the issue, so the code does work. Thank you again. If your interested in the redirect I'll post whole code im using.

<html>

<head>

<title></title>
<meta name="description" content="">
</head>
<body background="backborder.jpg">
<BODY TOPMARGIN=0>

<TABLE  cellSpacing=0 cellPadding=0 width="80%" align=center border=1 bordercolor=#FFFF40>
<TR>
    <TD align=left bgcolor="#C0C0FF">

<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center background=homepage.jpg 
border=0>
<TR>
    <TD align=center background=images/targetbanner2.jpg colSpan=0 height=100>
      <H1 align=center>

<img src="title.gif">

<br>
</H1></TD>
    <TD colSpan=2>
</TR></table>
<br>
<br>
<table width="500" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Multiple Files Upload </strong></td>
</tr>
<tr>
<td>Select file 
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<br>
<?php
$path = "test";

if(isset($_POST['file']) && is_array($_POST['file']))
{
	foreach($_POST['file'] as $file)
	{	
		unlink($path . "/" . $file) or die("Failed to delete file");
	}
}
?>
<form name="form1" method="post">
<?php
$path = "test";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);
?>
<input type="submit" name="Delete" value="Delete">
</form>
<br>

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>


</TD>
</TR></table>

</body>

</html>
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.