Reputation: 61
I have a script that can upload files, but whenever a filename has ñ it doesnt show properly or the file is changing its name. For example i have ñino.jpg when i upload it, it shows as niño.jpg, i tried putting:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
But still does't work, i tried changing the table in my database collation utf8_spanish_ci from latin1_swedish but still doesn't work
My header:
<?php
require('db.php');
include("auth.php");
?>
<?php header('content-type:text/html;charset=utf-8');?>
<html>
<head>
<title>Masterlist - Private Secondary </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/dashboard.css" rel="stylesheet">
</head>
Upvotes: 1
Views: 1991
Reputation: 324
The problem is the program itself, trying to name the file into the database and there is no translator. If you save the name of whatever is on your screen, depends on many issues, even browsers languages or operating sistems. It will show as you describe with the funky "�" question mark because it just saves whatever is in the fieldname where you tipe the name of your file. I had follow all advice everywhere. Set my headers correctly and tried all my charsets available. Even used google fonts and whatsoever font available to display and save correctly those characters on my database, but no success.
An old program fixed this problem translating the Ñ to its UNICODE HTML Entity: Ñ
for ñ you can use ñ
(see chart here https://www.compart.com/en/unicode/U+00D1 ) a process done back and forth from MySQL to the app.
So you just need to change your database strings containing the letter Ñ and ñ to their corresponding UNICODE to reflect correctly on your browser with UTF charset. In my case, I solved my issues replacing all my Ñ and ñ for their corresponding UNICODE in all the last names in my database.
Instead of saving the name of your filename in your database as
'niño.jpg' try saving it as 'niñ
o.jpg'
Remember, you will need to do the other way around to retrieve the file, as the name recorded will be 'niñ
o.jpg' and not 'niño.jpg'. Maybe adding an extra field called "displayed_file_name" so you can see the correct file name spanish spelling saved... You need to test that out on your environment.
I dont recommend saving a file using international codes (some operating sistems may not even work right in the first place), but is completely up to you, your choice. I hope that helps. Best regards and happy coding !
Upvotes: -2
Reputation: 1715
You should use mysql_set_charset funtion:
$link = mysql_connect(.....);
mysql_set_charset('utf8', $link);
And if you use PDO:
new PDO("mysql:host=$host;dbname=$name;charset=utf8", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
Upvotes: -1