meks
meks

Reputation: 807

unexpected '' (T_ENCAPSED_AND_WHITESPACE) while output

I'm sorry if my question may appear silly, but I'm really confused with an error I'm getting while outputting a file name from array.

First I'm populating array with a column names from my database. Here is the code:

    $AddNews = "SELECT * FROM picture_names";
$result = mysql_query( $AddNews, $IVE_Connection ) or die('ERROR: '.mysql_error());
for($i = 0; $i < mysql_num_fields($result); $i++){
    $field_info = mysql_fetch_field($result, $i);
    $name_array[] = $field_info->name;
}

    for($i = 0; $i < sizeof($name_array); $i++){
    echo "<input type='file' name='".$name_array[$i]."'    id='".$name_array[$i]."' value=''>";
    echo "</span><br />";
    echo "</div>";
}

There is no problem in that stage. But when I'm trying. I can Output in normally. The problem starts when i try to print condition with the same array:

for($i = 0; $i < sizeof($name_array); $i++){
echo "if($_FILES['".$name_array[$i]."']['tmp_name'] != ''){";// the problem ocures here
   ...some code here
echo "}";
}

here I always have a mistake unexpected '' (T_ENCAPSED_AND_WHITESPACE) Itried to stringify it but still having the same error. if I output just an array outside of $_FILES logic there is no problem.

I would be grateful if someone can point me what am I doing wrong.

Upvotes: 1

Views: 80

Answers (2)

Moyed Ansari
Moyed Ansari

Reputation: 8461

Try this

for($i = 0; $i < sizeof($name_array); $i++){

  if(isset($_FILES[$name_array[$i]]['tmp_name'])){
      //your code
  }
}

Upvotes: 1

Code Lღver
Code Lღver

Reputation: 15593

This is not the correct code:

for($i = 0; $i < sizeof($name_array); $i++){
echo "if($_FILES['".$name_array[$i]."']['tmp_name'] != ''){";// the problem ocures here
   ...some code here
echo "}";
}

Just remove the echo form the code

for($i = 0; $i < sizeof($name_array); $i++){
  if($_FILES[$name_array[$i]]['tmp_name'] != ''){
     echo "Text";
  };
}

Upvotes: 0

Related Questions