Jump to content

UNLINK() help need please.


richarro1234

Recommended Posts

hey all,

 

I have thispeice of code that displays images on a wenpage for approval, it all works fine for single file deletion, but as soon as you select more then one image to be deleted it doesnt work.

 

At the moment it will delete the file from the server and delete the imagefrom the database, but multiples wont seem to work.

 

Can someone help me as to why it wont work.

 

Thanks

 

<?include("header.php");?>
<?
if ($_GET['note'] == 'delsucmulti') {
	$note = "<br><center>
			 <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='75%' id='AutoNumber1' bordercolor='red' bgcolor='#000000'>
				<tr>
				  <td width='100%' class=trnext align='center'><font color='red' size='2'><b>Your messages have been deleted successfully!</b></font></td>
				</tr>
			 </table>
			 </center><br>";
	}
include("data.php");
mysql_connect($server,$anvandare, $losen);
mysql_select_db($databas);
$query = mysql_query("SELECT * from users WHERE username = '".$_COOKIE["twusername"]."'");
while ($r = @mysql_fetch_array($query)) {
$approv = $r['id'];?>

<?
if ($_GET['action'] == 'deletemulti') {
$delete = $_POST["file_name"];
$deleted_items = join(', ', $_POST["deleted_items"]);
$deleted_items1 = join(', ', $_POST["deleted_items1"]);

chdir('../userimages/');
    $do = unlink($deleted_items);
    if($do=="1"){
        echo "The file was deleted successfully.";
    } else { echo "There was an error trying to delete the file."; } 

$query = mysql_query("DELETE FROM userimg WHERE id = ($deleted_items1)");
header ("Location: approveimg.php?note=delsucmulti");
die();
}
?>

<?}?>
<center>
<?=$note?></center>
<br>
<div align="center">
  <center>
<div align="center">
  <center>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#5D6765" width="95%" id="AutoNumber1" bgcolor="#131313">
  <tr>
    <td width="5%" align="center" bgcolor="#000000"><b>Delete</b></td>
    <td width="16%" align="center" bgcolor="#000000"><b>Picture</b></td>
    <td width="16%" align="center" bgcolor="#000000"><b>User Edited</b></td>
    <td width="16%" align="center" bgcolor="#000000"><b>Value Edited</b></td>
    <td width="16%" align="center" bgcolor="#000000"><b>Original Value</b></td>
    <td width="15%" align="center" bgcolor="#000000"><b>New Value</b></td>
    <td width="16%" align="center" bgcolor="#000000"><b>Date</b></td>
  </tr>
  <tr>
    <td width="100%" align="center" colspan="6"> </td>
  </tr>
  <form action="approveimg.php?action=deletemulti" name="approv" method="post">
<?
$results= mysql_query("SELECT * FROM userimg WHERE approved = 'no'"); 
$id = "id";
$filename = "file_name";
$filesize = "filesize";
$filetype = "filetype";
$desc = "description";
$ownerid = "ownerid";

echo mysql_error();

if (mysql_Numrows($results)>0)                            //if there are records in the fields
{ 
  $numrows=mysql_NumRows($results);                       //count them
  $x=0; 
  while ($x<$numrows){   //loop through the records

    $theid=mysql_result($results,$x,$id);
    $thename=mysql_result($results,$x,$filename);
    $thesize=mysql_result($results,$x,$filesize);
    $theowner=mysql_result($results,$x,$ownerid);
    $thetype=mysql_result($results,$x,$filetype);
    $thedesc=mysql_result($results,$x,$desc);
    $thename1 = "<img src='../userimages/$thename' height='100' width='100'>";
?>

  <tr>
  <input type="hidden" class="normal" value="<?=$theid;?>" name="deleted_items1[]">
    <td width="5%" align="center"><input type="checkbox" class="normal" value="<?=$thename?>" name="deleted_items[]">
    <td width="16%" align="center"><a href="../userimages/<?=$thename;?>"><img src="..//userimages/<?=$thename?>" width="100" height="100"></a> </td>
    <td width="16%" align="center"><a href="profile.php?id=<?=$memlink['id'];?>"><?=$theusername?></a> </td>
    <td width="16%" align="center"><?=$theaction?> </td>
    <td width="16%" align="center"><?=$theorigvalue?> </td>
    <td width="15%" align="center"><?=$thenewvalue?> </td>
    <td width="16%" align="center"><?=$logdate?> </td>
  </tr>

<?
    $x++;
  }
?>
<td width="5%" align="center" bgcolor="#000000"><input type="submit" value="DEL"></td>
    <td width="95%" align="center" colspan="3" bgcolor="#000000"><font size="2"><b><a href="#" onclick="setCheckboxes('approv', true); return false;">Select All</a>
| <a href="#" onclick="setCheckboxes('approv', false); return false;">Unselect All</a></b></font></td>
  </form
</table>
  </center>
</div>
<?php } else {
?>
</center>
<center><b>There Are No New Images to Approve!</b></center>
<br>
<?
}
?>
      </td>
    </tr>
  </table>
  </center>
</div>
<br>
<?include("footer.php");?>

Link to comment
Share on other sites

To unlink and item, I think you will have to loop through each individual record. I do not tink you can just seperate each file by a comma and have it work.

 

$deleted_items = $_POST["deleted_items"];
foreach ($deleted_items as $filename) 
    unlink($filename);

 

Unsure if that will work since I cannot see the form, but yea.

Link to comment
Share on other sites

ill try the print and report back,

 

the form is under the second sql bit.

 

  <form action="approveimg.php?action=deletemulti" name="approv" method="post">

<?

$results= mysql_query("SELECT * FROM userimg WHERE approved = 'no'");

$id = "id";

$filename = "file_name";

$filesize = "filesize";

$filetype = "filetype";

$desc = "description";

$ownerid = "ownerid";

 

echo mysql_error();

 

if (mysql_Numrows($results)>0)                            //if there are records in the fields

{

  $numrows=mysql_NumRows($results);                      //count them

  $x=0;

  while ($x<$numrows){  //loop through the records

 

    $theid=mysql_result($results,$x,$id);

    $thename=mysql_result($results,$x,$filename);

    $thesize=mysql_result($results,$x,$filesize);

    $theowner=mysql_result($results,$x,$ownerid);

    $thetype=mysql_result($results,$x,$filetype);

    $thedesc=mysql_result($results,$x,$desc);

    $thename1 = "<img src='../userimages/$thename' height='100' width='100'>";

?>

 

  <tr>

  <input type="hidden" class="normal" value="<?=$theid;?>" name="deleted_items1[]">

    <td width="5%" align="center"><input type="checkbox" class="normal" value="<?=$thename?>" name="deleted_items[]">

    <td width="16%" align="center"><a href="../userimages/<?=$thename;?>"><img src="..//userimages/<?=$thename?>" width="100" height="100"></a> </td>

    <td width="16%" align="center"><a href="profile.php?id=<?=$memlink['id'];?>"><?=$theusername?></a> </td>

    <td width="16%" align="center"><?=$theaction?> </td>

    <td width="16%" align="center"><?=$theorigvalue?> </td>

    <td width="15%" align="center"><?=$thenewvalue?> </td>

    <td width="16%" align="center"><?=$logdate?> </td>

  </tr>

     

<?

    $x++;

  }

?>

<td width="5%" align="center" bgcolor="#000000"><input type="submit" value="DEL"></td>

    <td width="95%" align="center" colspan="3" bgcolor="#000000"><font size="2"><b><a href="#" onclick="setCheckboxes('approv', true); return false;">Select All</a>

| <a href="#" onclick="setCheckboxes('approv', false); return false;">Unselect All</a></b></font></td>

  </form>

Link to comment
Share on other sites

this is what i got after running the script again:

 

Array ( [0] => 28 [1] => 26 [2] => 27 [3] => 25 [4] => 19 ) Array ( [0] => f7d1b05822e3ad95d7bb4b617306f7e0.jpg [1] => 7610e445642b00a2ccca976e324ed276.jpg )

Warning: unlink(28) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27

 

Warning: unlink(26) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27

 

Warning: unlink(27) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27

 

Warning: unlink(25) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27

 

Warning: unlink(19) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 27

There was an error trying to delete the file.Error: Unknown column 'Array' in 'where clause'

Link to comment
Share on other sites

I am not sure why you are only pulling 2 images when you have 5 ids, but the array seems fine. implode should work on it. Why it is not I have no clue....

 

Weird stuff....I could be missing something cause I am tried, but yea. Hopefully someone can see the error and help ya.

Link to comment
Share on other sites

When i read the code i go all light headed,

 

I dont understand that code at all, very badly written.

 

i can see what your doing but using sessions so much easier to,

control your code structure.

 

all them loops are causing havoc man.

 

 

 

 

Link to comment
Share on other sites

try the other way please.

 

It definitely is not that, it is something with the implode function or how the form is being posted to the script. It is getting skewed cause the value isn't imploding properly to convert it to a string instead of an array.

Link to comment
Share on other sites

<?
if ($_GET['action'] == 'deletemulti') {
$delete = $_POST["file_name"];
$deleted_items = join(', ', $_POST["deleted_items"]);
$deleted_items1 = join(', ', $_POST["deleted_items1"]);

chdir('../userimages/');
    $do = unlink($deleted_items);
    if($do=="1"){
        echo "The file was deleted successfully.";
    } else { echo "There was an error trying to delete the file."; } 

$query = mysql_query("DELETE FROM userimg WHERE id = ($deleted_items1)");
header ("Location: approveimg.php?note=delsucmulti");
die();
}
?>

 

There :)

Link to comment
Share on other sites

tried it,

i got this:

 

Warning: unlink(3ffa3088b997d3bd87fbc1d7a21a0c2e.jpg, a35943485828735935b6fdb33b41f0fb.jpg, 83dbb2421835b36a457fc05d635f97d1.jpg) [function.unlink]: No such file or directory in /home/richspri/public_html/admin/approveimg.php on line 26

There was an error trying to delete the file.

 

and yes they do exsist before someone says anything.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.