//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.