Thursday, October 24, 2013

How to Delete(Unlink) a file from a Folder/Directory Using the file name from MySql Database Table

Hi PHP Programmers, my name is Moses, i stumble into a typical problem when i was working on a particular project and i tried to figure out how to delete a named file from a folder/Directory. After much research i was able to come out with a solution by writing out this snippet( few lines of code to delete a file from a folder). 

I will carefully comment or describe each line of code for a better understanding and if you feel link asking any question or feel link commenting please do feel free. 

<?php

// CONNECTION TO MYSQL SERVER

$hostname_conn = "localhost"; // assign server localhost to variable $hostname_conn$database_conn = "smf";       //assign assign database smf to a variable  $database_conn$username_conn = "root";       //assign server username root to a variable $username_conn$password_conn = "root";       //assign server password root to a variable $password_conn
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); // assign the full connection string path to a variable called $conn

mysql_select_db($database_conn,$conn);
$query = "SELECT reportpdf FROM bills WHERE id = '$_GET[del]'";
$result = mysql_query($query) ;

//$num=mysql_num_rows($result);
if ($result && isset($_GET['del'])){
$row = mysql_fetch_assoc($result);
$target= $row['reportpdf'];
@$pdffile = $target;
unlink($pdffile);
}
? >


Vivid Explanation of the above Code
I will start by explaining from the line of code after the connection string to MySql database server with the variable name $conn. i guess the comment above is self explanatory.

mysql_select_db($database_conn,$conn);
     This line of code send a signal to the the database   $database_conn through the connection string $conn. here the database is expose for you to  use the table there in. that means you don't have to border yourself about accessing the database with a username or password since you have done that with the connection string above.
Note (TIP):  

mysql_select_db :- This is a function that enable you to root the database(Select the database from mysql server.

$database_conn:- This is a variable already declared above now representing the database name instead of using the physical database name.

$conn:- This is a variable already declared above now representing the connection string to the mysql server(granting the user a successful connection).

$query = "SELECT reportpdf FROM bills WHERE id = '$_GET[del]'";
    This line of code is the SQL(Structure Query Language), which select the database table field reportpdf from the database table bills querying for the value of the field reportpdf using the primary key field id against a URL Parameter $_GET[del]

Note (TIP): 

$query:- This is variable assigned with the SQL statement "SELECT reportpdf FROM bills WHERE id = '$_GET[del]'"; that means instead of calling this full line of code you just make do with the variable $query that is all.


$result = mysql_query($query) ;
   This line of code execute the query using mysql_query() function
and assigned it value to a variable called $result.



if ($result && isset($_GET['del'])){
$row = mysql_fetch_assoc($result);
$target= $row['reportpdf'];
@$pdffile = $target;
unlink($pdffile);

}
  The above line of code is a conditional statement that check for for the existence of some set parameters inorder to execute the a set of instructions. for instance see below.

if ($result && isset($_GET['del'])):- This help to check of the query is true $result and the URL Parameter $_GET['del'] has a valid value then do the execute the following lines of code and delete the file from the folder.

{
$row = mysql_fetch_assoc($result);
//define a specific row in a table$target= $row['reportpdf']; // assign the reportpdf field value to a variable called $target
@$pdffile = $target; // assign $target to another variable called $pdffile
unlink($pdffile);
// unlike() function enclosing the variable $pdffile will remove the file from the folder with the name found in the reportpdf file



Note (TIP):
During the uploading of the file to the folder the folder name and the file file name was save along in the table (bills) field reportpdf like assetbills/phpdocument.pdf that is why i didn't concatenate the directory or folder with the file expected file name contained in the variable.

I guess this will help and if you benefited from this code do comment. thanks.

No comments:

Post a Comment