LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2011, 06:28 AM   #1
razzera
Member
 
Registered: Dec 2009
Posts: 105

Rep: Reputation: 15
mysql / delete on server question


Hello

I have a question that i hope someone can help me with. Im running a debian server with Apache2 and Mysql.

I wonder if it is possible to delete stuff of a folder in my webserver if it is not in a certain collumn in a table in my mysql database?

for example, let's say that in /home/user/public_html/images/ i have an image called 4520As.jpg, is it possible to check this against a collumn called "name" in the table "images" in my mysql database and if the name of the image is not in that collumn of the database it will get deleted ?
 
Old 06-19-2011, 06:47 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Code:
$database = "db";
$user = "usr";
$host = "host";
$password="passwd";
$query=" SELECT name FROM images";

$keep_images = array();
$image_list = array();

$link = @mysql_connect($host, $user, $password, true);
$result=mysql_query($query);
$number=mysql_numrows($result);
$i=0;
echo "<pre>";
while ($i < $number) {
    $keep_images[] = mysql_result($result,$i,"name");
    $i++;
}
mysql_close();

$myDirectory = opendir("/home/user/public_html/images/");
while($entryName = readdir($myDirectory)) {
    $image_list[] = $entryName;
}
closedir($myDirectory);

foreach ($image_list as $image){
    if(!in_array($image,$keep_images)){
        echo "Would delete /home/user/public_html/images/" . $image . "<br/>";
        //unlink("/home/user/public_html/images/" . $image);
    }
}
echo "</pre>";
Something similar to this should work. It's completely untested, hacked together from a couple of PHP scripts I had written for my work's website. I've set it up so it doesn't delete anything, only tells you which ones it would try to delete (for obvious reasons); if you want it to do actual deleting then uncomment the 'unlink()' command

Hope this helps,
 
Old 06-19-2011, 07:28 AM   #3
razzera
Member
 
Registered: Dec 2009
Posts: 105

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Snark1994 View Post
Code:
$database = "db";
$user = "usr";
$host = "host";
$password="passwd";
$query=" SELECT name FROM images";

$keep_images = array();
$image_list = array();

$link = @mysql_connect($host, $user, $password, true);
$result=mysql_query($query);
$number=mysql_numrows($result);
$i=0;
echo "<pre>";
while ($i < $number) {
    $keep_images[] = mysql_result($result,$i,"name");
    $i++;
}
mysql_close();

$myDirectory = opendir("/home/user/public_html/images/");
while($entryName = readdir($myDirectory)) {
    $image_list[] = $entryName;
}
closedir($myDirectory);

foreach ($image_list as $image){
    if(!in_array($image,$keep_images)){
        echo "Would delete /home/user/public_html/images/" . $image . "<br/>";
        //unlink("/home/user/public_html/images/" . $image);
    }
}
echo "</pre>";
Something similar to this should work. It's completely untested, hacked together from a couple of PHP scripts I had written for my work's website. I've set it up so it doesn't delete anything, only tells you which ones it would try to delete (for obvious reasons); if you want it to do actual deleting then uncomment the 'unlink()' command

Hope this helps,
Worked perfectly, thanks you very very much! is it possible to reverse his or add a line so that if there is a database entry for an image that do not exist the entry is removed from the database ?

Last edited by razzera; 06-19-2011 at 07:41 AM.
 
Old 06-20-2011, 09:50 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Code:
$database = "db";
$user = "usr";
$host = "host";
$password="passwd";
$query=" SELECT name FROM images";

$keep_images = array();
$image_list = array();

$link = @mysql_connect($host, $user, $password, true);
$result=mysql_query($query);
$number=mysql_numrows($result);
$i=0;
echo "<pre>";
while ($i < $number) {
    $image_list[] = mysql_result($result,$i,"name");
    $i++;
}

$myDirectory = opendir("/home/user/public_html/images/");
while($entryName = readdir($myDirectory)) {
    $keep_images[] = $entryName;
}
closedir($myDirectory);

foreach ($image_list as $image){
    if(!in_array($image,$keep_images)){
        $query="DELETE FROM images WHERE name='" . $image . "15'";
        echo "Would run:    " . $query . "<br/>";
        //mysql_query($query);
    }
}
mysql_close();
echo "</pre>";
It's pretty much the same as before - I've just swapped around the two arrays for keeping and listing images, and the code to delete items (so it deletes the database entry, not the file)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mysql-server-5.0 question (squeeze) radiodee1 Debian 3 09-30-2009 09:53 AM
Mysql: Is it OK to delete ibdata1 under /var/lib/mysql ? Kunsheng Linux - Software 8 09-26-2009 09:28 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. SpellChainz Linux - Newbie 1 06-23-2007 03:35 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 08:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration