scandir in php with special (hungarian) characters - php

I've been trying to read (image) files a folder in my website. But, there's a problem! There are some special hungarian character (like: á,í,ú,ű,ó,é, etc...) in the name of the folder which I want to read. I've been trying many mode, but it doens't work. My php code:
<?php
$images = scandir("images/2005/avatás/");
foreach($images as $file) {
print($file);
}
?>
I've been trying to fix the problem, with mb_convert_encoding(), urlencode(), urldecode() functions, but these couldn't help me.
Have you got any idea?
Thanks!

iconv("windows-1254", 'UTF-8', $foldername)
Same problem same language, I assume this should work for you too

You could try using iconv()
<?php
$folder = iconv("windows-1251", "UTF-8", "images/2005/avatás/");
$images = scandir($folder);
foreach($images as $file) {
print($file);
}

Related

Renaming files from blanc space between name to underscore with php

i am trying to find a solution for the following problem:
I have two kinds of files in a folder of my webserver:
locked files__date_time.txt
locked files_date_time.txt
I would like to rename the files of the second type with the single underscore between "filename" and "date" like "locked files_date_time.txt" to "locked_files_date_time.txt" with a simple php-script.
This for i found a sample script, but its not working.
<?php
$directory = '/daten/www/htdocs/files/locked/';
foreach (glob($directory."*locked files*") as $filename) {
$file = realpath($filename);
rename($file, str_replace("*locked files*","locked_files",$file));
}
?>
Script is geeting executed, without error, but nothing happens with the files.
What is wrong there?
Would be pleased about some support. Thanks very much in advance.
Greetz
To update the file names this line needs to be updated:
Old:
str_replace("*locked files*","locked_files",$file));
New:
str_replace("locked__files","locked_files",$file));
To find the files that you are looking for this line needs to be updated:
Old:
foreach (glob($directory."*locked files*") as $filename) {
New:
foreach (glob($directory."*locked__files*") as $filename) {
That is looking for files that match what is between the double quotes.
<?php
$directory = '/daten/www/htdocs/files/locked/';
foreach (glob($directory."*locked files*") as $filename) {
$file = realpath($filename);
rename($file, str_replace("locked__files","locked_files",$file));
}
?>
It will solve your problem
rename($file, preg_replace("/locked files/","locked_files",$file));

Replace .dds to .png?

I am looking for an way to replace .dds to .png with php.
The reason is this every images is automaticly inserted into the database with the extension .dds the .dds is an format that the game uses to render its images so i cant replace this.
So what i do is fetch the icons from the database and then the file name must be changed to .png before i insert it into an other database.
I have tried it with preg_replace but i need some help on how to set it up.
$str = str_replace('.dds', '.png', $mystring);
I don't think replacing extension from DDS to PNG is enought. I think you should rather convert from DDS to PNG:
<?php
$url = 'http://server.com/image.dds';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/image_convert.php?url=' . $url . '&format=png'));
if (#$data->success !== 1)
{
die('Failed');
}
$image = file_get_contents($data->file);
file_put_contents('rendered_page.png', $image);

How do I get fgetcsv() in PHP to work with Japanese characters?

I have the following data being generated from a google spreadsheet rss feed.
いきます,go,5
きます,come,5
かえります,"go home, return",5
がっこう,school,5
スーパー,supermarket,5
えき,station,5
ひこうき,airplane,5
Using PHP I can do the following:
$url = 'http://google.com.....etc/etc';
$data = file_get_contents($url);
echo $data; // This prints all Japanese symbols
But if I use:
$url = 'http://google.com.....etc/etc';
$handle = fopen($url);
while($row = fgetcsv($handle)) {
print_r($row); // Outputs [0]=>,[1]=>'go',[2]=>'5', etc, i.e. the Japanese characters are skipped
}
So it appears the Japanese characters are skipped when using either fopen or fgetcsv.
My file is saved as UTF-8, it has the PHP header to set it as UTF-8, and there is a meta tag in the HTML head to mark it as UTF-8. I don't think it's the document it's self because it can display characters through the file_get_contents method.
Thanks
I can't add comment to the answer from Darien
I reproduce the problem, after change a locale the problem was solved.
You must install jp locale on server before trying repeat this.
Ubuntu
Add a new row to the file /var/lib/locales/supported.d/local
ja_JP.UTF-8 UTF-8
And run command
sudo dpkg-reconfigure locales
Or
sudo locale-gen
Debian
Just execute "dpkg-reconfigure locales" and select necesary locales (ja_JP.UTF-8)
I don't know how do it for other systems, try searching by the keywords "locale-gen locale" for your server OS.
In the php file, before open csv file, add this line
setlocale(LC_ALL, 'ja_JP.UTF-8');
This looks like it might be the same as PHP Bug 48507.
Have you tried changing your PHP locale setting prior to running the code and resetting it afterwards?
You might want to consider this library. I remember using it some time back, and it is much nicer than the built-in PHP functions for handling CSV files. がんばって!
May be iconv character encoding help you
http://php.net/manual/en/function.iconv.php
You can do that by hand not using fgetcsv and friends:
<?php
$file = file('http://google.com.....etc/etc');
foreach ($file as $row) {
$row = preg_split('/,(?!(?:[^",]|[^"],[^"])+")/', trim($row));
foreach ($row as $n => $cell) {
$cell = str_replace('\\"', '"', trim($cell, '"'));
echo "$n > $cell\n";
}
}
Alternatively you can opt in for a more fancy closures-savvy way:
<?php
$file = file('http://google.com.....etc/etc');
array_walk($file, function (&$row) {
$row = preg_split('/,(?!(?:[^",]|[^"],[^"])+")/', trim($row));
array_walk($row, function (&$cell) {
$cell = str_replace('\\"', '"', trim($cell, '"'));
});
});
foreach ($file as $row) foreach ($row as $n => $cell) {
echo "$n > $cell\n";
}

Just show folders with preg match php

i am looking for a way to just show folders in php.
i am getting files like this returned from my amazon foreach loop using undesigneds s3 class.
Computer Fix Files/files/driver-ml-dlan-usb-windows-r2.exe
here my foreach.
foreach ($contents as $file){
$folder = rtrim($file['name'], "/");
if (!preg_match("/[0-9\.\-\_]/i", $folder)) { ?>
<?php echo $folder; ?>
<?php }
}
ok this kinda works but what i really want to do is just list the first folder.
so instead of this.
Computer Fix Files/files/
it would just show.
Computer Fix Files
any help please????
echo substr($folder, 0, strpos($folder, '/'));
echo reset(explode('/', $folder));
should work.

php weird file_exists bug

Has anyone got any idea to why doesn't the following work ?
$file = 'images/thumbs/1%20-%20Copy.jpg';
if(!file_exists($file)){
die('NOT THERE');
}
echo 'Yes its there.';
The problem is with the spaces. I have checked the file exists,dbl checked n triple checked im going nuts. :(
Help
file_exists works on the file system and not via HTTP. So %20 will not be recognized as space but literally as %20; use spaces instead:
$file = 'images/thumbs/1 - Copy.jpg';
$file = rawurldecode('images/thumbs/1%20-%20Copy.jpg');
try these two
$file = 'images/thumbs/1\ -\ Copy.jpg';
$file = 'images/thumbs/1 - Copy.jpg';

Categories