Tuesday 2 September 2008

Deleting unwanted images of the server.

What I wanted to do was to write a script that after loading an image would then check the image folder for any unwanted image, which would then be deleted.

So first of all I wanted to get a list of files in the folder - for this we need to use readdir - see http://uk3.php.net/manual/en/function.readdir.php

function list_folder($folder){

$dh = opendir($folder);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
rsort($files);
return $files;

}


////////////////////////////////////

//Then i needed to make an array of a list of images that I do want to keep.

//my list looks a bit like this

$images_wanted_array[0] = $row['img1'];
$images_wanted_array[1] = 'thum'.$row['img1'];

/* once have these two list then I run a foreach inside a foreach statement.
So each file gets checked against the list of wanted files. If theres a match I get the result '$success'. After checking the whole wanted list I do a check for any succesful matches and only delete if there aren't any.
Here's the function I wrote.
*/

function deleting_unwanted_images($images_array, $wanted_array, $path){ // this function takes an array of images and they aren't on the wanted_array list they will be deleted.

foreach ($images_array as $f) {
unset($success);
foreach ($wanted_array as $i){
if ($f == $i){
$success = TRUE;
}
}
if (!$success){
$file_to_delete = $path.$f;
unlink($file_to_delete);
echo '
The file'.$f.' - has been deleted
';
}else {
echo '
There has been a match so we are going to keep the file '.$f.'
';
}

}

Monday 1 September 2008

importing a database .csv to mysql

** this post also included a solution for Invalid query: Duplicate entry '127' for key errors **

The mission -> I have databases given to me in .csv format created in Microsoft Excel and I would like to import them into my mysql database so I can manipulate them from there.

The Solution -> the key to writing the solution for this can be found by using http://uk3.php.net/fgetcsv .

here's the code I wrote from this page. You will need to create your table first that we write to.

#############################################################################

$row = 1;
$handle = fopen ("database.csv","r");
// ip-to-country.csv must be in the same directory with this php file
// customer number here will represent the row it's on database given.

while ($data = fgetcsv ($handle, 1000, ",")) {
$query = "INSERT INTO database_table(`id`, `field1`, `field2`, `field3`)

VALUES('', '".$data[0]."', '".$data[1]."', '".$data[2]."')";
$result = mysql_query($query) or die("Invalid query: " . mysql_error().__LINE__.__FILE__);
$row++;
}
fclose ($handle);

############################################################################


The only error I came across was
Invalid query: Duplicate entry '127'

and this was down to database table I had created as I had the 'id' field set to TINYINT(3) - the resolution was to change this to INT(6)