Nombres de archivo de imagen con espacios en blanco

I get an array of image URL by php scanning image folders. Some of the image file names have white space. The part after white space got lost. for example: This file is fine:

This file will lost the part sky.png sky.png

My code for scanning the folder has nothing to do with checking or manipulating file names. How can I get the full name of blue sky.png without rename it to blue-sky.png?

preguntado el 31 de julio de 12 a las 15:07

Publique el código que está utilizando. -

Puede agregar un urlencode line in your code for scanning the folder to take care of that issue. -

Usar glob I can't reproduce what you're saying. Please post your code -

sorry for response late, I had power failure due to storm here after posting question. Thanks helping! -

after apply urlencode($url), the url can't find my files. str_replace %20 solved the problem. -

3 Respuestas

Space in url encoding are represented with the string "%20" so you may want to use str_replace to replace every instance of the " " character to the character "%20"

echo str_replace(' ', '%20', ' sky.png');


Información complementaria

Also, I never used it myself, but I would take a look at the php function códigourlen if I were you, it may contains useful information

Note : Url encode will transform every characters that is not standard of the string (so you may want to only use urlencode on the string that is your image name)

Respondido 31 Jul 12, 15:07

I encountered this problem but after experimenting with the answers above I realised that the real problem was that I had written my html link without the inverted commas - typically browsers are tolerant of this, but not if spaces are present. i.e I had coded something like
a href=../folder/blue sky.png
en lugar de
a href="../folder/blue sky.png"
With the correct syntax spaces in the address do not cause problems

Respondido 15 Oct 18, 21:10

This also prevents saving of downloaded files with names like blue%20sky.png, hence should be the proper solution. - Guney Ozsan

You may use regex.


$file_name = " sky.png";
$file_name2 = preg_replace("/ /", "-", $file_name);
echo $file_name2; //echos

espero que esto ayude...

Respondido 31 Jul 12, 15:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.