Conexión
nueva conexión |
$conn = new mysqli("localhost", "root", "", "tiendao"); |
mostrar error de conexión |
$conn->connect_error |
matar conexión |
die(); |
Consultas desde PHP a BD
crear consulta |
$query = $conexion->query($sql); |
número de filas de la consulta |
$query->num_rows |
mientras haya filas, guardar array |
while( $row = $consulta->fetch_assoc() ) |
imprimir valor dentro de la clave codigo |
echo $row['codigo'] |
Cookies y sesiones
crear cookie |
//nombre, valor, expires, path, domain, secure, httponly setcookie('tema', $tema, time() + (60 * 1)); |
variable de cookie |
$_COOKIE['tema' |
iniciar sesión |
session_start(); |
valor de sesión |
$_SESSION['usuario'] = $_POST['usuario']; |
regenerar id de sesión |
session_regenerate_id(); |
deshacer sesión, detsruír sesión |
session_unset(); session_destroy(); |
declarar tiempo de sesión |
$_SESSION["timeout"] = time(); |
|
$sessionTTL = time() - $_SESSION["timeout"]; |
Rutas
|
spl_autoload_register('mi_autoloader'); include_once $NombreClase . '.php'; $sara = new Persona() |
Namespace |
_DIR_ |
alias |
|
define("RUTA", realpath(dirname(__FILE__)).DS ); |
|
define("DS", DIRECTORY_SEPARATOR); |
consultas
bloqueo query |
transaction |
bloqueo tabla |
ficheros
crear |
$f = fopen($obj->ruta,'a+') |
escribir |
fwrite($f,serialize($o).PHP_EOL); |
leer |
$f = fopen($this->ruta,'r'); |
|
while(!feof($f)){ |
|
$u = unserialize(fgets($f)); |
|
$a[] = $u; |
- |
|
$no_of_lines = count(file($file)); |
subir |
<input type="file" id="docpicker" accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"> |
|
<input type="file" accept="image/*,.pdf"> |
|
<label for="file">Choose file to upload</label> <input type="file" id="file" name="file" multiple> |
|
accept="image/png" or accept=".png" |
|
<input type="file" id="image_uploads" name="image_uploads" accept=".jpg, .jpeg, .png" multiple> |
|
The selected files' are returned by the element's HTMLInputElement.files property, which is a FileList object containing a list of File objects. The FileList behaves like an array, so you can check its length property to get the number of selected files. |
|
<?php $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; } } |
|
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; |
|
if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } |
|
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } |
guardar |
borrar |
obtener log |
cerrar stream |
fclose($f); |
error
personalizados |
if($_REQUEST['ar']=="") throw new Error(); |
php-ini
|
<? phpinfo(); ?> |
log |
en ficheros |
constantes |
en ficheros |
htaccess |
|
file_uploads = On |
|
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> |
Controlador
enviar a misma página |
<form action="index.php" method="POST"> |
name="enviar" en submit |
if(isset($_REQUEST['enviar'])){ |
|
<input type="text" name="url" placeholder="Nombre"><br/> <input type="number" name="edad" placeholder="Edad"><br/> <input type="number" name="edad" placeholder="Edad"><br/> <input type="submit" name="enviar" value="Registrar"> |
Excepciones con ficheros
c |
if(filter_var($_REQUEST['email'],FILTER_VALIDATE_EMAIL)===false) throw new Error_Email(); |
c |
catch(Error_Email $e){echo $e->errorMessage();$e->grabar();} |
e e |
else throw new Exception( |
e e |
include_once('Fichero.php'); class Error_Email extends Exception{ |
e e |
public function errorMessage(){ return 'Error |
e e grabar |
$fichero = new Fichero('error.log'); $fichero->write('Email -> Linea:'.$this->getLine().'Fichero'.$this->getFile()); |
finally |
f wr |
$f = fopen($this->ruta,'a+'); |
|
fwrite($f,serialize($o).PHP_EOL); |
f read |
$f = fopen($this->ruta,'r'); while(!feof($f)){ $u = unserialize(fgets($f)); $a[] = $u; |
c |
for($i=0;$i<count($o)-1;$i++){ $t.="<tr><td>".$o[$i]->nombre."</td> ($o){ <td>".$o[$i]->edad."</td> return $t; |
|
$persona=new Persona($nombre,$edad,$dni); // CLASE PERSONA $fichero=new Fichero('persona.txt'); $fichero->write($persona); echo generarTabla($fichero->read()); |
|