List Files by Extension - PHP

Asked

Viewed 110 times

1

Good afternoon,

I’m trying to list the files in a folder. After running Cod. below, the error occurs:

Parse error: syntax error, Unexpected 'as' (T_AS), expecting ')' in C: xampp htdocs 01-Signer testAssinador.php on line 23

Personally, I can’t see where the mistake is. Could someone help me? Thank you!

$_path = 'C:\xampp\htdocs\01-Signer\Fev19';
if(is_dir($_path)){

  foreach(glob("$_path\*.txt") as $arquivo){
   echo "Nome do Arquivo: <a href='$arquivo'>$arquivo</a><br />";
  }
 }else{
  
  echo 'Não existe arquivo com extensão pdf.';
 }
  • 1

    The result of glob("$_path\*.txt") you have to put into a variable, and use that variable in the foreach

  • 1

    @Edwardramos Why you need to put in a variable?

  • And what I saw in that question https://stackoverflow.com/questions/47805963/using-glob-to-get-txt-and-png-files-only-folder?noredirect=1&lq=1

  • 2

    @Edwardramos The question is php. Python and PHP are different.

  • I put the glob("$_path*.txt") in a variable, but it still gives error (Parse error: syntax error, Unexpected 'as' (T_AS), expecting ')' in C: xampp htdocs 01-Signer testAssinator.php on line 26)

  • @Renata https://www.php.net/manual/en/function.glob.php e https://www.electrictoolbox.com/php-glob-find-files/

  • 4

    @Renata Your code is correct, but it seems that there are invisible characters interfering with your file. Review how you’re writing the code or take care of where you copy and paste. I rewrote your code (https://repl.it/@acwoss/Allblushingratio) and it worked perfectly as expected.

  • 6

    @Edwardramos Wanting to help is very good, don’t get me wrong, but helping by making baseless random guesses tend to get in the way of more than helping. If you’re not sure if you can solve the problem, test it first and see if it works. If it works, see if you can justify why you solved it. A solution without justification also doesn’t help much. I recommend repl.it for testing, just like I did above.

Show 3 more comments

2 answers

8


When analyzing the characters of your code we have:

inserir a descrição da imagem aqui

Result generated by https://www.soscisurvey.de/tools/view-chars.php

All these characters marked in blue are "invisible" characters that appear to be normal whitespace. This happens because you are not using a code editor to write the codes or copied the code from elsewhere and ended up bringing these characters together.

If you remove all undue characters your code will work perfectly.

1

After validation of @Anderson Carlos Woss in a comment on your question, the problem you are facing should be mitigated by paying attention to the following points:

  1. Your text editor creates files in which encoding?

The recommended (or good practice) is to work with UTF-8

  1. Whether your editor creates code in UTF-8 is with or without BOM?

o GOOD is a signature that defines the type of encoding for UTF-8 files. The answer to this question should clarify better: What is the difference between GOOD and NO GOOD encoding files?

The problem with conflicting file encodings is that there are probably hidden characters that the PHP interpreter will try to execute.

A good tip when working with Windows is to use Notepad++ which offers the possibility to configure the encoding of the file you are writing, but my personal recommendation is to work with Phpstorm, it makes all this transparent ;)

And, finally, your code is correct, but Anderson’s rewrite improved the reading, watch out for that too :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.