How to display files without underline?

Asked

Viewed 179 times

2

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

The problem is that it doesn’t work with file addresses without underline.

$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'>";

 }
  • Good evening @Nilson, some of the answers solved your problem?

  • Good evening, I wonder if any of the answers helped you, if not please comment on what you think is missing.

4 answers

3

The problem is not with PHP is with HTML, missed you add img/ and use quotation marks on the attribute.

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

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

A tip urls need to be coded to work "best", use rawurlencode thus:

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

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

Read:

1

Whenever manipulating files, their names must be without special characters, with underline or without spaces. You used child with cake.jpg and at some point in the method glob(); the program did not understand the file name because of the cedilla or spaces.

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 non-existent image. Moreover, the directory must be defined. img/

In the version in which you wrote directly, you inserted the bounding 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 quotation marks, the HTML interpreter understands that you should read the file name up to the closing quote.

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. But it is common to confuse these mixtures with languages.

If you want to use double quotes in HTML, just invert, using single quotes in PHP:

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

*In PHP, it is recommended to use simple quotes to delimit the strings. The double quotes have a specific purpose for the compiler, which consumes a little more resources (memory, processor, etc).

As for the use of Urlencode, it is unnecessary, because the browser itself does the conversion, except very old browsers of 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

It is a page of Amazon Japan. Note the katakana in the URL. The purpose of the term in Japanese 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 onwards, multibyte characters will be automatically converted with their respective entities, this includes Latin characters.

As I mentioned in other discussions regarding encodes, the developer needs to have a globalized vision to build an internationalized platform. The vast majority of Velopers build localized systems and many think their systems are global and actually are not.

I avoided commenting here on this topic because I found it unnecessary and would distort the main focus of the question, but anyway, I hope I made it clear.

  • I don’t want to disagree with you, but there are some mobile browsers, like Safari, that interpret differently, some use utf8 and others use ASCII-compatible. The urlencode or rawurlencode can help yes. ;)

  • I didn’t understand the disagreement.. I didn’t post saying that browsers don’t support multibyte in the url bar... obviously, newer ones do.. the problem is with older browsers. I added a practical example in the answer to make it clearer.

  • I guess I didn’t express myself right, you said the urlencode is unnecessary and I spoke the reason for the need that still exists yes... Now if you start talking about SEO, then it’s best to change the whole url scheme, starting with not using spaces but hyphens, but this doesn’t seem to be the focus of the AP problem.

  • It is unnecessary to convert the space character to its respective entitie, because even without Encode, the browser converts automatically.. and this is independent of SEO questions.. but anyway, the best way is to maintain a safer standard, no spacing, even for SEO.. Obviously I am disregarding browsers of 1900 and ball.. is an audience irrelevant even for large services.

  • Daniel ios8.3 mobile Safari was a browser that has faced such a problem, I think I’ve mentioned this.

  • show this in practice, referring to the safari of Ios 8.3.

  • Damn it, you think I’m lying? Why would I want to throw a false information in here? The afternoon provided a test-case to try to confirm the problem.

  • calm.. ô loco... kkk.. is you who is interpreting this way.. in no time I used any adjective or made insinuations.... Just understand that it’s very vague to say that you saw X someday, that you saw Y again there I don’t know when.. understand? then just show 1 + 1 = 2.. It’s silly to stay here in this unnecessary slam.. Damn, I say.. Mr. Mr.

Show 3 more comments

0

The ideal is not to have spaces and special characters in filenames. You can treat this when uploading the file.

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

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

will result in:

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

and will become accessible.

Browser other questions tagged

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