How to view files without underline?

2

I need to display images in elements img of HTML using as value of src attribute the address of the file retrieved through the function glob() .

The problem is that it does not work with underline file addresses.

$arquivos = glob("img/*.*");     

for ( $i = 0; $i < count($arquivos); $i++ ) {           

    $num = $arquivos[$i];                       


    //não exibe a imagem, por quê?
    echo "<img src=". $num .">";


    //quando coloco o nome do arquivo direto funciona:
    echo "<img src='img/criança com bolo.jpg'>";

 }
    
asked by anonymous 14.08.2015 / 21:04

4 answers

3

The problem is not with PHP is with HTML, you have to add img/ and use quotation marks in the attribute.

I recommend that you use ' (apostrophes) in PHP and quotation marks in html, it should look something like:

echo '<img src="img/'. $num . '">';

A tip urls need to be coded to work "better", use rawurlencode like this:

$arquivos = glob("img/*.*");

for ( $i = 0; $i < count($arquivos); $i++ ) {
     $num = $arquivos[$i];
     echo '<img src="img/'. rawurlencode($num) . '">';
 }

Read:

14.08.2015 / 21:22
1

Whenever you manipulate files, their names must be without special characters, with underline or no spaces. You used child with bolo.jpg and at some point in the glob method the program did not understand the filename because of cedilla or spaces.     

14.08.2015 / 21:18
0

The ideal is not to have spaces and special characters in file names. You can handle this by submitting the file.

In your case, for files that are already on the server you can use the str_replace () function to replace spaces with% 20

echo str_replace(' ', '%20', 'img/criança com bolo.jpg');

will result in:

img/criança%20com%20bolo.jpg

And it will become affordable.

    
14.08.2015 / 21:23
0
  //não exibe a imagem, por quê?
    echo "<img src=". $num .">";

The result of this will be

<img src=criança com bolo.jpg>

HTML will interpret the end of the file in the first space character:

<img src=criança >

Obviously, since this name does not exist, it returns a nonexistent image. In addition, the directory is missing. img/

In the version in which you wrote directly, you entered the delimiting quotes and directory:

//quando coloco o nome do arquivo direto funciona:
echo "<img src='img/criança com bolo.jpg'>";

This results in <img src='img/criança com bolo.jpg'>

Due to the quotation marks, the HTML interpreter understands that it should read the name of the file up to the closing quotation mark.

To resolve, add the bounding quotes and the correct path:

echo "<img src='img/". $num ."'>";

These are basic HTML rules. PHP itself has no relation whatsoever. But it's common to confuse these blends with languages.

If you want to use double quotes in HTML, just reverse, using single quotation marks in PHP:

echo '<img src="img/'.$num.'">';

* In PHP, it is recommended to use single quotation marks to delimit strings. Double quotes have a specific purpose for the compiler, which consumes a bit more resources (memory, processor, etc.).

As for the use of URLEncode, it is unnecessary because the browser itself does the conversion, except for very old browsers more than 10 years ago.

Practical example, access the following URL:

http://www.amazon.co.jp/タイムセール/b/ref=nav_topnav_deals?ie=UTF8&node=2221688051

This is an Amazon Japan page. Note the katakana in the URL. The purpose of the Japanese term is for SEO.

Imagine if you use urlencode or rawurlencode, see how it would look:

http://www.amazon.co.jp/%E3%82%BF%E3%82%A4%E3%83%A0%E3%82%BB%E3%83%BC%E3%83%AB/b/ref=nav_topnav_deals?ie=UTF8&node=2221688051

Horrible .. spoiled the URL .. "spoiled the SEO" of this page.

In older browsers from 2003 or 2004 onward, multibyte characters will be automatically converted with their respective entities, this includes Latin characters.

As I mentioned in other discussions about encodes, the developer needs to have a globalized view to build an internationalized platform. The vast majority of developers build localized systems and many people think their systems are global and in fact are not.

I avoided commenting here on this topic because I found the main focus of the question unnecessary and misleading, but I hope I have made it clearer.

    
15.08.2015 / 05:43