Jump to content

[SOLVED] foreach problem


graham23s

Recommended Posts

Hi Guys,

 

what i'm trying to do is grab the file contents from some files that are uploaded for example:

 

     $content = file_get_contents("file/$renamed_file"); 
     $file_for_mysql = mysql_real_escape_string($content);

 

if i put this in a foreach and try to echo it out i get nothing but if i take it out the foreach i get the file contents from 1 of the files, but not them all. extra code:

 

	 foreach($_FILES['files']['name'] as $key => $value) {

 $new_nfo_name = basename($value);

     $random_number_again = rand(00000000,99999999);
     $renamed_file = ("$random_number_again.txt");

 $target_path2 = "files/";
 $target_path2 = $target_path2.$renamed_file; 

 //echo $target_path2;

 if(move_uploaded_file($_FILES['files']['tmp_name'][$key], $target_path2)) {


 }

$content = file_get_contents("file/$renamed_file"); 
     $file_for_mysql = mysql_real_escape_string($content);
     
     echo $file_for_mysql;


 } 

 

maybe my foreach was wrong, the user can specify from 2-5 how many uploads they want to do, i was wanting to be able to read the files and store all the contents into mysql, works well enough with 1 file but need to do with all the files uploaded.

 

any help would be appreciated

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/68406-solved-foreach-problem/
Share on other sites

You are using the variable "name" inside the arrays as the array parameter for foreach. You need to use just the array, like $_FILES.

foreach($_FILES as $key => $value) {
echo $value['name'];
}

 

I think the $key is just going to be an incremented integer, so it isn't really necessary.

Hi Mate,

 

tried:

 

	 foreach($_FILES['files']['name'] as $keys => $values) {

 $file = basename($values);

     $content = file_get_contents("$file"); 
     $file_for_mysql = mysql_real_escape_string($content);

     echo $file_for_mysql;
     
     }

 

this isnt retrieving anything from:

 

     $content = file_get_contents("$file"); 
     $file_for_mysql = mysql_real_escape_string($content);

 

im stumped lol

 

Graham

I believe you need to do the following:

 

foreach ($_FILES['files'] AS $key=>$val) {
  echo $key . ': ' . $val;
}

 

I believe that should output all the file information you're looking for; name, size, and type.

 

Let me know if that doesn't work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.