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;
}
}
readfilecan be used with HTTP URLs, ifallow_url_fopenis set to true.file_existshowever won't work with HTTP URLs, because as php.net/manual/en/wrappers.http.php shows, the wrapper does not supportstatfunctionality.__DIR__.'/relative_path/to/file.csv'.__DIR__is the home directory of your php script.