Create a file with another languages characters in php - php

i'm trying to create a file with name "Дискография" but in the folder appears with the name ДиÑкографиÑ.
The php file is in UTF-8.
I'm working on windows.
My file system is NTFS.
i have this:
<?php
$nombre = "Дискография";
$fp = fopen("C:/$nombre", 'w+');
fclose($fp);
?>

Looks like this was addressed somewhat in another answer:
How do I use filesystem functions in PHP, using UTF-8 strings?
Original/old answer:
It probably depends on the coding of your filesystem, but at a guess, I'd use:
$nombre = utf8_encode("Дискография");

Related

Use PHP to write a file to Windows that contains Japanese characters in the filename

I want to save a file to Windows using Japanese characters in the filename.
The PHP file is saved with UTF-8 encoding
<?php
$oldfile = "test.txt";
$newfile = "日本語.txt";
copy($oldfile,$newfile);
?>
The file copies, but appears in Windows as
日本語.txt
How do I make it save as
日本語.txt
?
I have ended up using the php-wfio extension from https://github.com/kenjiuno/php-wfio
After putting php_wfio.dll into php\ext folder and enabling the extension, I prefixed the filenames with wfio:// (both need to be prefixed or you get a Cannot rename a file across wrapper types error)
My test code ends up looking like
<?php
$oldfile = "wfio://test.txt";
$newfile = "wfio://日本語.txt";
copy($oldfile,$newfile);
?>
and the file gets saved in Windows as 日本語.txt which is what I was looking for
Starting with PHP 7.1, i would link you to this answer https://stackoverflow.com/a/38466772/3358424 . Unfortunately, the most of the recommendations are not valid, that are listed in the answer that strives to be the only correct one. Like "just urlencode the filename" or "FS expects iso-8859-1", etc. are terribly wrong assumptions that misinform people. That can work by luck but are only valid for US or almost western codepages, but are otherwise just wrong. PHP 7.1 + default_charset=UTF-8 is what you want. With earlier PHP versions, wfio or wrappers to ext/com_dotnet might be indeed helpful.
Thanks.

Why file_get_contents not working in PHP?

This code get content file txt:
$str = "c:\\\\表\\t.txt";
$con=file_get_contents( $str);
echo $con;
File exist in folder:
Result: show error:
Warning: file_get_contents(c:\表\t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 6
Why is file_get_contents() not working?
How read content of path c:\表\t.txt?
Try to use urlencode to encode your directory name:
$str = 'c:\\'.urlencode('表').'\t.txt';
$con=file_get_contents( $str);
echo $con;
This is described here in detail.
EDIT
Assuming you're using UTF-8 encoded source files, you could also try one of the following
$str = 'c:\\'.urlencode(mb_convert_encoding('表', 'UTF-16', 'UTF-8')).'\t.txt';
// or just
$str = 'c:\\'.mb_convert_encoding('表', 'UTF-16', 'UTF-8').'\t.txt';
As far as I know newer (> FAT32) Microsoft filesystems use UTF-16 encoding. But this will make your solution fail on other (e.g. Linux) filesystems.
EDIT 2
You can also try to convert your UTF-8 filename into a different encoding such as SJIS, SJIS-win, SJIS-2004, JIS, EUC-JP, eucJP-win, EUC-JP-2004, CP932, JIS-ms or the like. But I'm not an expert in east asian character encodings - so treat that information with caution.
try this
<?PHP
$con=file_get_contents('./表/t.txt', true);
$con=file_get_contents('./表/t.txt',FILE_USE_INCLUDE_PATH);
echo $con;
?>
Language : PHP
$results = scandir('c:\');
$result will give you all folder name inside c drive.
you can find your folder in $result array.
now you have folder name in $result and now you can go to file.
Modified :
$results = scandir('/web');
$results = "/web/$results[21]/t.txt";
$con=file_get_contents( $results);
echo $con;
Explanation :
1.) /web is the directory where is 表 folder and some other folders.
2.) $result[21] is giving me value 表 on browser i know its 表 folder.
3.) Now you have file path. Go ahead.
NOTE : If you still use Chinese character in your folder and file then you have to change your OS from window to ubuntu.
Create path based on single /
$content = file_get_contents("C:/表/t.txt");
You can read more about Filesystem here: http://php.net/manual/en/wrappers.file.php

How to convert a CSV file using a PHP script

I need to convert a CSV file to UTF-8 and rename it using a PHP script.
The following code worked on my PC but now i need to do this on a server as a CRON task
iconv -f UTF-16LE -t UTF-8 OLD-FILE.csv > NEW-FILE.csv
Anyone know the equivalent in PHP. Thanks a lot.
A simple method would be to load the CSV file to a string using this command:
http://php.net/manual/en/function.file-get-contents.php
Then you can UTF-8 Encode the string using this command:
http://php.net/manual/en/function.utf8-encode.php
Finally write the string to a file using file_put_contents.
Finished code could look like this:
$file_data = file_get_contents('/my/path/to/file.csv');
$utf8_file_data = utf8_encode($file_data);
$new_file_name = '/my/path/to/new_file.csv';
file_put_contents($new_file_name , $utf8_file_data );
Make sure the web server has the correct permissions to both read and write to appropriate locations.
Here is the link to file_put_contents():
http://php.net/manual/en/function.file-put-contents.php
So here is the solution I found thanks to our brainstorming:
<?php
$file_data = file_get_contents('/home/MYFILE.csv');
//$utf8_file_data = utf8_encode($file_data);
$utf8_file_data = mb_convert_encoding($file_data, "UTF-8", "UTF-16LE");
//$utf8_file_data = iconv("UTF-16LE","UTF-8",$file_data);
$new_file_name = '/home/MYFILE_NEW.csv';
file_put_contents($new_file_name , $utf8_file_data );
?>
The only pb is that the output size is twice as big as the input. If I use ICONV on my PC it is HALF the size...
If anyone knows I'd like to hear why.
if iconv is available you can use the PHP equivalent: http://php.net/manual/en/function.iconv.php note that this takes a string, you will need to read in the file http://php.net/manual/en/function.fread.php and then write it out http://php.net/manual/en/function.file-put-contents.php but this approach may be slower and for big files, it will require to load the file to memory.

Issue with the text file writing in php

I am using urldecode data for writing a content in to a text file, but in that file all the contents are showing together(not aligned expected) in windows notepad(in windows wordpad it is coming correctly), also when i open it in Ubuntu contents are coming correctly(my contents have enter key and spaces some special characters too).
$attachment_file = fopen(Yii::app()->basePath.'/../uploads/attachment'.$user_id.'.txt', "a+") or die("Unable to open file!");
$content = urldecode($note_data["note_data"]);
fwrite($attachment_file,$content);
fclose($attachment_file);
For the quick fix i did
$content = str_replace("\n","\r\n",$content);
but i want to know is there any other methods to do it.
If you are using Linux to create the file, you should manually add this. If you use Windows, You can try str_replace("\n", PHP_EOL, $content) instead.
I don't understand why you are doing urldecode. Maybe you should use something like utf8_decode if you have your data in utf-8 format.

encoding from .txt file to string using php

I'm doing this project where I receive a .txt file from a MATLAB program in a PHP server. I would like to extract the data in the .txt file and encode it to a string to be sent by the server to an android. Any ideas on how to do that?
Screw the string.
readfile()
use below code for your task.
$file = file_get_contents('test.txt', true);
$parsed_str = json_decode($file);
I think it may be helpful to you..
Thanks.

Categories