scripts examples

This commit is contained in:
2024-12-10 11:16:23 +01:00
parent 4fa387e8af
commit 513b4645c7
2 changed files with 177 additions and 0 deletions

146
_paths.php Normal file
View File

@@ -0,0 +1,146 @@
<?php
function _pr($d) {
echo "<div style='border: 1px solid#ccc; padding: 10px;'>";
echo '<strong>' . debug_backtrace()[0]['file'] . ' ' . debug_backtrace()[0]['line'] . '</strong>';
echo "</div>";
echo '<pre>';
if(is_array($d)) {
print_r($d);
} else if(is_object($d)) {
var_dump($d);
}
echo '</pre>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<style>
pre {
background: #f4f4f4;
border: 1px solid #ddd;
border-left: 3px solid #f36d33;
color: #666;
page-break-inside: avoid;
font: normal normal 14px/16px "Courier New", Courier, Monospace;
line-height: 1.0;
margin-bottom: 1.6em;
max-width: 100%;
padding: .5em 1em;
display: block;
word-wrap: break-word;
margin-left: 30px;
overflow: auto;
overflow-x: auto;
white-space: pre-wrap;
counter-reset: line;
}
h3 {
font-family: sans-serif;
font-weight: 400;
margin-top: 2em;
}
q {
font-family: sans-serif;
margin-bottom: 1em;
padding-bottom: 1em;
font-style: italic;
margin-left: 2em;
}
a:link, a:visited {
color: #858585;
}
a:hover {
color: #373737;
}
a:active {
color: #c91717;
}
</style>
</head>
<body>
<?php
// https://www.php.net/manual/fr/function.dirname.php
// Renvoie le chemin du dossier parent
// dirname()
// https://www.php.net/manual/fr/function.basename.php
// Retourne le nom de la composante finale d'un chemin
// basename()
// https://www.php.net/manual/fr/function.pathinfo.php
// Retourne des informations sur un chemin système
//pathinfo()
echo '<h3>pathinfo(__FILE__)</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/function.pathinfo.php">Retourne des informations sur un chemin système</a></q>' . PHP_EOL;
_pr(pathinfo(__FILE__));
// https://www.php.net/manual/fr/function.realpath.php
// Retourne le chemin canonique absolu
// realpath()
echo '<h3>realpath('.')</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/function.realpath.php">Retourne le chemin canonique absolu</a></q>' . PHP_EOL;
echo '<pre>' . realpath('.') . '</pre>' . PHP_EOL;
// https://www.php.net/manual/fr/function.parse-url.php
// Analyse une URL et retourne ses composants
// parse_url()
echo '<h3>__FILE__</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/language.constants.magic.php">Le chemin complet et le nom du fichier courant avec les liens symboliques résolus</a></q>' . PHP_EOL;
echo '<pre>' . __FILE__ . '</pre>' . PHP_EOL; // /Users/bruno/Sites/sls/paths.php
echo '<h3>__DIR__</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/language.constants.magic.php">Le dossier du fichier</a></q>' . PHP_EOL;
echo '<pre>' . __DIR__ . '</pre>' . PHP_EOL; // /Users/bruno/Sites/sls
echo '<h3>dirname(__FILE__)</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/function.dirname.php">Renvoie le chemin du dossier parent</a></q>' . PHP_EOL;
echo '<pre>' . dirname(__FILE__) . '</pre>' . PHP_EOL; // /Users/bruno/Sites/sls
echo '<h3>getcwd()</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/function.getcwd.php">Retourne le dossier de travail courant</a></q>' . PHP_EOL;
echo '<pre>' . getcwd() . '</pre>' . PHP_EOL; // /Users/bruno/Sites/sls
// https://www.php.net/manual/en/features.file-upload.post-method.php
echo '<h3>$_FILES</h3>' . PHP_EOL;
_pr($_FILES); // array vide
echo '<h3>$_SERVER["DOCUMENT_ROOT"]</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/reserved.variables.server.php">La racine sous laquelle le script courant est exécuté, comme défini dans la configuration du serveur</a></q>' . PHP_EOL;
echo '<pre>' . $_SERVER["DOCUMENT_ROOT"] . '</pre>' . PHP_EOL; // /Users/bruno/Sites/sls
echo '<h3>$_SERVER["SCRIPT_FILENAME"]</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/reserved.variables.server.php">Le chemin absolu vers le fichier contenant le script en cours d\'exécution</a></q>' . PHP_EOL;
echo '<pre>' . $_SERVER["SCRIPT_FILENAME"] . '</pre>' . PHP_EOL; // /Users/bruno/Sites/sls/paths.php
echo '<h3>$_SERVER["SCRIPT_NAME"]</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/reserved.variables.server.php">Contient le nom du script courant. Cela sert lorsque les pages doivent s\'appeler elles-mêmes. La constante __FILE__ contient le chemin complet ainsi que le nom du fichier (i.e. inclut) courant</a></q>' . PHP_EOL;
echo '<pre>' . $_SERVER["SCRIPT_NAME"] . '</pre>' . PHP_EOL; // /paths.php
echo '<h3>$_SERVER["PHP_SELF"]</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/reserved.variables.server.php">Le nom du fichier du script en cours d\'exécution, par rapport à la racine web</a></q>' . PHP_EOL;
echo '<pre>' . $_SERVER["PHP_SELF"] . '</pre>' . PHP_EOL; // /paths.php
echo '<h3p>$_SERVER["PATH"]</h3>' . PHP_EOL;
echo '<q><a href="https://www.php.net/manual/fr/reserved.variables.server.php">Renvoie le chemin du dossier parent</a></q>' . PHP_EOL;
echo '<pre>' . $_SERVER["PATH"] . '</pre>' . PHP_EOL; // /opt/homebrew/bin:/opt/homebrew/sbin:/usr/bin:/bin:/usr/sbin:/sbin
?>
</body>
</html>