You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP从指定路径获取文件失败求助:多种方法均提示文件不存在

问题:PHP无法读取指定路径的文件/图片

我想通过PHP从指定路径获取文件/图片,已尝试file_get_contents、opendir、readdir等多种方法,但均未解决问题,恳请协助。以下是我使用的静态和动态文件路径:

$static_file_path = 'http://100.100.10.101/dev/livedev/srm/trunk/FILE_UPLOADS/2024/06/27/EBS_sep3.csv';
$dynamic_file_path = base_url()."FILE_UPLOADS/2024/06/27/EBS_sep3.csv";

我尝试了以下两种方法,均提示文件不存在:

方法一

public function getfilename2(){
    //$file_path = 'http://100.100.10.101/dev/livedev/srm/trunk/FILE_UPLOADS/2024/06/27/EBS_sep3.csv';
    $file_path = base_url()."FILE_UPLOADS/2024/06/27/EBS_sep3.csv";

    // Open a file for reading
    $handle = fopen($file_path, "r");

    // Read data from the file
    while (($line = fgets($handle)) !== false) {
        echo $line;
    }

    // Close the file handle
    fclose($handle);
}

方法二

public function getfilename(){ 
    // $file_path = 'http://100.100.10.101/dev/livedev/srm/trunk/FILE_UPLOADS/2024/06/27/EBS_sep3.csv'; 
    $file_path = base_url()."FILE_UPLOADS/2024/06/27/EBS_sep3.csv";

    if (file_exists($file_path)) {
        // Set headers for CSV file download
        header('Content-Description: File Transfer');
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
        header('Content-Length: ' . filesize($file_path));
        header('Cache-Control: private, max-age=0, must-revalidate');
        header('Pragma: public');

        // Clear output buffer
        ob_clean();
        flush();

        // Read the file and output it to the browser
        readfile($file_path);
        exit;
    } else {
        // File not found
        echo "File not found at path: " . $file_path;
    }
}

内容的提问来源于stack exchange,提问作者Niranajan C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:42:37