Modern browsers can open files whose names contain spaces, so a space does not automatically make a file unavailable. However, spaces must be encoded in a URL and can cause problems in links, scripts, command-line tools, content management systems, and third-party integrations. For predictable URLs, it is better to avoid them in website file names.
In a URL, a space is normally represented as
%20. For example:
https://example.com/uploads/product%20photo.jpg
Browsers usually handle this conversion automatically, but other tools may not. Common problems include:
%20;
Use short, descriptive names made from lowercase Latin letters, numbers, and hyphens. Hyphens are usually preferable to spaces or underscores because they keep URLs readable and clearly separate words.
product-photo.jpgproduct_photo.jpgProduct Photo Final #2.jpg
It is also worth avoiding URL-reserved characters such as
#, ?,
%, and
&. Keep the original file extension, use
consistent naming rules across the project, and make each name meaningful
enough to identify the file without opening it.
If files with spaces have already been uploaded, choose the approach that best fits the project:
%20 in its URL or let a URL-aware
library encode each path segment. Do not apply PHP’s
urlencode() to an entire URL: it is
intended for query-string values and converts spaces to plus signs. For
path segments, rawurlencode() is the
more appropriate function.
After renaming files, clear relevant application and CDN caches, then test
the affected pages for missing images, downloads, and other broken
resources. On case-sensitive servers,
Photo.jpg and
photo.jpg are different files, so check
letter case as well.
The following Windows batch script creates an
out directory and copies common image
files into it, replacing spaces with hyphens. The source files remain
unchanged.
@echo off
setlocal EnableDelayedExpansion
if not exist "out" mkdir "out"
for %%F in (*.jpg *.jpeg *.png *.webp *.gif) do (
if exist "%%F" (
set "name=%%~nxF"
set "newName=!name: =-!"
if exist "out\!newName!" (
echo Skipped: !newName! already exists
) else (
copy "%%F" "out\!newName!" >nul
echo Created: !newName!
)
)
)
echo Done.
pause
Save the code as a file with the
.bat extension and run it from the
directory that contains the images. Review the copies in the
out folder before uploading them. Files
that would produce duplicate names are skipped to prevent accidental
overwriting.
The same operation can be performed with Bash on both Linux and macOS. This
script also creates normalized copies in an
out directory and leaves the source files
unchanged.
#!/usr/bin/env bash
shopt -s nullglob nocaseglob
mkdir -p "out"
for file in ./*.jpg ./*.jpeg ./*.png ./*.webp ./*.gif; do
name="${file#./}"
new_name="${name// /-}"
if ; then
echo "Skipped: $new_name already exists"
else
cp "$file" "out/$new_name"
echo "Created: $new_name"
fi
done
echo "Done."
Save the script as rename-images.sh in
the image directory. Then open a terminal in that directory and run:
bash rename-images.sh
The command works with the Bash version supplied by macOS as well as common
Linux distributions. The
nocaseglob option makes extension
matching case-insensitive, so files such as
PHOTO.JPG are included too. Review the
files in out before uploading them.
Spaces in file names are valid on many systems, but they add unnecessary complexity to web URLs. A consistent lowercase naming convention with hyphens makes assets easier to link, process, cache, move, and maintain. For existing files, rename them carefully, update all references, and preserve old public URLs with redirects where necessary.