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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.