0

I had written PHP loong time ago. I wrote this simple script but I am getting this error below. I have also added the code. Please help me to find my mistake.

Parse error: syntax error, unexpected '"book.txt"' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\test\index.php on line 11

<?php

print('<pre>');
if(!isset($_FILES['userfile'])){
    $fname="book.txt";
    $arrayk = explode("\n", file_get_contents($fname));
    $num=array_rand($arrayk,1);
    print($arrayk[$num]."</br>");
}else{
    $uploadedfile = "book.txt"; //this line is giving an error.
    $extensions= array("txt");
          

    $file_type=$_FILES['userfile']['type'];
    if(in_array($file_ext,$extensions)=== false){
         print("extension not allowed, please choose a txt file.");
    }else{
        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadedfile)) {
            print("File is valid, and was successfully uploaded.\n");
        } else {
            print("Possible file upload attack!\n");
        }  
    }

    print("Here is some more debugging info:");
    print_r($_FILES);
}
print("</pre>");

?>
<!DOCTYPE html>
<html>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="index.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
</html>

I tried the code I have written. I am guessing there is a problem with my PHP

Nico Haase
  • 11,420
  • 35
  • 43
  • 69

1 Answers1

1

Your code looks correct at the offensive line but I assume you copied it from somewhere and that's why you also copied some invisible garbage¹ characters as well (see these squares):

enter image description here

You got more of these characters below that line as well:

enter image description here

so you need to get yourself something more advanced than Notepad and clean the code. Once you do that you should be good.

¹) Actually it's not a garbage, but UTF-8 U+00A0 (0xC2, 0x0A) which is no-break space which is harmless in string literals, yet not as PHP code element separator.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141