没有这样的文件或目录

No such file or directory

提问人:Harsh Gupta 提问时间:10/10/2023 最后编辑:Harsh Gupta 更新时间:10/10/2023 访问量:81

问:

在代码中,我无法确定路径出了什么问题,因此在 Appwrite 中运行该函数会导致文件未找到问题

我参考了这段代码 https://github.com/appwrite/templates/blob/main/php/sync-with-algolia/src/utils.php

<?php

$staticFolder = dirname(__FILE__) . '/../static';
/**
 * Returns the contents of a file in the static folder
 * @param string $fileName
 * @return string Contents of static/{$fileName}
 */
function get_static_file(string $fileName): string
{
    global $staticFolder;
    $file = $staticFolder . '/' . $fileName;
    return file_get_contents($file);
}

/**
 * Throws an exception if any of the keys are missing from the object
 * @param array|object $obj
 * @param string[] $keys
 * @throws Exception
 */
function throw_if_missing(mixed $obj, array $keys): void
{
    $missing = [];
    foreach ($keys as $key) {
        if (!isset($obj[$key]) || empty($obj[$key])) {
            $missing[] = $key;
        }
    }
    if (count($missing) > 0) {
        throw new Exception('Missing required fields: ' . implode(', ', $missing));
    }
}

/**
 * Interpolates values into a template string.
 * @param string $template The template string containing placeholders like "{{key}}".
 * @param array<string, string|null> $values An associative array with keys and values to replace in the template.
 * @return string The interpolated string with placeholders replaced by corresponding values.
 */
function interpolate(string $template, array $values): string
{
    return preg_replace_callback('/{{([^}]+)}}/', function ($matches) use ($values) {
        $key = $matches[1];
        return isset($values[$key]) ? $values[$key] : '';
    }, $template);
}

Directory Structure

以下是我在Appwrite中运行函数并使用/GET请求执行时发生的错误enter image description hereenter image description here

PHP 函数 appwrite

评论

6赞 RiggsFolly 10/10/2023
请不要发布代码/数据/错误消息的图像或站外链接。请编辑您的问题,并将文本复制/粘贴到问题中,格式化。这样我们就可以尝试重现问题,而不必重新键入所有内容,并且屏幕阅读器可以正确索引或读取您的问题。
0赞 droopsnoot 10/10/2023
不要忘记物理路径和虚拟路径之间的区别 - 您尝试打开的文件位于物理文件系统的根目录中,而不是站点的虚拟根目录中。
0赞 Harsh Gupta 10/10/2023
您能否详细说明我遇到的问题,我也编辑了这个问题
0赞 Tamil Selvan C 10/10/2023
函数内的全局变量$staticFolder返回 null
1赞 symcbean 10/10/2023
stackoverflow.com/help/minimal-reproducible-example

答: 暂无答案