-1

I have database that stores the name of the file in a column "name" and the extension in "ext" column

I need to know how to get the data and merge the filename(.)ext into array and then using,

$files = array('some-file.php', 'some-file2.php');

foreach ($files as $file) {
  unlink($file);
}

To delete the files

My code is :

<?php

  include('../dbConfig.php');
   $data = mysql_query("SELECT * FROM snaps") or die(mysql_error());
   $row = mysql_fetch_array($data);

   $files[] = $row['name'].'.'.$row['ext'];
   foreach ($files as $file) {
       unlink($file);
   }

 ?>
ahmedalimody
  • 48
  • 1
  • 6
  • You run the sql query to fetch said files, then you unlink the absolute path.... – Darren Nov 25 '14 at 23:15
  • Erm, concat? In php something like `$row['name'].'.'.$row['ext']`, in mysql `CONCAT(name,'.',ext)`, take your pick... – Wrikken Nov 25 '14 at 23:15
  • @Wrikken I know that but Can you give me the correct sql code – ahmedalimody Nov 25 '14 at 23:16
  • @Wrikken Can you give me the php code that will enter the filename.ext into array as far I executed the first row only :( – ahmedalimody Nov 25 '14 at 23:25
  • Erm... is `$files[] = $row['name'].'.'.$row['ext'];` what you're looking for? – Wrikken Nov 25 '14 at 23:26
  • @Wrikken please take a look at my update – ahmedalimody Nov 25 '14 at 23:38
  • Hopefully those file names aren't from arbitrary user input. – Ryan Nov 25 '14 at 23:38
  • @self the file names is just numbers, I renaming all files to be random numbers. – ahmedalimody Nov 25 '14 at 23:41
  • 1
    If you're just learning PHP, please, do not learn the obsolete `mysql_query` interface. It's awful and is being removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). A guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. Always be absolutely **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will have severe [SQL injection bugs](http://bobby-tables.com/). – tadman Nov 25 '14 at 23:41
  • _it is awful_ Wow @tadman that is a bold statement. I'm sure you were grateful when it was the only API around. – Ryan Nov 25 '14 at 23:43
  • @self: when it was the only option around, it was not a matter of being grateful but dealing with it. We hadn't objects or classes either, or even `foreach`... Doesn't mean I don't say something if I see someone doing `reset($array); while(list($row)=each($array)){..` – Wrikken Nov 25 '14 at 23:50
  • @self Come on. It was never good. It was barely more than the C library rolled into PHP. There's options now, so let's use them. – tadman Nov 25 '14 at 23:50

1 Answers1

0

Like this?

$Q = "SELECT CONCAT(name, '.', ext) as file_name FROM table;
Wojtek B
  • 160
  • 1
  • 12