Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image filenames with white space

Tags:

php

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:

http://domain.com/folder/blue-sky.png

This file will lost the part sky.png

http://domain.com/folder/blue 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?

like image 238
Jenny Avatar asked Nov 30 '22 23:11

Jenny


2 Answers

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
instead of
a href="../folder/blue sky.png"
With the correct syntax spaces in the address do not cause problems

like image 20
Jeremy Young Avatar answered Dec 04 '22 02:12

Jeremy Young


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', 'http://domain.com/folder/blue sky.png');

will output

http://domain.com/folder/blue%20sky.png

Complementary information

Also, I never used it myself, but I would take a look at the php function urlencode 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)

like image 121
Hipny Avatar answered Dec 04 '22 03:12

Hipny