-5

I want to retrieve a file/image from specific path in php, I was tried different ways like file_get_contents and opendir and readdir like etc , but unable to find any solutions, Can you please help on this issue. Below are the static and dynamic file paths adding.


         $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";

I was tried below methods all are showing file not exist .

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);

}

way2 below

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;
}

}

8
  • what does base_url function return Commented Jun 28, 2024 at 11:43
  • 2
    The primary issue in your code is that the file_exists function and the readfile function are intended to work with the filesystem path, not a URL. When you use base_url() to generate the file path, it creates a URL, but file_exists and readfile need a local filesystem path. Commented Jun 28, 2024 at 11:45
  • @desboisrob readfile can be used with HTTP URLs, if allow_url_fopen is set to true. file_exists however won't work with HTTP URLs, because as php.net/manual/en/wrappers.http.php shows, the wrapper does not support stat functionality. Commented Jun 28, 2024 at 11:48
  • A guess: File path is more likely: __DIR__.'/relative_path/to/file.csv'. __DIR__ is the home directory of your php script. Commented Jun 28, 2024 at 11:48
  • Any one of you Could you please suggest how to retrieve file from specific path in codeigniter Please Commented Jun 28, 2024 at 11:49

1 Answer 1

-1

With file_get_contents....?

<?
$content = @file_get_contents("http://.../example.csv");

if($content)
{
    echo "<pre>";
    echo $content;
    echo "</pre>";
}
else echo "No File";
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Why include the error suppression operator?
@David To avoid the warning message
@Juan .. If I want to retrieve image also is this script work ??
@NiranajanC You have the contents of the remote file with this simple script. Afterwards it is possible to save this content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.