How to handle Japanese characters in HTML , PHP & MySQL - php

I have written the following code in order to read content from an Excel file that contains Japanese characters and display on the web page:
<!DOCTYPE html>
<?php
//header("Content-Type: text/plain; charset=UTF-8"); // output as text file
header("Content-Type: text/html; charset=UTF-8");
if(isset($_POST['upload'])){
unset($_POST['upload']);
$file_name = basename($_FILES['csv_file']['name']);
$name = pathinfo($file_name, PATHINFO_FILENAME );
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$csvFile = fopen($_FILES['csv_file']['tmp_name'], 'r');
//skip first line
fgetcsv($csvFile);
$flag = true; // flag set false when query fails for one or more records
while($line = fgetcsv($csvFile)){
if(count($line)>0){
$data = utf8_decode($line[0]);
echo "$data <br>";
}
}
if($flag)
echo "<h1 style='color:limegreen'> All records imported successfully ! </h1>";
else
echo " Error while fetching one or more records";
fclose($csvFile);
}
?>
<form method="post" action="importExcel.php" enctype="multipart/form-data">
<input type="file" name="csv_file" id="csv_file" accept=".xlsx" >
<input type="submit" name="upload" id="upload" >
</form>
This is the excel sheet with the Japanese characters:
My question is :
How do i make those Japanese characters to display properly on web browser? I tried using the utf_decode() function. It did not help.
Also if i want to store these Japanese alphabets in MySQL database, what changes will i be required to make?
Currently the browser displays the Japanese characters as question marks and some garbage values after using utf_decode() function. When i remove it, it displays junk values on screen.
Edit:
Here is the sample data from the excel file:
アイリッシュ・セッター アイリッシュ・ウォーター・スパニエル アイリッシュ・ウルフハウンド

Do not use any encoders/decoders, that will only make things worse.
Do have CHARACTER SET utf8 (or utf8mb4)
Do have the header or meta, as already discussed.
Do set the connection between php and mysql to be UTF-8. (Let's see that part of the code to check.)
Do specify UTF-8 in <form method="post" ... accept-charset="UTF-8">
Please show us what went wrong. There are at least 5 variants of "not working", as can be seen here: Trouble with utf8 characters; what I see is not what I stored
If you can get the HEX, note that utf8 encoding for アイリッ is E382A2 E382A4 E383AA E38383. Other Katakana characters are also E3xxyy. (Kanji and Hiragana are also 3 bytes: Ewxxyy.)

You don't need utf8_decode, adding header("Content-Type: text/html; charset=UTF-8"); will work for you. like->
NOTE: utf8_decode This function converts the string data from the UTF-8 encoding to ISO-8859-1. but you need utf-8 in your case.
NOTE: using fgetcsv may not give the desired data when reading excel file . convert it in csv or use some excel reader library.
header("Content-Type: text/html; charset=UTF-8");
$str="七起千去";
echo "$data <br>";
NOTE: if you are not using header add <meta charset="utf-8"> to your page this should also work.

Related

Image Upload in MYSQL Database not working

I have read numerous articles on stackoverflow and google regarding file upload and show directly in/from MYSQL BLOB column. I donot need to upload file anywhere because only one file is involved in my complete project and that is the logo file to be updated by the user.
Although I wanted to do the things with Codeigniter Upload library, but i couldn't complete the code so I was trying simple PHP solution, but it hasnot worked either.
Below is my code.
Code for Upload Form:
<?php echo form_open_multipart('UpdateCompanyInfo'); ?>
<div class="form-group">
<label>Logo</label>
<input type="file" class="form-control" name="logo">
[200 px (width)x200px (height)]
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Update Logo</button>
</div>
<?php echo form_close(); ?>
Code for Upload:
$check = getimagesize($_FILES["logo"]["tmp_name"]);
if($check !== false)
{
if($check[0]=="200" && $check[1]=="200" )
{
$image = $_FILES['logo']['tmp_name'];
$imageFileType = strtolower(pathinfo($_FILES['logo']['name'],PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
return '<div class="alert alert-danger">'.$imageFileType.' Logo file of only jpg/png/jpeg type is acceptable.</div>';
}
$imgContent = addslashes(file_get_contents($image));
if($this->db->update("company", array('logo'=>$imgContent)))
{
return '<div class="alert alert-success">Logo has been updated successfully.</div>';
}
else
{
return '<div class="alert alert-danger">Error 101. Failed to update the logo.</div>';
}
}
else
return '<div class="alert alert-danger">Logo file of only 200px X 200px is acceptable.</div>';
}
Code to show the Image in View
echo '<img src="data:image/png;base64,'.base64_encode($companyinfo[0]->logo).'"/>';
I uploaded the png file, so I have used data:image/png
On inspecting the image element in view, I get the following output:
<img src="data:image/png;base64,iVBORw0KGgpcMFwwXDAN ....... v6/IDhCwtLWDE56vdo6CwCC07dMeva1dhgIGnEBikg7xODG0Pq+m61z8bC4Vqpzxpq6d8BhQK7Q1+rte/oyhK61mIAoGRznxWpiYmb8jaleunKEotrSaXy1Xb1lnxXhXvSAGgKtXBetZJOQEAIQkBAQAhCQEAIQkBACEJA8P7xP2GoiDrA7B2BXDBcMFwwXDBJRU5ErkJggg==">
All articles over internet are showing this solution, but why it is not working at my end?
You need to save image as base64_encode in database. Change your code as below line while uploading:
$imgContent = base64_encode(file_get_contents($image));
and when you need to show image, you just need to put content. No need to encode again. As below:
echo '<img src="data:image/png;base64,'.$companyinfo[0]->logo.'"/>';
Hope it helps you.
After trying the suggestions of #dnFer I tried using stripslashes while displaying the image. Things worked fine.
echo '<img src="data:image/png;base64,'.base64_encode(stripslashes($companyinfo[0]->logo)).'"/>';
But I have a Question. Somewhere I read: "As I was reading this I saw the problem was stripslashes(). Since the data is binary it might contain arbitrary characters that are equal to slashes so it will remove them. "
Will this be a problem? And is my data still safe after stripping away the slashes?
Short answer:
Change
$imgContent = addslashes(file_get_contents($image));
to
$imgContent = file_get_contents($image);
Long answer:
When applying addslashes(), the binary data of the image changes when it contains one or more single quote ('), double quote ("), backslash (\) or NUL (the NUL byte). It adds a backslash before those characters.
This is probably done to avoid SQL injection at the time prepared statements didn't exist or where rarely used.
Eg:
$imgContent = addslashes(file_get_contents($image));
$query = mysql_query ("insert into imageTable values ("INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$imgContent}', '{$image_name}')"));
//Do Stuff like reading the image date from the database...
$imgContent = stripslashes($imgContent);
echo '<img src="data:image/png;base64,' . base64_encode($imgContent) . '"/>';
These days, not using prepared statements is bad practice and I suspect your database library makes use of it as well at the following line:
$this->db->update("company", array('logo'=>$imgContent));
Prepared statements (using mysqli or PDO) statements will prevent SQL injection, which make the use of addslashes() unnecessary.
Notes:
Make sure your database data field is large enough (TINYBLOB, BLOB,
MEDIUMBLOB, and LONGBLOB) to contain the complete image (data).
Do not store base64 encoded image data in the database as this data
is about 30% larger than the binary data.
Prefer to store images on the file server instead of the database, since storing lots of data can slow down the database server.
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
</form>
Php code upload.php
<?php
include_once 'dbconn.php';
$image = $_FILE['image'];
$name = $image['name'];
$tmpname = $image['tmp_name'];
$imgdestination = '../img/'.$name;
move_uploaded_file($tmpname, $imgdestination);
?>

UTF-8 encoding issue with turkish characters in php, ajax, json, jquery ,mysql

I am having trouble with the character encoding. I have tried lots of things, but got no result.
Here is the case: I have some stored procedures in my DB and I send notifications to the users. Here it is:
some calculations..
IF #howmanyknew=0 || #howmanyknew>=6 THEN
SET #memberID=(SELECT userID FROM myDB.users WHERE ticketID=#ID);
INSERT INTO notifications (senderID,receiverID,messageContent,isRead)VALUES(1,#memberID,"Some turkish characters like ö ç ş ğ ü ı İ",0);
END IF;
And here is the structure of my notifications table:
id (INT) (PRIMARY_KEY)
senderID (INT)
receiverID (INT)
messageContent (TEXT) utf-8_turkish_ci
isRead (INT)
After adding this, a notification ball appears to the user just like in the facebook. When the user clicks it, he/she sees the message:
No turkish character, no problem:
Some turkish characters: ö,ç,ü. I got this message in the console:
(index):167 Uncaught TypeError: Cannot read property 'counter' of null
Here, ajax can't get the variables from the back php page (fetchMessages.php) via json_encode()
The rest turkish characters: ş,ı,ğ.
Here is the relevant parts of my index.php and fetchMessage.php
IMPORTANT: Both are coded with "UTF-8 without BOM"
index.php:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<script>
.
.
$.ajax({
type:"POST",
url:"fetchMessages.php",
data: '{"id":"'+id+'"}',
dataType: "json",
success: function(returnedData) {
var n=returnedData.counter;
$(".content").empty();
for(i=0;i<n;i++)
{
$(".content").append("<span style='display:block;text-align:left;font-size:9px;font-style:italic;background:gray'>"+returnedData.nick[i]+":</span>");
$(".content").append("<div class='divtext' contentEditable>"+returnedData.msg[i]+"</div>");
}
$(".content").fadeIn("slow");
},
error: function(returnedData){
alert(returnedData);
}
});
.
.
</script>
fetchMessages.php:
<?php
//header('Content-Type: application/json; charset=iso-8859-1');
//setlocale(LC_ALL, 'tr_TR');
include("conn.php");
//iconv('cp1252', 'utf-8', "\x80 and \x95")-> "\xe2\x82\xac and \xe2\x80\xa2";
$input = file_get_contents('php://input');
$result=json_decode($input);
$id=$result->id;
$result=dbquery("select * from notifications where receiverID=".$id." and isRead=0");
$senders=array();
$msgs=array();
$ids=array();
$counter=0;
while ($row = $result->fetch_assoc())
{
$m=$row["messageContent"];
//$m=iconv("UTF-8", "ISO-8859-1//TRANSLIT", $m), PHP_EOL;
$msgs[$counter]=$m;
$ids[$counter]=$row["id"];
$senderID=$row["senderID"];
$result=dbquery('select * from usertbl where userID='.$senderID.'');
$s=$result->fetch_assoc();
$senders[$counter]=$s["username"];
dbupdate("notifications", array("isRead"=>1), array("id"=>$ids[$counter]));
$counter=$counter+1;
}
echo json_encode(array(msg=>$msgs,nick=>$senders,counter=>$counter,ids=>$ids));
?>
You also can see the some comment outs in the php page. I have tried them too. But as I said, no result.
AND If I convert them from "utf-8 without BOM" to "utf-8" I only get an alert dialog box for the characters in Case 2 above:
[object XMLHttpRequest]
However, I still get the question mark for the rest.
You can try adding
"SET NAMES UTF-8"
Sql statement just after your connection to database.
You could try enforce your connection using: set-charset
http://php.net/manual/en/mysqli.set-charset.php

how to handling non English chars with php

I have code in php that create file with name that user input like below
html
<form method="post">
<input type="text" name="file">
<input type="submit">
</form>
php
$file = $_POST['file'].'.php';
$f = fopen($file, 'w');
fclose($f);
the problem appear when user use non-english chars
example user input = اللغة العربيه
resault = ظ…ط±ط§ظ‡ظ‚ظˆ-ط§ظ„طھط§ظٹطھظ†ط.php
but it should be = اللغة العربيه.php
Trying to create files with non-english filenames can be quite hard.
Easiest solution is to not use such filenames at all, but encode all filenames e.g. with urlencode:
$filename = $_POST['file'].'.php';
$encoded_filename = urlencode($filename);
$f = fopen($encoded_filename, 'w');
fclose($f);
This will not affect English characters and will allow creating filenames using any language.
Note that each filesystem will have some limits on how long filesnames can be, so if $encoded_filename becomes too long this will not work.

MySQL UTF8 Characters load

OMG I AM SOO SORRY I HAVE WRONG CODE TYPE :D HERE IS AN EDIT
OK I somehow figured out how to import UTF8 characters in MYSQL but when I load them they are not UTF8. And YES I have
<meta charset="UTF-8"/>
Look At this http://prntscr.com/b13xel . First post is OK but its not UTF8 it is from normal chars latin i think. But 2ND post isnt working :/ I have stored with UTF8 Charset in mysql http://prntscr.com/b13y2n (2ND is Test and Test2 if you dont get it xD) . I think I spelled it wrong xD but NVM I think you will understand me.
This is the code:
<div class = "tablatekst">
<?php
$novostid = 1;
while($novostid < 500)
{
$sqlnovosti = mysql_query("SELECT * FROM novosti WHERE ID = $novostid");
if(mysql_num_rows($sqlnovosti) > 0)
{
while($red = mysql_fetch_assoc($sqlnovosti))
{
$nnaslov = $red['Naslov'];
$ntekst = $red['Tekst'];
}
echo "<h2> $nnaslov </h2>";
echo $ntekst;
echo "<br><hr><br>";
}
$novostid = $novostid + 1;
}
?>
</div>
Put this before your while:
mysql_query('SET NAMES utf8');
However, I strongly recommend you to migrate to mysqli
What are the content-type headers set to in your PHP script?
https://stackoverflow.com/a/4279294/6275228
Use header to modify the HTTP header:
header('Content-Type: text/html; charset=utf-8');
Note to call this function before any output has been sent to the
client. Otherwise the header has been sent too and you obviously can’t
change it any more. You can check that with
headers_sent. See the manual page of
header for more information.

Unable to display uploaded images via PHP into MySQL

I've trimmed my code down to the bare minimum to try to find why I cannot display any image that I upload & store via PHP into MySQL. If anyone can point out my error(s) I'd be most grateful.
On execution, the browser reports that the image cannot be displayed as it contains errors.
However, the image uploads & displays fine in other databases running in this same environment.
I've checked that the database holds a blob after upload.
I guess I'm missing something obvious.
Upload form..
<body>
<form enctype="multipart/form-data" action="imagetestprocess.php" method="post">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
Form handler..
<?php
include("../mylibrary/login.php");
login();
$imagefile = file_get_contents($_FILES['image']['tmp_name']);
$imagefile = mysql_real_escape_string($imagefile);
$query="UPDATE pieces SET image_full='$imagefile' WHERE assetno='1'";
$result = mysql_query($query);
?>
Image displayer..
<?php
include("../mylibrary/login.php");
login();
echo "<body>";
echo "before";
echo "<img src=\"showimage.php\" alt=\"showimage\">";
echo "after";
?>
called function...
<?php
include("../mylibrary/login.php");
login();
$query = "select * from pieces where assetno='1'";
$result=mysql_query($query);
$row=mysql_fetch_array($result, MYSQL_ASSOC);
$image=$row['image_full'];
header("Content-type: image/jpeg");
echo $image;
?>
change the image_full field type to MEDIUMBLOB / BLOB
user this $image = chunk_split(base64_encode(file_get_contents("image.jpg")));
instead of $imagefile = file_get_contents($_FILES['image']['tmp_name']);
and in show image function use image as below.
header("Content-type: image/jpeg");
echo base64_decode($image);
use mysql_escape_string or addslashes and clear your browser cache to see if it works
If the above solutions does not work for you.
Try increasing the length of the field in database.
if still it does not work,
You can check if the image format is RGB or CMYK.
format shoud be RGB to see on screen.
To make it sure you can try opening the same image file in browser.
I think it has something to do with your database encoding. some encoding does not support binary data.
If you cannot change the encoding, maybe you css base64 encode the data before saving and decode it when displaying. only thing is base64 will increase the size by 3.

Categories