Jump to content

Problem with get_file_contents


graham23s

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.