stumped
stumped

Reputation: 3293

Display images with spaces in the file name (php)

I'm going through the images I have in the img folder, and all of the images show up fine except for the ones that have spacing in their names e.g. "Screen shot.png". The user can upload photos, and their files may have spaces in the file name. Should I just remove all spaces before I save the images into my img folder? Or is there a way to still display the images with spacing in the image filepath?

function displayImage($row) {
                    $file_path = "img/" . $row['File_name'];
                    $image_title = $row['Title'];
                    echo "<br>" . $file_path;
                    echo "<li><img src=$file_path alt=$image_title>";
                    echo "<span class='info'><span>$image_title</span></span></li>";
                }

Upvotes: 0

Views: 6457

Answers (5)

Bar-code
Bar-code

Reputation: 277

Just replace spaces by their url_encoded value : %20.

In your case, replace the first line of your function by:

 $file_path = "img/" . str_replace(" ", '%20', $row['File_name']); 

Upvotes: 0

Martin
Martin

Reputation: 22760

You want to use rawurlencode* your file name (not the file path simply the name element thereof; Such as:

 $file_path = "img/" . urlencode($row['File_name']);

*> Use rawurlencode because it will encode spaces as %20 rather than + which urlencode will do. It also more closely fits the RFC.

But it is not a good idea to use these non url safe values and it would be better to rename the file on upload such as with:

$newFilename = preg_replace("/\h*/","_",$uploadedFile['name']);

\h catches any horizontal whitespace character
* catches it zero or more times (greedy)
These are then replaced with an underscore _

Upvotes: 1

manian
manian

Reputation: 1438

You should always rename the file with some unique file names. For example, you can append timestamp to the file name. By this way you can generate new file names without spaces. You can save the original file name for display purpose. But do not save the file to the server with original name of the file. It's because it is possible that the users could upload 2 images with same name which will replace the old file.

Upvotes: 1

Simon K
Simon K

Reputation: 1523

Use rawurlencode()...

function displayImage($row) {
    $file_path = "img/" . rawurlencode($row['File_name']);
    $image_title = $row['Title'];
    echo "<br>" . $file_path;
    echo "<li><img src=$file_path alt=$image_title>";
    echo "<span class='info'><span>$image_title</span></span></li>";
}

Upvotes: 3

ficuscr
ficuscr

Reputation: 7053

Does this work? Like I commented... Looks like you are just not quoting the path / src value.

function displayImage($row) {
                    $file_path = "img/" . $row['File_name'];
                    $image_title = $row['Title'];
                    echo "<br>" . $file_path;
                    echo "<li><img src=\"{$file_path}\" alt=\"{$image_title}\">";
                    echo "<span class='info'><span>$image_title</span></span></li>";
                }

Upvotes: 0

Related Questions