Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handle Multiple Files

Status
Not open for further replies.

alsaffar

Programmer
Joined
Oct 25, 2001
Messages
165
Location
KW
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=&quot;multipart/form-data&quot; action=&quot;Upload.php&quot; method=&quot;post&quot;>
<?
for ($i=1; $i<=5; $i++)
{
$UploadDirectory = &quot;Images/&quot;;
$RemoteImageName = $UploadDirectory . $i . &quot;.jpg&quot;;
if (is_file($RemoteImageName) == 1)
{?><input type=&quot;checkbox&quot; name=&quot;Delete&quot; value=&quot;Yes&quot;><?}?>
<input name=&quot;userfile[]&quot; type=&quot;file&quot;><br>
<?}?>
<input type=&quot;hidden&quot; name=&quot;Uploaded&quot; value=&quot;1&quot;>
<input type=&quot;submit&quot;></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] <> &quot;&quot; or $_POST[Delete] == &quot;Yes&quot;)
{
//Prepare Remote path
$UploadDirectory = &quot;Images/&quot;;
$RemoteImageName = $UploadDirectory . $i . &quot;.jpg&quot;;
//Delete Image moduel
if ($_POST[Delete] == &quot;Yes&quot;)
{
//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
}
}
}
 
Change
[tt]<input type=&quot;checkbox&quot; name=&quot;Delete&quot; value=&quot;Yes&quot;><?}?>[/tt]
to read
[tt]<input type=&quot;checkbox&quot; name=&quot;Delete[<?php echo $i ?>]&quot; value=&quot;Yes&quot;><?}?>[/tt]
and then delete the elements that are present in the [tt]$_POST['Delete'][/tt] array.

//Daniel
 
Ok, I changed &quot;Delete&quot; to &quot;Delete<? echo $i ?>

Then how can I check the Delete post variable? How can I changed the if condition?

if ($_POST[Delete] == &quot;Yes&quot;)
 
I suggest to pass the filename in the value of the checkbox. The way Daniel suggested there would be several fields starting with the name Delete.
Code:
# when image exists:
<input type=&quot;checkbox&quot; name=&quot;Delete&quot; value=&quot;<? echo $RemoteImageName ?>&quot;>

# in the receiving script:
Code:
foreach ($_POST['Delete'] as $value) {
   # unlink code with filename etc....
 
Ok, still Delete is not an array! how can I use the code foreach!

I dont want to use foreach statement, because Im in the big for loop that will examine each file at a time.

Please I want my script to examine each userfile[]:

- if it was not filled and Delete not checked -> no action
- if it was not filled and Delete is checked -> delete
- if it was filled it will upload it

Please help me, I have to finish it today :(
 
It seems the Tek-Tips parser has changed, what I meant was:
Code:
<input type=&quot;checkbox&quot; name=&quot;Delete[<?php echo $i ?>]&quot; value=&quot;Yes&quot;><?}?>
and I think DRJ478 meant:
Code:
<input type=&quot;checkbox&quot; name=&quot;Delete[]&quot; value=&quot;<? echo $RemoteImageName ?>&quot;>

//Daniel
 
I LOVE YOU GUYS :) IT IS WORKING ;) THANX ALOT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top