Simulate file structure with PHP
I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.
In your htaccess use like:
<IfModule mod_rewrite.c>
RewriteEngine On
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as
class URI
{
var $uri;
var $segments = array();
function __construct()
{
$this->uri = $_SERVER['REQUEST_URI'];
$this->segments = explode('/',$this->uri);
}
function getSegment($id,$default = false)
{
$id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
return isset($this->segments[$id]) ? $this->segments[$id] : $default;
}
}
Use like
http://mysite.com/posts/22/robert-pitt-shows-mvc-style-uri-access
$Uri = new URI();
echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'
Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param
Hope this helps you move forward with your application.
PHP: Serve pages without .php files in file structure
Sounds like you need the Front Controller pattern.
Basically every URL gets redirected to one PHP page that determines what to do with it. You can use Apache mod_rewrite for this with this .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
That redirects everything except static content files to index.php. Adjust as required.
If you just want to affect the URL /new-page then try something like:
RewriteEngine on
RewriteBase /
RewriteRule ^new-page/ myhandler.php
Any URLs starting with "new-page" will be sent to myhandler.php.
Simulating fake directories using PHP, without .htaccess, mod_rewrite, or 404 redirects
Well, I figured out what was going on: I had a misconfigured nginx server.
Marc B's answer, though related to Apache, prompted me to check out my PATH_INFO value — lo and behold, PATH_INFO was nonexistent, even for requests like GET /other.php/something_else
.
Some more searching turned up the answer. Using nginx's fastcgi_split_path_info
to split the URI into a) the path to the script and b) the path that appears following the script name will fail if done after a use of the try_files
directive, due to its rewriting the URI to point to a file on the disk. This obliterates any possibility of obtaining a PATH_INFO string.
As a solution, I did three things (some of these are probably redundant, but I did them all, just in case):
In a server's location block, make use of
fastcgi_split_path_info
before setting up PATH_INFO viafastcgi_param PATH_INFO $fastcgi_path_info;
...and do both before making use of
try_files
.Store
$fastcgi_path_info
as a local variable in case it ever changes later (e.g. withset $path_info $fastcgi_path_info;
)When using
try_files
, don't use$uri
... use$fastcgi_script_name
. (Pretty sure this was my biggest mistake.)
Sample Configuration
server {
# ... *snip* ...
location ~ \.php {
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
try_files $fastcgi_script_name =404;
}
# ... *snip* ...
}
Where fastcgi_params
contains:
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# ... *snip* ...
Reference
The following links explain the problem pretty well and provide an alternate nginx configuration:
- http://trac.nginx.org/nginx/ticket/321
- http://kbeezie.com/php-self-path-nginx/
How to include PHP files that require an absolute path?
This should work
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/inc/include1.php";
Edit: added imporvement by aussieviking
How can I write tests for file upload in PHP?
I've found an alternate solution. I've spoofed the $_FILES
array with test data, created dummy test files in the tmp/
folder (the folder is irrelevant, but I tried to stick with the default).
The problem was that is_uploaded_file
and move_uploaded_file
could not work with this spoofed items, because they are not really uploaded through POST
.
First thing I did was to wrap those functions inside my own moveUploadedFile
and isUploadedFile
in my plugin so I can mock them and change their return value.
The last thing was to extends the class when testing it and overwriting moveUploadedFile
to use rename
instead of move_uploaded_file
and isUploadedFile
to use file_exists
instead of is_uploaded_file
.
PHP script to generate a file with random data of given name and size?
To start you could try something like this:
function generate_file($file_name, $size_in_bytes)
{
$data = str_repeat(rand(0,9), $size_in_bytes);
file_put_contents($file_name, $data); //writes $data in a file
}
This creates file filled up with a random digit (0-9).
Related Topics
Best Way to Check for Positive Integer (Php)
Correct Format for Strings/Numbers Beginning with Zero
How to Get Date from Excel Using PHPexcel Library
Accessing a Variable Defined in a Parent Function
Mp4 Plays When Accessed Directly, But Not When Read Through PHP, on iOS
PHP Jquery Ajax Call Throws Net::Err_Empty_Response
How to Get the Server Path to the Web Directory in Symfony2 from Inside the Controller
JSON_Encode Not Working with a HTML String as Value
[PHP Warning: Mail(): " Sendmail_From" Not Set in PHP.Ini or Custom "From:" Header Missing
How to Get User Image with Twitter API 1.1
Difference Between Set_Time_Limit() and Ini_Set('Max_Execution_Time', ...)
Interview Question: in PHP, Is 123==0123
How to Add Commas to Numbers in PHP
Symfony 2 Load Different Template Depending on User Agent Properties
Subtracting a Certain Number of Hours, Days, Months or Years from Date