graham23s Posted September 8, 2007 Share Posted September 8, 2007 Hi Guys, i'm having a bit of bother with this code what im trying to do is read a file associated with an upload then store the contents into mysql, when i echo out the file contents they are all fine 3 seperate entries but when i store the data into mysql its the exact same data 3 times. code is: foreach($_FILES['nfo']['name'] as $key => $value) { $new_nfo_name = basename($value); $random_number_again = rand(00000000,99999999); $renamed_nfo = ("$random_number_again.nfo"); $target_path2 = "nfos/"; $target_path2 = $target_path2.$renamed_nfo; if(move_uploaded_file($_FILES['nfo']['tmp_name'][$key], $target_path2)) { $content = file_get_contents("nfos/$renamed_nfo"); $nfo_for_mysql = mysql_real_escape_string($content); echo $nfo_for_mysql; echo ("<br />"); echo ("<hr />"); echo ("<hr />"); } } any help would be appreciated Graham Quote Link to comment https://forums.phpfreaks.com/topic/68459-problem-with-get_file_contents/ Share on other sites More sharing options...
recklessgeneral Posted September 8, 2007 Share Posted September 8, 2007 Hi Graham, Could you post the code that inserts the data into the database as, judging from your description, that's where the problem lies? Thanks, Darren. Quote Link to comment https://forums.phpfreaks.com/topic/68459-problem-with-get_file_contents/#findComment-344181 Share on other sites More sharing options...
graham23s Posted September 8, 2007 Author Share Posted September 8, 2007 Hi Mate, i basically used the variable: $nfo_for_mysql to store the entry into mysql but what i thinks happening is, the foreach loop is calling the function 3 times or the number of arrays in the file, not sure how to get the other entries added though. cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/68459-problem-with-get_file_contents/#findComment-344185 Share on other sites More sharing options...
d22552000 Posted September 8, 2007 Share Posted September 8, 2007 (place the code plex so we can help u) Quote Link to comment https://forums.phpfreaks.com/topic/68459-problem-with-get_file_contents/#findComment-344187 Share on other sites More sharing options...
graham23s Posted September 8, 2007 Author Share Posted September 8, 2007 no probs here we are: ## is the isset active ############################################################# if(isset($_POST['submit'])) { $filename = $_FILES['nzb']['name']; ## empty ### if(empty($_FILES['nzb']['name'][0])) { stderr("Error","That was a blank submission."); include("includes/footer.php"); exit; } foreach($_FILES['nzb']['name'] as $K => $V) { ## vars ############################################################################ $poster = $_POST['hidden_id']; $cat_id = $_POST['cat_id']; $nzb_size = $_FILES['nzb']['size']; $nzbs = $_POST['nzbs_needed']; $filename = basename($V); $file_name = str_replace(".nzb"," ", $filename); ## Rename the nzb with random digits ############################################### $random_number = rand(00000000,99999999); $renamed_nzb = ("$random_number.nzb"); $target_path = "nzbs/"; $target_path = $target_path.$renamed_nzb; if(move_uploaded_file($_FILES['nzb']['tmp_name'][$K], $target_path)) { //stderr("Multi-Uploader Successfull","The NZB named <b>".$renamed_nzb."</b> have been uploaded successfully"); } #################################################################################### #################################################################################### //foreach ($_FILES as $keys => $vals) { //echo $keys . ': ' . $vals; //$nfo_file = basename($values); //$content = file_get_contents("$nfo_file"); //$nfo_for_mysql = mysql_real_escape_string($content); //echo $nfo_for_mysql; //} #################################################################################### #################################################################################### foreach($_FILES['nfo']['name'] as $key => $value) { $new_nfo_name = basename($value); $random_number_again = rand(00000000,99999999); $renamed_nfo = ("$random_number_again.nfo"); $target_path2 = "nfos/"; $target_path2 = $target_path2.$renamed_nfo; if(move_uploaded_file($_FILES['nfo']['tmp_name'][$key], $target_path2)) { $content = file_get_contents("nfos/$renamed_nfo"); $nfo_for_mysql = mysql_real_escape_string($content); echo $nfo_for_mysql; echo ("<br />"); echo ("<hr />"); echo ("<hr />"); } } ## strip the .nzb ################################################################## ## strip the .nzb ################################################################## ## nfo ############################################################################# //=====================================================================================// // NZB/XML Code... BETA //=====================================================================================// $filecontent = file_get_contents("nzbs/$renamed_nzb"); $xml = simplexml_load_string($filecontent); $nzbpiece = 0; foreach($xml->{"file"} as $nfile) { $nposter = (string) trim($nfile['poster']); $nposter = ($nposter); $ndate = 0 + trim($nfile['date']); $nsubject = (string) trim($nfile['subject']); // To find num of segments in subject: $nsubjsegs = 0 + subj_seg($nsubject); // To find out if Par or not $npar = (stristr($nsubject, "par2")?0:1); $nsubject = ($nsubject); $groups = array(); foreach($nfile->groups->group as $group) { $groups[] = (string) trim($group); } $ngroups = (serialize($groups)); $size = 0; foreach($nfile->segments->segment as $segment) $size += $segment['bytes']; } preg_match_all('#bytes="\d{1,6}"#i', $xml, $out); $bytes = (sizeof($out[0])-1) * 259072 + $out[0][sizeof($out[0])]; //echo $bytes; $matchesfound = preg_match_all('/subject="/', $filecontent, $matchfound); ## strtolower the files incase some par2's are uppercase $parsfound = preg_match_all("/.par2/", strtolower($filecontent), $parfind); ## final check for size ########################################################### //$filecontent = file_get_contents("filename.nzb"); //$xml = simplexml_load_string($filecontent); preg_match_all('#bytes="\d{1,15}"#i', $filecontent, $out); // use previously posted preg_match_all $search = array('bytes=','"'); // array of things to search for $sizes = str_replace($search, "", $out[0]); // strip off any things in the $search array $sum = array_sum($sizes); // get the sum $propersize = formatbytes($sum); //=====================================================================================// // Easy way to count number of files preg_match all the subject=" and e-voila //=====================================================================================// ## do some time calculations ... ################################################## $time = $ndate; $date = date("Y-m-d\TH:i:s",$time); //=====================================================================================// // NZB/XML Code... BETA //=====================================================================================// //=====================================================================================// // Search text //=====================================================================================// $search = array('.','-','_'); $search_txt = str_replace($search," ",$file_name); //=====================================================================================// // DUPE ALERT //=====================================================================================// $query_dupe = "SELECT `file_name`,`ng_subject` FROM `uploaded_nzbs` WHERE `ng_subject`='$nsubject'"; $result_dupe = mysql_query($query_dupe); $dupe_check = mysql_num_rows($result_dupe); if(($dupe_check) > 0) { stderr("Dupe Alert","Sorry, this file appears to be a dupe! it's already in the database."); include("includes/footer.php"); ## delete the dupe file unlink("nzbs/$renamed_nzb"); exit; } //================================================================================// // Insertion... //================================================================================// $q = "INSERT INTO `uploaded_nzbs` (`id`,`poster_id`,`file_name`,`nzb_file`,`nzb_size`,`cat_id`,`nfo`,`nfo_name`,`date_added`,`search_text`,`ng_poster`,`ng`,`ng_subject`,`ng_size`,`ng_date`,`ng_pars`,`ng_files`) VALUES ('','$poster','$file_name','$renamed_nzb','$new_nzb_size','$cat_id','$nfo_for_mysql','$renamed_nfo',now(),'$search_txt','$nposter','$group','$nsubject','$propersize','$date','$parsfound','$matchesfound')"; $r = mysql_query($q) or die (mysql_error()); //================================================================================// // Insertion... //================================================================================// } Graham Quote Link to comment https://forums.phpfreaks.com/topic/68459-problem-with-get_file_contents/#findComment-344190 Share on other sites More sharing options...
d22552000 Posted September 8, 2007 Share Posted September 8, 2007 one comment about your insert, holy sh*t your databse willg et big quickly, if someone uploads a 1byte text file the table itself will be a few kbytes because of all those values... got the column that holds the file set to binary or blob? (important) Quote Link to comment https://forums.phpfreaks.com/topic/68459-problem-with-get_file_contents/#findComment-344191 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.