Php require all files in dir

Hello every one

«require_once» and «require» are language constructs and not functions. Therefore they should be written without «()» brackets!

require_once may not work correctly inside repetitive function when storing variable for example:

function foo () require_once( ‘var.php’ );
return $foo ;
>

to make sure variable bar available at each function call , replace require once with require. eg situation : https : //stackoverflow.com/questions/29898199/variables-not-defined-inside-function-on-second-time-at-foreach

function foo () require( ‘var.php’ );
return $foo ;
>

> php check2 . php
result :
bar
bar
bar
bar
bar

There’s been a lot of discussion about the speed differences between using require_once() vs. require().
I was curious myself, so I ran some tests to see what’s faster:
— require_once() vs require()
— using relative_path vs absolute_path

I also included results from strace for the number of stat() system calls. My results and conclusions below.

METHODOLOGY:
————
The script (test.php):
$start_time = microtime ( true );

/*
* Uncomment one at a time and run test below.
* sql_servers.inc only contains define() statements.
*/

//require (‘/www/includes/example.com/code/conf/sql_servers.inc’);
//require (‘../../includes/example.com/code/conf/sql_servers.inc’);
//require_once (‘/www/includes/example.com/code/conf/sql_servers.inc’);
//require_once (‘../../includes/example.com/code/conf/sql_servers.inc’);

$end_time = microtime ( true );

$handle = fopen ( «/tmp/results» , «ab+» );
fwrite ( $handle , ( $end_time — $start_time ) . «\n» );
fclose ( $handle );
?>

The test:
I ran ab on the test.php script with a different require*() uncommented each time:
ab -n 1000 -c 10 www.example.com/test.php

RESULTS:
———
The average time it took to run test.php once:
require(‘absolute_path’): 0.000830569960420
require(‘relative_path’): 0.000829198306664
require_once(‘absolute_path’): 0.000832904849136
require_once(‘relative_path’): 0.000824960252097

The average was computed by eliminating the 100 slowest and 100 fastest times, so a total of 800 (1000 — 200) times were used to compute the average time. This was done to eliminate any unusual spikes or dips.

The question of how many stat() system calls were made can be answered as follows:
— If you run httpd -X and then do an strace -p , you can view the system calls that take place to process the request.
— The most important thing to note is if you run test.php continuously (as the ab test does above), the stat() calls only happen for the first request:

first call to test.php (above):
——————————-
lstat64 («/www», lstat64 («/www/includes», lstat64 («/www/includes/example.com», lstat64 («/www/includes/example.com/code», lstat64 («/www/includes/example.com/code/conf», .
lstat64 («/www/includes/example.com/code/conf/sql_servers.inc», open («/www/includes/example.com/code/conf/sql_servers.inc», O_RDONLY) = 17

subsequent calls to test.php:
——————————
open («/www/includes/example.com/code/conf/sql_servers.inc», O_RDONLY) = 17

— The lack of stat() system calls in the subsequent calls to test.php only happens when test.php is called continusly. If you wait a certain period of time (about 1 minute or so), the stat() calls will happen again.
— This indicates that either the OS (Ubuntu Linux in my case), or Apache is «caching» or knows the results of the previous stat() calls, so it doesn’t bother repeating them.
— When using absolute_path there are fewer stat() system calls.
— When using relative_path there are more stat() system calls because it has to start stat()ing from the current directory back up to / and then to the include/ directory.

CONCLUSIONS:
————
— Try to use absolute_path when calling require*().
— The time difference between require_once() vs. require() is so tiny, it’s almost always insignificant in terms of performance. The one exception is if you have a very large application that has hundreds of require*() calls.
— When using APC opcode caching, the speed difference between the two is completely irrelevant.
— Use an opcode cache, like APC!

Konstantin Rozinov
krozinov [at] gmail

Источник

Require all files in dir php

If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0: // Check file size if ($_FILES[«fileToUpload»][«size»] > 500000) < echo "Sorry, your file is too large."; $uploadOk = 0; >Limit File Type The code below only allows users to upload JPG, JPEG, PNG, and GIF files. The form: First file (public_folder/mysite/process_login.php): Second file (../../includes/process_login.php):

PHP File Upload

With PHP, it is easy to upload files to the server.

However, with ease comes danger, so always be careful when allowing file uploads!

Configure The «php.ini» File

First, ensure that PHP is configured to allow file uploads.

In your «php.ini» file, search for the file_uploads directive, and set it to On:

Create The HTML Form

Next, create an HTML form that allow users to choose the image file they want to upload:

Some rules to follow for the HTML form above:

  • Make sure that the form uses method=»post»
  • The form also needs the following attribute: enctype=»multipart/form-data». It specifies which content-type to use when submitting the form

Without the requirements above, the file upload will not work.

  • The type=»file» attribute of the tag shows the input field as a file-select control, with a «Browse» button next to the input control

The form above sends data to a file called «upload.php», which we will create next.

Create The Upload File PHP Script

The «upload.php» file contains the code for uploading a file:

$target_dir = «uploads/»;
$target_file = $target_dir . basename($_FILES[«fileToUpload»][«name»]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST[«submit»])) <
$check = getimagesize($_FILES[«fileToUpload»][«tmp_name»]);
if($check !== false) <
echo «File is an image — » . $check[«mime»] . «.»;
$uploadOk = 1;
> else <
echo «File is not an image.»;
$uploadOk = 0;
>
>
?>

  • $target_dir = «uploads/» — specifies the directory where the file is going to be placed
  • $target_file specifies the path of the file to be uploaded
  • $uploadOk=1 is not used yet (will be used later)
  • $imageFileType holds the file extension of the file (in lower case)
  • Next, check if the image file is an actual image or a fake image

Note: You will need to create a new directory called «uploads» in the directory where «upload.php» file resides. The uploaded files will be saved there.

Check if File Already Exists

Now we can add some restrictions.

First, we will check if the file already exists in the «uploads» folder. If it does, an error message is displayed, and $uploadOk is set to 0:

// Check if file already exists
if (file_exists($target_file)) <
echo «Sorry, file already exists.»;
$uploadOk = 0;
>

Limit File Size

The file input field in our HTML form above is named «fileToUpload».

Now, we want to check the size of the file. If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0:

// Check file size
if ($_FILES[«fileToUpload»][«size»] > 500000) <
echo «Sorry, your file is too large.»;
$uploadOk = 0;
>

Limit File Type

The code below only allows users to upload JPG, JPEG, PNG, and GIF files. All other file types gives an error message before setting $uploadOk to 0:

Complete Upload File PHP Script

The complete «upload.php» file now looks like this:

$target_dir = «uploads/»;
$target_file = $target_dir . basename($_FILES[«fileToUpload»][«name»]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST[«submit»])) <
$check = getimagesize($_FILES[«fileToUpload»][«tmp_name»]);
if($check !== false) <
echo «File is an image — » . $check[«mime»] . «.»;
$uploadOk = 1;
> else <
echo «File is not an image.»;
$uploadOk = 0;
>
>

// Check if file already exists
if (file_exists($target_file)) <
echo «Sorry, file already exists.»;
$uploadOk = 0;
>

// Check file size
if ($_FILES[«fileToUpload»][«size»] > 500000) <
echo «Sorry, your file is too large.»;
$uploadOk = 0;
>

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) <
echo «Sorry, your file was not uploaded.»;
// if everything is ok, try to upload file
> else <
if (move_uploaded_file($_FILES[«fileToUpload»][«tmp_name»], $target_file)) <
echo «The file «. htmlspecialchars( basename( $_FILES[«fileToUpload»][«name»])). » has been uploaded.»;
> else <
echo «Sorry, there was an error uploading your file.»;
>
>
?>

Complete PHP Filesystem Reference

For a complete reference of filesystem functions, go to our complete php filesystem Reference.

Php require path to file Code Example, how to get full path of uploaded file in php. php file_get_contents follow redirect. absolute path php. current pathinfo php. php include file in parent directory. get absolute path php file. get file name from file path in php. php parse url get path. add php to path.

Источник

PHP Include and Require

In this chapter, we will learn how to include the content of a PHP file into another PHP file.

Why Including? (or Requiring)

Let’s assume that you have a simple blog which consists of three main parts; header, body, and footer. If you needed to change the header of each page are you going to open all the files for each page and update the header? No, you will need to keep the header in a single file and include it in the other files.

PHP Including Example

Simply, including and requiring is used to reuse PHP files. It makes handling projects very easy as we can change the layout with just changing a single file.

How to Include Files? (or Require)

There are 4 statements in PHP that is used for this purpose.

include vs require

The include and require statements are identical except their error handling behavior when the file was not found.

  • include produces a warning and continues execution of the script
  • require produces a fatal error and stops the execution of the script

If the included script is needed to do the next processes, you should use require .

_once

By adding _once to include or require , you can give a new rule for those statements; «Include the file only if it is not already included».

For instance, when you use require_once , it checks if that file is already required (or included) in the current script. If no, the file will be required. Otherwise, the statement will be skipped.

It is a good practice to use require_once , as it stops the script if the file is not found and it will only require a file once. But, this depends on your need. If you need to include the same file multiple times, you should use include or require .

How to use Include and Require?

For this example, we will create template files to include. (header.php and footer.php)

1. include

PHP include Example

    

I'm using the include statement

2. require

PHP require Example

    

I'm using the require statement

3. include_once

PHP include_once Example

    

I'm using the include_once statement

4. require_once

PHP require_once Example

     

I'm using the require_once statement for header and include for footer. No matter what we used, require_once checks if the file was included or required earlier. So, last require_once has no effect

Tips on Including (or Requiring)

Источник

Читайте также:  Revit api python уроки
Оцените статью