How to return a file in PHP
I think you want this:
$attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=file.zip");
readfile($attachment_location);
die();
} else {
die("Error: File not found.");
}
PHP rest api how to return a file or download
From your download file URL $request_url
.
This is force download source code for server side.
// https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($request_url) . ".jpg\"");
readfile($request_url);
exit();// end process to prevent any problems.
This is source code for download working on client side.
$url = 'https://my-domain.tld/rest-api/path.php';// change this to your REST API URL.
// https://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl
$fileext = pathinfo($url, PATHINFO_EXTENSION);
echo $fileext;
set_time_limit(0);
// For save data to temp file.
$fp = fopen (__DIR__ . '/localfile.tmp', 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// get curl response
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
fwrite($fp, $body);
curl_close($ch);
fclose($fp);
// rename temp file.
preg_match('/filename=[\'"]?([\w\d\.\-_]+)[\'"]?/i', $header, $matches);
var_dump($matches);
if (isset($matches[1]) && !is_file(__DIR__ . '/' . $matches[1])) {
// if target rename file is not exists.
rename(__DIR__ . '/localfile.tmp', __DIR__ . '/' . $matches[1]);
} else {
// for debug
echo 'something wrong!<br>';
var_dump($matches);
echo '<br>';
echo 'File was downloaded into ' . __DIR__ . '/localfile.tmp';
exit();
}
Return from include file
includeme.php:
$x = 5;
return $x;
main.php:
$myX = require 'includeme.php';
also gives the same result
This is one of those little-known features of PHP, but it can be kind of nice for setting up really simple config files.
How to return an included file in PHP without using a return
Your problem is that using include essentially treats your template.php file as a single giant "echo" because there is no value returned inside template.php
You could change the template.php file to return a HTML string, and it would achieve what you want, but this is a nightmare to look at and manage.
return '<div>
<br/>' . $data . '<br/>
</div>';
An alternative, is to "capture" the output of the included file. Its a lot nicer on your template.php file and achieves the same thing. The output buffer functions essentially capture any information "echoed" - you need to be careful using this and the placement of your session_start however as it can catch you off guard depending on how your application is bootstrapped.
This is done by using three ob_ functions the main one being ob_get_contents which will be the contents of template.php in the example below.
http://php.net/manual/en/function.ob-get-contents.php
ob_start();
include $_SERVER['DOCUMENT_ROOT'] . 'template.php';
$html = ob_get_contents();
ob_end_clean();
This was your template files stay nice and clean, and the contents are all captured for returning in your json format.
How to return non blade based view in laravel?
I have custom php file (index.php and not index.blade.php) with HTML & PHP codes. How I can use this file as view in laravel and return it?
I would strongly recommend to use Laravel's Blade Engine and stick to the MVC principle of separating your view from your business logic.
However, if you have to use a custom PHP file instead, then you could do it like this:
class YourController extends Controller
{
public function index()
{
$content = require 'path/to/your/index.php';
return $content;
}
}
If your path leads to the resource directory you could use the resource_path helper:
$content = require resource_path('views/index.php');
Related Topics
Request Headers Bag Is Missing Authorization Header in Symfony 2
Get the Last Modified Date of a Remote File
Compile a Referenced Less File into CSS with PHP Automatically
How to Call a Controller Function Inside a View in Laravel 5
MySQL Error "Too Many Connections"
Another Twitter Oauth Curl Access Token Request That Fails
Php.Exe' Is Not Recognized as an Internal or External Command, Operable Program or Batch File
What Is a Good Parser Generator for PHP
How to Write a Stored Procedure in PHPmyadmin
Passing PHP Objects to JavaScript
Get Value of Dynamically Chosen Class Constant in PHP
Access a Global Variable in a PHP Function
Accessing Variables and Methods Outside of Class Definitions
How to Encrypt PHP Source or Compile It So Others Can't See It? and How
$_Files Field 'Tmp_Name' Has No Value on .Jpg File Extension