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.

Tuesday, October 22, 2013

How to re-enable JSP support in Dreamweaver and add the latest MySQL Connector/J.


Although I don’t really use Adobe Dreamweaver for web development (although if i’m doing interface design where I need a quick way to alter and view CSS changes fast I do) a friend of mine the other day (who uses Dreamweaver heavily) wanted to know how to re-enable the old JSP (Java Server Page) support in Adobe Dreamweaver CS 5 as Adobe removed the JSP support in Dreamweaver CS4 (I believe, may be a slightly older version they removed it however.)
A couple of months ago I developed a secure members system for him in Java (JSP’s and some Java Beans too) using my favourite IDE… NetBeans, he  however wanted to make some visual changes to the JSP files and likes to use the ‘Live View’ mode in Adobe Dreamweaver CS5, so I found out how to re-enable this functionality (although not officially supported by Adobe) and so thought I’d share it here and also explain how to add the latest MySQL J/Connector to be able to work in Dreamweaver as he needed that for the ‘Live View’ too.
To re-enable ‘JSP support’ in Adobe Dreamweaver
  • Open up the Adobe Extentsion Manager
  • Click on ‘Install‘ (to install a new plugin)
  • Browse to and select the following file: “C:\Program Files (x86)\Adobe\Adobe Dreamweaver CSx\configuration\DisabledFeatures\JSP_Support.mxp” and ‘Install‘ that extension.
  • …your done, open Adobe Dreamweaver and your good to go!
To add the MySQL J/Connector for Adobe Dreamweaver
Unlike running your Java web application on Tomcat or another Java application server such as GlassFish etc. Dreamweaver looks in a certain place in the Dreamweaver folder to get its plugin’s to work within Dreamweaver, so when developing with Dreamweaver and JSP and if you rely on a MySQL database (or any other Java libraries for example the IBM DB2 database driver etc ) you’ll need to add these to the folder ‘JDBCPlugins’ in the Dreamweaver folder on your system otherwise you’ll probably see an error message like so:-
“org/gjt/mm/mysql/Driver” class not found
So lets begin…
  • Firstly download the latest MySQL Connector/J from MySQL here: (http://www.mysql.com/downloads/connector/j/), at the time of writing I downloaded (mysql-connector-java-5.1.18.zip)
  • Extract the JAR (Java archive) file named ‘mysql-connector-java-5.1.18-bin‘ from the downloaded archive to “{Dreamweaver Folder}\Configuration\JDBCDrivers\“.
  • Re-open Adobe Dreamweaver and you should now be able to use the MySQL JDBC driver in Adobe Dreamweaver.
I hope this has been helpful to some peopl.
Do Leave a comment if this helped you out....