PHP 14 week function learning record

Time:2020-11-22

filter_has_var()

effect

filter_has_var()Function to check the existence of a variable of the specified input type.

usage

filter_has_var(type, variable)

type

  • INPUT_GET
  • INPUT_POST
  • INPUT_COOKIE
  • INPUT_SERVER
  • INPUT_ENV

case

<?php
if(!filter_has_var(INPUT_GET, "name"))
{
echo("Input type does not exist");
}
else
{
echo("Input type exists");
}
?>

result

Input type exists

filter_id()

effect

filter_id()Function returns theIDnumber.

usage

filter_id(filter_name)

case

<?php
echo(filter_id("validate_email"));
?>

result

274

filter_input()

effect

filter_input()Function takes input (such as form input) from outside the script and filters it.

usage

filter_input(input_type, variable, filter, options)

case

<?php
if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
{
echo "E-Mail is not valid";
}
else
{
echo "E-Mail is valid";
}
?>

result

E-Mail is valid

filter_input_array()

effect

filter_input_array()Function takes multiple inputs (such as form input) from outside the script and filters them.

usage

filter_input_array(input_type, filter_args)

case

<?php
$filters = array
(
"name" => array
(
"filter"=>FILTER_CALLBACK,
"flags"=>FILTER_FORCE_ARRAY,
"options"=>"ucwords"
),
"age" => array
(
"filter"=>FILTER_VALIDATE_INT,
"options"=>array
(
"min_range"=>1,
"max_range"=>120
)
),
"email"=> FILTER_VALIDATE_EMAIL,
);
print_r(filter_input_array(INPUT_POST, $filters));
?>

result

Array
(
[name] => Peter
[age] => 41
[email] => [email protected].com
)

filter_list()

effect

filter_list()Function returns an array containing all supported filters.

usage

filter_list()

case

<?php
print_r(filter_list());
?>

result

Array
(
[0] => int
[1] => boolean
[2] => float
[3] => validate_regexp
[4] => validate_url
[5] => validate_email
[6] => validate_ip
[7] => string
[8] => stripped
[9] => encoded
[10] => special_chars
[11] => unsafe_raw
[12] => email
[13] => url
[14] => number_int
[15] => number_float
[16] => magic_quotes
[17] => callback
)

filter_var_array()

effect

filter_var_array()Function gets multiple variables and filters them.

usage

filter_var_array(array, args)

case

<?php
$arr = array
(
    "name" => "peter griffin",
    "age" => "41",
    "email" => "[email protected]",
);

$filters = array
(
    "name" => array
            (
            "filter"=>FILTER_CALLBACK,
            "flags"=>FILTER_FORCE_ARRAY,
            "options"=>"ucwords"
            ),
    "age" => array
            (
                "filter"=>FILTER_VALIDATE_INT,
                "options"=>array
                        (
                        "min_range"=>1,
                        "max_range"=>120
                        )
            ),
    "email"=> FILTER_VALIDATE_EMAIL,
);

print_r(filter_var_array($arr, $filters));
?>

result

Array
(
    [name] => Peter Griffin
    [age] => 41
    [email] => [email protected].com
)

filter_var()

effect

filter_var()Function to filter a variable through a specified filter.

usage

filter_var(variable, filter, options)

case

<?php
if(!filter_var("[email protected]", FILTER_VALIDATE_EMAIL))
{
    echo("E-mail is not valid");
}
else
{
    echo("E-mail is valid");
}
?>

result

E-mail is not valid

ftp_alloc()

effect

ftp_alloc()Function to upload toFTPFile allocation space for the server.

usage

ftp_alloc(ftp_connection,size,return)

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

ftp_alloc($conn,"160",$response);
echo $response;

ftp_close($conn);
?>

ftp_cdup()

effect

ftp_cdup()Function to change the current directory toFTPThe parent directory on the server.

usage

ftp_cdup(ftp_connection)

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

//Outputs the current directory
echo "Dir: ".ftp_pwd($conn);
echo "<br />";
//Change to the images directory
ftp_chdir($conn,"images");
echo "Dir: ".ftp_pwd($conn);
echo "<br />";
//Change current directory to parent directory
ftp_cdup($conn);
echo "Dir: ".ftp_pwd($conn);

ftp_close($ftp_server);
?>

result

Dir: /
Dir: /images
Dir: /

ftp_chdir()

effect

ftp_chdir()Function changeFTPThe current directory on the server is the specified directory.

usage

ftp_chdir(ftp_connection,directory)

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

//Outputs the current directory
echo "Dir: ".ftp_pwd($conn);
echo "<br />";
//Change to the images directory
ftp_chdir($conn,"images");
echo "Dir: ".ftp_pwd($conn);

ftp_close($ftp_server);
?>

result

Dir: /
Dir: /images

ftp_chmod()

effect

ftp_chmod()Function settingsFTPPermissions for the specified file on the server.

usage

ftp_chmod(ftp_connection,mode,file)

Mode required. Define new authority.

The mode parameter consists of four numbers

  • The first number is usually 0
  • The second number specifies the authority of the owner
  • The third number specifies the permissions of the user group to which the owner belongs
  • The fourth number defines the authority of the other owners

Possible values (to set more than one permission, please sum up the following numbers:

  • 1 = Execution Authority
  • 2 = write permission
  • 4 = read permission

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"user","pass");

// Read and write for owner, nothing for everybody else
ftp_chmod($conn,"0600","test.txt");

// Read and write for owner, read for everybody else
ftp_chmod($conn,"0644","test.txt");

// Everything for owner, read and execute for everybody else
ftp_chmod($conn,"0755","test.txt");

// Everything for owner, read for owner's group
ftp_chmod($conn,"0740","test.txt");

ftp_close($conn);
?>

ftp_close()

effect

ftp_close()Function offFTPconnect.

usage

ftp_close(ftp_connection)

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
//some code to be executed
ftp_close($conn);
?>

ftp_delete()

effect

ftp_delete()Function deleteFTPA file on the server.

usage

ftp_delete(ftp_connection,file)

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

echo ftp_delete($conn,"test.txt");

ftp_close($conn);
?>

result

1

ftp_connect()

effect

ftp_connect()Function openFTPconnect.

usage

ftp_connect(host,port,timeout)

case

<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
?>

ftp_exec()

effect

ftp_exec()Function request inFTPA program or command is executed on the server.

usage

ftp_exec(ftp_connection,command)

case

<?php
$command = "ls-al > test.txt";
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

if (ftp_exec($conn,$command))
{
echo "Command executed successfully";
}
else
{
echo "Execution of command failed";
}

ftp_close($conn);
?>

source

Rookie course

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint