Hi there,
I have a form that list multiple files to be uploaded or deleted. The idea of my script is as follows:
1) I have a directory called "Images" that can have only 5 Images and their names start from "1.jpg" to "5.jpg"
2) If any of the input fields was filled it will upload it.
3) If the field was filled and in the remote the file was exist with the same name, it will replace it.
4) My script will check these 5 images and if someone exists it will echo a "checkbox" if this checkbox is checked then click submit this single file that was cjecked will be deleted.
The problem is any one of the checkboxe's if it was checked all the files are deleted!
I tried to make the checkbox as an array Delete[] but also it didn't worked!
HOW CAN I CHECK ONE BOX AND THEN DELETE THE DESIRED FILE ONLY NOT ALL OF THEM?
Here is my script:
<?
if (!isset($_POST[Uploaded]))
{ ?>
<form enctype="multipart/form-data" action="Upload.php" method="post">
<?
for ($i=1; $i<=5; $i++)
{
$UploadDirectory = "Images/";
$RemoteImageName = $UploadDirectory . $i . ".jpg";
if (is_file($RemoteImageName) == 1)
{?><input type="checkbox" name="Delete" value="Yes"><?}?>
<input name="userfile[]" type="file"><br>
<?}?>
<input type="hidden" name="Uploaded" value="1">
<input type="submit"></td>
</form>
<?
}
else
{
//Loop for handeling Images
for ($i=0; $i<5; $i++)
{
//When file field is NOT empty or DELETE field is CHECKED
if ($_FILES[userfile][name][$i] <> "" or $_POST[Delete] == "Yes"
{
//Prepare Remote path
$UploadDirectory = "Images/";
$RemoteImageName = $UploadDirectory . $i . ".jpg";
//Delete Image moduel
if ($_POST[Delete] == "Yes"
{
//Image exists and to be deleted
if (is_file($RemoteImageName) == 1)
{
unlink($RemoteImageName);
}
else
{
//Image doesn't exist
}
}
else
{
//Upload Images module
$UploadFile = $UploadDirectory. $_FILES[userfile][name][$i];
if (move_uploaded_file($_FILES[userfile][tmp_name][$i], $UploadFile))
{
//Image exist and to be replaced
if (is_file($RemoteImageName) == 1)
{
unlink($RemoteImageName);
rename($UploadFile,$RemoteImageName);
}
else
{
//Image dowsn.t exist and to be uploaded
rename($UploadFile,$RemoteImageName);
}
}
else
{
//Upload error
}
}
}
else
{
//Image field is empty and DELETE field is not checked
}
}
}
I have a form that list multiple files to be uploaded or deleted. The idea of my script is as follows:
1) I have a directory called "Images" that can have only 5 Images and their names start from "1.jpg" to "5.jpg"
2) If any of the input fields was filled it will upload it.
3) If the field was filled and in the remote the file was exist with the same name, it will replace it.
4) My script will check these 5 images and if someone exists it will echo a "checkbox" if this checkbox is checked then click submit this single file that was cjecked will be deleted.
The problem is any one of the checkboxe's if it was checked all the files are deleted!
I tried to make the checkbox as an array Delete[] but also it didn't worked!
HOW CAN I CHECK ONE BOX AND THEN DELETE THE DESIRED FILE ONLY NOT ALL OF THEM?
Here is my script:
<?
if (!isset($_POST[Uploaded]))
{ ?>
<form enctype="multipart/form-data" action="Upload.php" method="post">
<?
for ($i=1; $i<=5; $i++)
{
$UploadDirectory = "Images/";
$RemoteImageName = $UploadDirectory . $i . ".jpg";
if (is_file($RemoteImageName) == 1)
{?><input type="checkbox" name="Delete" value="Yes"><?}?>
<input name="userfile[]" type="file"><br>
<?}?>
<input type="hidden" name="Uploaded" value="1">
<input type="submit"></td>
</form>
<?
}
else
{
//Loop for handeling Images
for ($i=0; $i<5; $i++)
{
//When file field is NOT empty or DELETE field is CHECKED
if ($_FILES[userfile][name][$i] <> "" or $_POST[Delete] == "Yes"

{
//Prepare Remote path
$UploadDirectory = "Images/";
$RemoteImageName = $UploadDirectory . $i . ".jpg";
//Delete Image moduel
if ($_POST[Delete] == "Yes"

{
//Image exists and to be deleted
if (is_file($RemoteImageName) == 1)
{
unlink($RemoteImageName);
}
else
{
//Image doesn't exist
}
}
else
{
//Upload Images module
$UploadFile = $UploadDirectory. $_FILES[userfile][name][$i];
if (move_uploaded_file($_FILES[userfile][tmp_name][$i], $UploadFile))
{
//Image exist and to be replaced
if (is_file($RemoteImageName) == 1)
{
unlink($RemoteImageName);
rename($UploadFile,$RemoteImageName);
}
else
{
//Image dowsn.t exist and to be uploaded
rename($UploadFile,$RemoteImageName);
}
}
else
{
//Upload error
}
}
}
else
{
//Image field is empty and DELETE field is not checked
}
}
}