我正在做一个小画廊.我想从目录中读取文件名,并在删除一些前导数字和文件扩展名后打印下面的文件名.
我有两个版本的代码.
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
$extension = end($parts); // set to we can see last file extension
$bfile= substr($file, 2); //strips the first two characters
$cfile= preg_replace(('/\d/'),' ',$bfile);//remove numbers
$cfile= preg_replace(('/_/'),' ',$cfile);
$cfile= preg_replace(('/.jpg/'),' ',$cfile); …Run Code Online (Sandbox Code Playgroud) 假设我想对目录中的所有文件调用某个命令,并设置一个监视来对在该目录中创建的所有文件调用该命令。如果我做:
while( ( sdi = readdir( d )) != NULL ) { ... }
closedir( d );
/* Files created here will be missed */
inotify_add_watch( ... );
Run Code Online (Sandbox Code Playgroud)
那么某些文件可能会丢失。如果我inotify_add_watch()在 之前调用readdir(),文件可能会被执行两次(这将需要相当多的基础设施来防止执行两次,而且似乎边缘情况很难处理)。是否有一种简单的方法可以避免记录循环期间处理的所有文件的名称readdir并将它们与结构中返回的名称进行比较inotify_event?我可以最大限度地减少必要的比较量:
while( ( sdi = readdir( d )) != NULL ) { ... }
inotify_add_watch( ... );
while( ( sdi = readdir( d )) != NULL ) { /* record name */ ... }
closedir( d );
Run Code Online (Sandbox Code Playgroud)
通常第二个readdir()循环不会执行任何操作,但这感觉像是一个糟糕的黑客。
在UNIX®系统线程参考中,在"线程安全"标题下的是"不保证在所有UNIX系统上都是线程安全的"功能列表.此列表中不存在函数scandir(),而列表中显示readdir().
然而,scandir()的glibc 源清楚地显示为调用readdir(),而不是线程安全的readdir_r().因为某些其他原因,scandir()在列表中被省略了,或者由于某种原因我是否缺少线程安全?
我是我大学系统编程课的助教。最近,学生们正在完成一项涉及复制该程序的作业pwd。
一些学生注意到了似乎不一致的地方。当他们从 readdir 条目中读取 ino 时,它会给出与统计同一目录时不同的 inode。他们中的许多人都在问为什么。目录项的索引节点不应该指向目标目录所在的索引节点吗?如果是这样,为什么 stat 给出不同的 inode?
这是一些示例代码来演示:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#define DIR_NAME "~redacted~"
int getReaddirInode(char* entName)
{
DIR* directory;
struct dirent* entry;
ino_t result;
if ((directory = opendir(".")) == NULL)
{
perror("opendir");
exit(1);
}
while ((entry = readdir(directory)) != NULL)
{
if (strcmp(entName, entry->d_name) == 0)
{
result = entry->d_ino;
break;
}
}
if (entry == NULL)
{
fprintf(stderr, "No such directory: %s.\n", …Run Code Online (Sandbox Code Playgroud) 我有以下代码
<?php
if ($handle = opendir('C:/xampp/htdocs/movies')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo $file."<br />\n";
}
}
closedir($handle);
}
?>
Run Code Online (Sandbox Code Playgroud)
当它有像日语这样的mb语言时,它不能正常显示而是显示像kyuukyoku Choujin R ?????~?而不是kyuukyoku Choujin R研极极人あ〜る
无论如何要让它显示正确的名称或使其仍可由其他人下载?
谢谢你帮助我:)
我使用Perl readdir来获取文件列表,但是,该目录包含超过250,000个文件,这导致执行readdir并使用超过80MB的RAM的时间长(超过4分钟).由于这是每5分钟一次的重复工作,因此这个滞后时间是不可接受的.
更多信息:另一项工作将填写扫描目录(每天一次).此Perl脚本负责处理文件.为每个脚本迭代指定文件计数,目前每次运行1000次.Perl脚本每5分钟运行一次并处理(如果适用)最多1000个文件.文件计数限制旨在允许下游流处理随着Perl将数据推入数据库而触发复杂的工作流程.
是否有另一种从目录获取文件名的方法,理想情况下限制为1000(由变量设置),这将大大提高此脚本的速度?
我得到的图像文件在文件名中有捷克语字符(例如,ěščřžýáíé),我想重新命名它们而不需要重音符号,这样它们就更适合网络.我以为我可以使用一个简单的str_replace函数,但它似乎与文件数组的工作方式不同,它与字符串文字一样.
检查扩展后,我用readdir读取文件.
function readFiles($dir, $ext = false) {
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($ext){
if(end(explode('.', $file)) == $ext) {
$f[] = $file;
}
} else {
$f[] = $file;
}
}
closedir($dh);
return $f;
} else {
return false;
}
} else {
return false;
}
}
$files = readFiles(".", "jpg");
$search = array('š','á','ž','í','?','é','?','?','ý','?',' ');
$replace = array('s','a','z','i','e','e','r','n','y','c','-');
$string = "?š?áýísdjksnalci sáš?ééalskcnkkjy+?éší";
$safe_string = str_replace($search, $replace, $string);
echo '<pre>';
foreach($files …Run Code Online (Sandbox Code Playgroud) 我有这个功能:
if (is_dir($dir)) {
//are we able to open it?
if ($dh = opendir($dir)) {
//Let's cycle
while (($subdir = readdir($dh)) !== false) {
if ($subdir != "." && $subdir != "..") {
echo $subdir;
}
}
}
Run Code Online (Sandbox Code Playgroud)
返回:
directory1 , directory2, directory3 etc.. etc..
Run Code Online (Sandbox Code Playgroud)
无论如何,如果我这样做:
if (is_dir($dir)) {
//are we able to open it?
if ($dh = opendir($dir)) {
//Let's cycle
while (($subdir = readdir($dh)) !== false) {
if ($subdir != "." && $subdir != "..") {
if (is_dir($subdir)) …Run Code Online (Sandbox Code Playgroud) 我想检查文件是否返回readdir目录.我尝试使用DT_DIR常量(man readdir如上所述)但它未定义.我应该包含哪些文件才能获得它?
现在我用
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
Run Code Online (Sandbox Code Playgroud)
gcc版本是4.6.1
编译字符串:
gcc a.c --std=c99 -Wall
Run Code Online (Sandbox Code Playgroud) 我在Windows中运行Perl,我使用readdir获取目录中所有文件的列表,并将结果存储在数组中.数组中的前两个元素似乎总是"." 和"......".这个订单是否有保证(假设操作系统没有改变)?
我想执行以下操作来删除这些值:
my $directory = 'C:\\foo\\bar';
opendir my $directory_handle, $directory
or die "Could not open '$directory' for reading: $!\n";
my @files = readdir $directory_handle;
splice ( @files, 0, 2 ); # Remove the "." and ".." elements from the array
Run Code Online (Sandbox Code Playgroud)
但我担心这样做可能不安全.我见过的所有解决方案都使用了数组中每个元素的正则表达式或if语句,如果我不需要,我宁愿不使用这些方法.思考?