Hello,

I am trying to delete a file using the unlink function from my website, and database.

MySQL works properly at removing the input in my database using:

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

  mysql_select_db($database_Creative, $Creative);
  $Result1 = mysql_query($deleteSQL, $Creative) or die(mysql_error());

  $deleteGoTo = "content-files.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

So it deletes from my database properly, but doesn't delete my file from the server.
( The database field for my file is: 'File' )

So in basic cade I want to implement this:

$tmpfile = "../uploads/files/Jim.txt";
unlink($tmpfile);

So basically I need the two above codes fused together but I cannot seem to get them to work together to delete both the input in my database, and the file on the server.

Thank you for your help in advance!

I tried this but it didn't work:

$target_path = "../uploads/files/";
	$tmpfile = $target_path . basename($_FILES['File']['name']);
	unlink($tmpfile);
    $image_name = "Jim.txt";
    	$tmpfile = "../uploads/files/".$image_name;
    	unlink($tmpfile);

    How can I get $image_name = to be the input 'File' in my above query.

      creativeink wrote:

      How can I get $image_name = to be the input 'File' in my above query.

      There is no 'File' input anywhere you've shown us... did you perhaps mean the DB column 'File' instead? If so, do a SELECT query to retrieve the File field from the database before you DELETE that row.

        Something like this:

        	
        $filequery = mysql_query("SELECT * FROM files WHERE File = %s");
        $row = mysql_fetch_array($filequery);
        $image_name = "".$row['File']."";
        $tmpfile = "../uploads/files/".$image_name;
        unlink($tmpfile);
        

        Doesn;t work either but I feel I am getting closer.

        But YES 'File' is the Db column.

          Above you're searching the "id" field based on the value of $_GET['id']. Why are you now searching the 'File' field? Also, that '%s' is wrong (since it's going to be a literal percent sign followed by the letter 's') if you're not using [man]sprintf/man.

            $id = intval($_GET['id']);
            $sql="SELECT File FROM files WHERE id='$id'";
            $result=mysql_query($sql);
            $file = "../uploads/files/[$result]";
            unlink($file);
            

            Something more like that ?

              Yes and no... now you've got square brackets in your file path as well as trying to use the return value of [man]mysql_query/man as if it contained data (when it's really a resource - you still need to use [man]mysql_result/man or one of the "fetch" functions to retrieve the result).

                $id = intval($_GET['id']);
                $sql="SELECT File FROM files WHERE id='$id'";
                $result=mysql_query($sql);
                $rows=mysql_fetch_array($result);
                $target='../uploads/files/';
                $file=$rows['File'];
                $path="$target$file";
                unlink($path);
                

                Still doesn't wanna work

                  What is the value of $path after you set it just above the unlink() command?

                    Well I got that last one from this page that you actually commented on:
                    http://phpbuilder.com/board/showthread.php?t=10338804&highlight=unlink

                    Basically speaking this works statically:

                    $image_name = "Document.txt"; 
                    $tmpfile = "../uploads/files/".$image_name; 
                     unlink($tmpfile); 
                    

                    But I want the $image_name to be equal to the 'File' database column that I am carrying from my original query.

                    if ((isset($_GET['id'])) && ($_GET['id'] != "")) { 
                      $deleteSQL = sprintf("DELETE FROM files WHERE id=%s", 
                                           GetSQLValueString($_GET['id'], "int")); 
                    
                      mysql_select_db($database_Creative, $Creative); 
                      $Result1 = mysql_query($deleteSQL, $Creative) or die(mysql_error()); 
                    
                      $deleteGoTo = "content-files.php"; 
                      if (isset($_SERVER['QUERY_STRING'])) { 
                        $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; 
                        $deleteGoTo .= $_SERVER['QUERY_STRING']; 
                      } 
                      header(sprintf("Location: %s", $deleteGoTo)); 
                    } 
                    

                    I really appreciate your help BTW

                      What I meant was can you echo out $path just before the unlink() statement and see what path it ends up with?

                      Also, if it's not working, do you have display_errors set to On and error_reporting set to E_ALL? If so, are you getting an error message?

                        No errors are displaying

                        This query erases the input from my database, and erases Document.txt as a test.

                        However I want '$image_name' to be dynamic, carrying the filename from the query. Document.txt is static right now, and I need a piece of code that enters the filename text there instead, dynamically.

                        $deleteSQL = sprintf("DELETE FROM files WHERE id=%s",
                                               GetSQLValueString($_GET['id'], "int"));
                        
                        $image_name = Document.txt;
                        	$tmpfile = "../uploads/files/".$image_name;
                        	unlink($tmpfile);
                        
                          mysql_select_db($database_Creative, $Creative);
                          $Result1 = mysql_query($deleteSQL, $Creative) or die(mysql_error());
                        
                          $deleteGoTo = "content-files.php";
                          if (isset($_SERVER['QUERY_STRING'])) {
                            $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
                            $deleteGoTo .= $_SERVER['QUERY_STRING'];
                          }
                          header(sprintf("Location: %s", $deleteGoTo));
                        }
                        

                        Thanks

                          Write a Reply...