How to read arabic file names inside directory - php

I am trying to read files inside directory.It works fine i am getting the list of files but the file names with arabic characters not showing just showing "???.txt?"
<?php
$dir = "C:\Users\Administrator\Pictures";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if ($file != "." && $file != "..") {
$file_utf8 = iconv( "Windows-1256", "utf-8", $file );
echo "filename:" . $file_utf8 . "<br>";
}
}
closedir($dh);
}
}
?>
Any help will be appreciated

If you echo the
echo $dir;
you may find that it is not stored in $dir variable as expected. So you should put the string in single quotes (') instead of double quotes ("):
$dir = `C:\Users\Administrator\Pictures`;

Related

How to get Arabic filenames charset display correctly using PHP readdir() function

snapshot of the result i
that's what I tried and ended with question marks like the snapshot above , I don't know how to get rid of these question marks and get the correct arabic charset instead , anyone help
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if ($file != "." && $file != "..") {
$file_utf8 = iconv( "CP1256", "UTF-8", $file );
echo $file_utf8 . "<br>";
}
}
closedir($dh);
}

Get images url from folder Filter only images from folder

I use the code suggested here
https://stackoverflow.com/a/18316453
This is what I have now.
<?php
$dir = "sliders/slides";
$images = array();
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if (!is_dir($dir.$file)) $images[] = $dir . '/' . $file;
}
closedir($dh);
}
}
echo json_encode($images);
?>
My result includes 2 extra items
sliders/slides/.
sliders/slides/..
which makes my slider having 2 extra blank slides
How can I filter the result to show only .jpg and .png files in order to remove /. and /.. be included in the results
I'm trying to create sliders that gets images from a folder
Thanks
Try something like this inside the while :
if ($file != "." && $file != ".." && !is_dir($file) {
$images[] = $dir . '/' . $file;
}

read files insert text into mysql

myFolderi have thousands of image files that have keyword text for the name. i am trying to read from the list of images and upload the text into a dB field. the problem is that some of the text has utf8 characters like l’Été that show up like this ��t�
how can i read foreign characters so that the accents will insert into the dB field?
this is how im handling it now
function ListFiles($dir) {
if($dh = opendir($dir)) {
$files = Array();
$inner_files = Array();
while($file = readdir($dh)) {
if($file != "." && $file != ".." && $file[0] != '.') {
if(is_dir($dir . "/" . $file)) {
$inner_files = ListFiles($dir . "/" . $file);
if(is_array($inner_files)) $files = array_merge($files, $inner_files);
} else {
array_push($files, $dir . "/" . $file);//$dir = directory name
//array_push($files, $dir);
}
}
}
closedir($dh);
return $files;
}
}
foreach (ListFiles('../../myDirectory') as $key=>$file){
//$file = preg_replace( '#[^\0-\x80]#u',"", $file );
echo $file ."<br />";
}
this is producing the same result
$str = "l’Été";
utf8_decode($str);
echo $str;
This solution may work for you, it will loop through all files in a directoy and then recursivly through any directories found until it ends up with a massive array of files.
Ive added some points you may wish to change, eg either mutli or single dimension arrays ( all depend on if you may want to maintain the folder structure.
and also if you want the file extention to be saved when you save the file name to db.
Code
function recursive_search_dir($dir) {
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (in_array($file,array(".","..")))
continue; // We dont want to do anything with parent / current directory.
if (is_dir($file)) {
$result[] = recursive_search_dir($file); // Multi-dimension
# OR
array_merge($result,recursive_search_dir($file));// Single-dimension if you dont care about folder structure.
} else {
$result[] = utf8_decode($file); // full file name ( includes extention )
# OR
$result[] = utf8_decode(filename($file,PATHINFO_FILENAME)); // if you only want to capture the name and not the extention.
}
}
closedir($handle);
}
return $result;
}
$files = recursive_search_dir("."); // recursively searcht the current directory.

How to stripslashes from the names of file on server with php

Sequel to my question here: Addslashes displays as forward slashes in php ,
I want to strip slashes from the filenames of files i have on my server. the slashes were added during file upload (magic_quotes).
Please how can i go about this? thanks
Here you go:
<?php
$path = '/path/to/files/dir/';
$file_types = 'txt,doc,pdf';
foreach (glob($path.'*.{'.$file_types.'}', GLOB_BRACE) as $filename){
if(rename($filename , stripslashes($filename))){
echo 'Renamed file from '.$filename.' to '.stripslashes($filename).'<br />';
} else{
echo 'Failed to rename file from '.$filename.' to '.stripslashes($filename).'<br />';
}
}
?>
Change path to files and the comma separated list of file types.
Update with asker's code on the comments:
$dir='cv';
if(is_dir($dir)){
if ($dh = opendir($dir)) {
while (false !== ($file = readdir($dh))) {
if ($file != "." && $file != "..") {
$file2 = $dir."/".$file; $newfile=$dir."/".stripslashes(urldecode($file));
if(rename($file2, $newfile)){
echo "renamed from $file2 to $newfile <br>";
} else{
echo "error renaming from $file2 to $newfile <br>";
}
}
}
closedir($dh);
}
}
http://php.net/function.stripslashes

Display contents of multiple text files

I have a number of text files held in directory
/results/...
All the text files are named with unixtime stamps, inside each of the following files there is:
#text¬test¬test1¬test2¬test3¬test4¬1262384177
Each piece of text is seperated by '¬'.
I'd then like to feed the contents of the text file into an array and output it, in for example a table, but for each of the files (Perhaps loop-like?)
If have this but it only works for one file and fixed file name:
$filename = "results/unixtime.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$array01 = explode("¬",$contents);
$count = count($array01);
echo "<table width = 500 border=1 cellpadding=4>";
$i=0;
for ($i=0;$i<$count;$i++) {
echo "<tr><td>";
echo $array01[$i];
echo "</td></tr>";
}
echo "</table>";
I suggest the fairly-unknown glob function to detect all your files. Then with all the filenames in a handy array, just iterate through and open up/read each one. Sort of like this:
$files = glob('*.txt');
while(list($i, $filename) = each($files)){
//what you have now
}
A couple of things:
Unless you're dealing with really large files just use file_get_contents() to load files. It's a one-liner versus three lines of code that you just don't need;
Loop over arrays using foreach unless you explicitly need a loop counter. The loop condnition/counter is just another area where you can make simple errors;
Use opendir(), readdir() and closedir() for reading directory contents; and
Directories will contain entries like "." and "..". Use filetype() and/or a check on the name and/or extension to limit it to the files you're interested in.
Example:
$directory = "results/";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if ($type == 'file') {
$contents = file_get_contents($filename);
$items = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
foreach ($items as $item) {
echo "<tr><td>$item</td></tr>\n";
}
echo '</table>';
}
}
closedir($dir);
You can get all the files located in "result" via opendir.
There is also an example ...
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
Grab the files in the directory and read each filename.
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$filename = $file;
//your code
}
}
closedir($handle);
}
?>
source: http://php.net/manual/en/function.readdir.php
Here is a more elegant way of writing brianreavis solution, also use file_get_contents instead of fopen, fread and fclose, it's faster and less verbose.
foreach (glob('*.txt') as $filename)
{
$contents = file_get_contents($filename);
}
Use this code, replace DOCROOT with directory you want to scan.
foreach (scandir(DOCROOT.'css') as $dir) {
echo $dir . "<br>";
echo file_get_contents(DOCROOT . 'css/' . $dir ) . "<hr />";
}

Categories