>我正在尝试打开连接到不同用户名的目录中的最新文件。但是,有些用户名没有上传目录,我想跳过这些用户或创建目录无论哪个都无关紧要。这是我当前的代码。
<?php $path = base_url(''.$value['username']) . '/' . 'uploaded';)
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}?>
上传时我收到的错误是
A PHP Error was encountered
Severity: Warning
Message: dir(https://www.domain.com/pdfs/bob/uploaded): failed to open dir: not implemented
Filename: views/search.php
Line Number: 71
我尝试搜索并尝试了一些我发现不起作用的 MKDIR 东西。甚至是我在堆栈溢出上找到的最新文件。
如果不存在,如何在 php 中创建目录。
if (!is_dir('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
在这里我描述你的代码..
<?php $path = base_url(''.$value['username']) . '/' . 'uploaded';)
$latest_ctime = 0;
$latest_filename = '';
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
?>
注意:您必须在is_dir()
和mkdir()
函数中传递路径。您不能在这些函数中传递 URL。
在
尝试打开目录之前,您应该检查它是否存在。尝试使用 is_dir(),例如:
if (is_dir($path)) {
$d = dir($path);
} else {
mkdir($path, 0700); // Change the permission as you need.
$d = dir($path); // Since the folder is being just created you could return
}
如果您希望跳过这些用户,则可以更改else{}
中的代码以exit
脚本或任何您喜欢的内容。
在 PHP 中,dir() 适用于文件系统,而不是 URL。您需要使用的内容将取决于文件的确切位置以及托管的设置方式。根据信息尝试如下操作:
$path = '<wwwroot>/pdfs/' . $value['username'] . '/uploaded';
这应该会给你一个目录对象(如果存在)。如果你不知道你的www根是什么,你可以尝试这样的事情:
$path = __DIR__ . '/pdfs/' . $value['username'] . '/uploaded';
这将基于您正在执行的文件的当前目录。
您正在尝试从 URL 读取目录。您需要将文件系统中目录的路径传递给它。例如。
'/home/your_app/pdfs/' . $value['username']) . '/' . 'uploaded'