gdfhghjdfghgfhf Posted April 20, 2008 Share Posted April 20, 2008 Ok here is a little script i use to rename multiple images files and store their names inside database It will look for all files in a folder, and output the list then it will rename each files ...so far everything is good... but then i want to add each of my files inside database my problem is that only the last item of the array will be added.... <?php /** * This funtion will take a pattern and a folder as the argument and go thru it(recursivly if needed)and return the list of * all files in that folder. * Link : http://www.bin-co.com/php/scripts/filesystem/ls/ * Arguments : $pattern - The pattern to look out for [OPTIONAL] * $folder - The path of the directory of which's directory list you want [OPTIONAL] * $recursivly - The funtion will traverse the folder tree recursivly if this is true. Defaults to false. [OPTIONAL] * $options - An array of values 'return_files' or 'return_folders' or both * Returns : A flat list with the path of all the files(no folders) that matches the condition given. */ function ls($pattern="*", $folder="", $recursivly=false, $options=array('return_files','return_folders')) { $current_folder = getcwd(); if($folder) { $folder = preg_replace('#([\\/]){2,}#', '$1', $folder); if(in_array('quiet', $options)) { // If quiet is on, we will suppress the 'no such folder' error if(!file_exists($folder)) return array(); } if(!chdir($folder)) return array(); } $get_files = in_array('return_files', $options); $get_folders= in_array('return_folders', $options); $both = array(); // Get the all files and folders in the given directory. if($get_files) $both = glob($pattern, GLOB_BRACE + GLOB_MARK); if($recursivly or $get_folders) $folders = glob("*", GLOB_ONLYDIR + GLOB_MARK); //If a pattern is specified, make sure even the folders match that pattern. $matching_folders = array(); if($pattern !== '*') $matching_folders = glob($pattern, GLOB_ONLYDIR + GLOB_MARK); chdir($current_folder); // Necessary incase of relative filepaths //Get just the files by removing the folders from the list of all files. $all = array_values(array_diff($both,$folders)); if($recursivly or $get_folders) { foreach ($folders as $this_folder) { if($get_folders) { //If a pattern is specified, make sure even the folders match that pattern. if($pattern !== '*') { if(in_array($this_folder, $matching_folders)) array_push($all, $this_folder); } else array_push($all, $this_folder); } if($recursivly) { // Continue calling this function for all the folders $new_folder = "$folder/$this_folder"; if(!$folder) $new_folder = $this_folder; $deep_items = ls($pattern, $new_folder, $recursivly, $options); # :RECURSION: foreach ($deep_items as $item) { array_push($all, $this_folder . $item); } } } } return $all; } // -------------------------------------------------------------------------------------- $opt[0]="return_files"; $origine = $_GET['origine']; $tracks=ls ("*.{gif,jpg}","images/$origine",true,$opt); // print_r($tracks); // echo "<br><br><br><br>"; foreach ($tracks as $option) { $id = $_GET['id']; include("db.php"); $query = "SELECT title from news2 WHERE id='$id'"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $band = $row["title"]; } $id = $_GET['id']; rename("/home/qcanarco/public_html/db/img/images/$origine/$option", "/home/qcanarco/public_html/db/img/images/$origine/id".$id."_".$band."_$option"); echo $option; echo " renommé."; echo "<br>"; $ki = $_GET['ki']; $vraidate = date('d/m/y'); $imagename = "id".$id."_".$band."_$option"; include("db.php"); $result=MYSQL_QUERY("INSERT INTO bands_pics (id,bandid,url,date,infos,valide,user)". "VALUES ('NULL', '$id', '$imagename', '$vraidate','$option','1','$ki')"); ) ?> could anyone help me debug my script? thanks edit: nvm, now the result is just totally blank.... my script is really messed up Quote Link to comment Share on other sites More sharing options...
gdfhghjdfghgfhf Posted April 20, 2008 Author Share Posted April 20, 2008 Ok here is the correct code: <?php /** * This funtion will take a pattern and a folder as the argument and go thru it(recursivly if needed)and return the list of * all files in that folder. * Link : http://www.bin-co.com/php/scripts/filesystem/ls/ * Arguments : $pattern - The pattern to look out for [OPTIONAL] * $folder - The path of the directory of which's directory list you want [OPTIONAL] * $recursivly - The funtion will traverse the folder tree recursivly if this is true. Defaults to false. [OPTIONAL] * $options - An array of values 'return_files' or 'return_folders' or both * Returns : A flat list with the path of all the files(no folders) that matches the condition given. */ function ls($pattern="*", $folder="", $recursivly=false, $options=array('return_files','return_folders')) { $current_folder = getcwd(); if($folder) { $folder = preg_replace('#([\\/]){2,}#', '$1', $folder); if(in_array('quiet', $options)) { // If quiet is on, we will suppress the 'no such folder' error if(!file_exists($folder)) return array(); } if(!chdir($folder)) return array(); } $get_files = in_array('return_files', $options); $get_folders= in_array('return_folders', $options); $both = array(); // Get the all files and folders in the given directory. if($get_files) $both = glob($pattern, GLOB_BRACE + GLOB_MARK); if($recursivly or $get_folders) $folders = glob("*", GLOB_ONLYDIR + GLOB_MARK); //If a pattern is specified, make sure even the folders match that pattern. $matching_folders = array(); if($pattern !== '*') $matching_folders = glob($pattern, GLOB_ONLYDIR + GLOB_MARK); chdir($current_folder); // Necessary incase of relative filepaths //Get just the files by removing the folders from the list of all files. $all = array_values(array_diff($both,$folders)); if($recursivly or $get_folders) { foreach ($folders as $this_folder) { if($get_folders) { //If a pattern is specified, make sure even the folders match that pattern. if($pattern !== '*') { if(in_array($this_folder, $matching_folders)) array_push($all, $this_folder); } else array_push($all, $this_folder); } if($recursivly) { // Continue calling this function for all the folders $new_folder = "$folder/$this_folder"; if(!$folder) $new_folder = $this_folder; $deep_items = ls($pattern, $new_folder, $recursivly, $options); # :RECURSION: foreach ($deep_items as $item) { array_push($all, $this_folder . $item); } } } } return $all; } $opt[0]="return_files"; $origine = $_GET['origine']; $tracks=ls ("*.{gif,jpg}","images/$origine",true,$opt); // print_r($tracks); // echo "<br><br><br><br>"; foreach ($tracks as $option) { $id = $_GET['id']; include("db.php"); $query = "SELECT title from news2 WHERE id='$id'"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $band = $row["title"]; } $id = $_GET['id']; rename("/home/qcanarco/public_html/db/img/images/$origine/$option", "/home/qcanarco/public_html/db/img/images/$origine/id".$id."_".$band."_$option"); echo $option; echo " renommé."; echo "<br>"; } $ki = $_GET['ki']; $vraidate = date('d/m/y'); $imagename = "id".$id."_".$band."_$option"; // $result=MYSQL_QUERY("INSERT INTO bands_pics (id,bandid,url,date,infos,valide,user)". // "VALUES ('NULL', '123', '123', '123','123','123','123')"); include("db.php"); $result=MYSQL_QUERY("INSERT INTO bands_pics (id,bandid,url,date,infos,valide,user)". "VALUES ('NULL', '$id', '$imagename', '$vraidate','$option','1','$ki')"); ?> Like i said, everything is working except it only adds the last item of the array instead of the whole array (1 row in db per array item) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.