Jump to content

[SOLVED] Cannot unlink file


emediastudios

Recommended Posts

Hi, have a delete record code that does the job and deletes the record, im trying to unlink the images that are related to that record.

i added a code but it doesnt work.

 

Here is my delete record code

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}



if ((isset($_POST['id'])) && ($_POST['id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM news WHERE id=%s",
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_p2w, $p2w);
  $Result1 = mysql_query($deleteSQL, $p2w) or die(mysql_error());


$deleteGoTo = "news_deleted.php";
   if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];

$myFile = "../images/news/". $_POST['photo1']; 
if (!empty($_POST['photo1']))
     {

unlink($myFile);
}


}
  
  header("Location: $deleteGoTo");

}

mysql_select_db($database_p2w, $p2w);
$query_Recordset1 = "SELECT * FROM news ORDER BY id ASC";
$Recordset1 = mysql_query($query_Recordset1, $p2w) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

 

My photos are included in my form as

    <?php 

              <input name="photo1" type="hidden" id="photo1" value="<?php echo $row_Recordset1['photo1']; ?>" />
              <input name="photo2" type="hidden" id="photo2" value="<?php echo $row_Recordset1['photo1']; ?>" />
              <input name="photo3" type="hidden" id="photo3" value="<?php echo $row_Recordset1['photo1']; ?>" />

?>

 

Once i get one file to be unlinked i ll work on an array to include the 3 files.

My error is as follows

Warning: unlink(../images/news/11.jpg) [function.unlink]: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php on line 97

 

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php:97) in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php on line 103

 

Link to comment
https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/
Share on other sites

sorry

 

$array =array('photo1','photo2','photo3');
foreach($array as $val){
    $myFile = "../news/". $_POST[$val];
    if (file_exists($myFile)){
        unlink($myFile); 
    }
    else{
        //file doesnt exist
    }
}

edited...

Used this code

//or $dir = "../news/"
$array =array('photo1','photo2','photo3');
foreach($array as $val){
    $myFile = "../news/". $_POST[$val];
    if (file_exists($_POST[$val])){
        unlink($myFile); 
//or   unlink($dir.$_POST[$val]); 
       
    }
    else{
        //file doesnt exist
    }
}

and it deletes the record but no unlinking?

Were close i know :)

Sorry

Changed the code to this

$array =array('photo1','photo2','photo3');
foreach($array as $val){
    $myFile = "../news/". $_POST[$val];
    if (file_exists($myFile)){
        unlink($myFile); 
    }
    else{
        //file doesnt exist
    }

It still only unlinks photo1 only

$array =array('photo1','photo2','photo3');

foreach($array as $val){

    $myFile = "../news/". $_POST[$val];

    if (file_exists($myFile)){

        echo $_POST[$val];

        unlink($myFile);

    }

    else{

        echo ++x;

    }

 

i add echo to see if you really have those post data

print the result here

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.