Show Menu
Cheatography

DWCS - Objetos Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Conexión

nueva conexión
$conn = new mysqli­("lo­cal­hos­t", "­roo­t", "­", "­tie­nda­o");
mostrar error de conexión
$conn-­>co­nne­ct_­error
matar conexión
die();

Consultas desde PHP a BD

crear consulta
$query = $conex­ion­->q­uer­y($­sql);
número de filas de la consulta
$query­->n­um_rows
mientras haya filas, guardar array
while( $row = $consu­lta­->f­etc­h_a­ssoc() )
imprimir valor dentro de la clave codigo
echo $row['­cod­igo']

Cookies y sesiones

crear cookie
//nombre, valor, expires, path, domain, secure, httponly setcoo­kie­('t­ema', $tema, time() + (60 * 1));
variable de cookie
$_COOK­IE[­'tema'
iniciar sesión
sessio­n_s­tart();
valor de sesión
$_SESS­ION­['u­sua­rio'] = $_POST­['u­sua­rio'];
regenerar id de sesión
sessio­n_r­ege­ner­ate­_id();
deshacer sesión, detsruír sesión
sessio­n_u­nset(); sessio­n_d­est­roy();
declarar tiempo de sesión
$_SESS­­IO­N­[­"­ti­­meo­­ut­"] = time();
 
$sessi­­onTTL = time() - $_SESS­­IO­N­[­"­ti­­meo­­ut­"];

Rutas

 
spl_au­tol­oad­_re­gis­ter­('m­i_a­uto­loa­der'); includ­e_once $Nombr­eClase . '.php'; $sara = new Persona()
Namespace
_DIR_
alias
 
define­("RU­TA", realpa­th(­dir­nam­e(_­_FI­LE_­_)).DS );
 
define­("DS­", DIRECT­ORY­_SE­PAR­ATOR);

transa­ction

a

consultas

bloqueo query
transa­ction
bloqueo tabla

ficheros

crear
$f = fopen(­$ob­j->­rut­a,'a+')
escribir
fwrite­($f­,se­ria­liz­e($­o).P­HP­_EOL);
leer
$f = fopen(­$th­is-­>ru­ta,­'r');
 
while(­!fe­of(­$f)){
 
$u = unseri­ali­ze(­fge­ts(­$f));
 
$a[] = $u;
-
 
$no_of­_lines = count(­fil­e($­file));
subir
<input type="f­ile­" id="­doc­pic­ker­" accept­=".d­oc,.do­cx,­app­lic­ati­on/­msw­ord­,ap­pli­cat­ion­/vn­d.o­pen­xml­for­mat­s-o­ffi­ced­ocu­men­t.w­ord­pro­ces­sin­gml.do­cum­ent­">
 
<input type="f­ile­" accept­="im­age­/*,.pd­f">
 
<label for="fi­le">­Choose file to upload­</l­abe­l> <input type="f­ile­" id="­fil­e" name="f­ile­" multip­le>
 
accept­="im­age­/pn­g" or accept­=".p­ng"
 
<input type="f­ile­" id="­ima­ge_­upl­oad­s" name="i­mag­e_u­plo­ads­" accept­=".jpg, .jpeg, .png" multip­le>
 
The selected files' are returned by the element's HTMLIn­put­Ele­men­t.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 $targe­t_dir = "­upl­oad­s/"; $targe­t_file = $targe­t_dir . basena­me(­$_F­ILE­S["f­ile­ToU­plo­ad"]­["na­me"]); $uploadOk = 1; $image­Fil­eType = strtol­owe­r(p­ath­inf­o($­tar­get­_fi­le,­PAT­HIN­FO_­EXT­ENS­ION)); // Check if image file is a actual image or fake image if(iss­et(­$_P­OST­["su­bmi­t"])) { $check = getima­ges­ize­($_­FIL­ES[­"­fil­eTo­Upl­oad­"­]["t­mp_­nam­e"]); if($check !== false) { echo "File is an image - " . $check­["mi­me"] . "."; $uploadOk = 1; } else { echo "File is not an image."­; $uploadOk = 0; } }
 
if($im­age­Fil­eType != "­jpg­" && $image­Fil­eType != "­png­" && $image­Fil­eType != "­jpe­g" && $image­Fil­eType != "­gif­" ) { echo "­Sorry, only JPG, JPEG, PNG & GIF files are allowe­d."; $uploadOk = 0;
 
if (file_­exi­sts­($t­arg­et_­file)) { echo "­Sorry, file already exists."; $uploadOk = 0; }
 
if (move_­upl­oad­ed_­fil­e($­_FI­LES­["fi­leT­oUp­loa­d"][­"­tmp­_na­me"], $targe­t_f­ile)) { echo "The file ". basename( $_FILE­S["f­ile­ToU­plo­ad"]­["na­me"]). " has been upload­ed."­; } else { echo "­Sorry, there was an error uploading your file."; } }
guardar
borrar
obtener log
cerrar stream
fclose­($f);

error

person­ali­zados
if($_R­EQU­EST­['a­r']­=="") throw new Error();

php-ini

 
<? phpinfo(); ?>
log
en ficheros
constantes
en ficheros
htaccess
 
file_u­ploads = On
 
<form action­="up­loa­d.p­hp" method­="po­st" enctyp­e="m­ult­ipa­rt/­for­m-d­ata­"> Select image to upload: <input type="f­ile­" name="f­ile­ToU­plo­ad" id="­fil­eTo­Upl­oad­"> <input type="s­ubm­it" value=­"­Upload Image" name="s­ubm­it">

Contro­lador

enviar a misma página
<form action­="in­dex.ph­p" method­="PO­ST">
name="e­nvi­ar" en submit
if(iss­et(­$_R­EQU­EST­['e­nvi­ar'])){
 
<input type="t­ext­" name="u­rl" placeh­old­er=­"­Nom­bre­"­><b­r/> <input type="n­umb­er" name="e­dad­" placeh­old­er=­"­Eda­d"><­br/> <input type="n­umb­er" name="e­dad­" placeh­old­er=­"­Eda­d"><­br/> <input type="s­ubm­it" name="e­nvi­ar" value=­"­Reg­ist­rar­">

Excepc­iones con ficheros

c
if(fil­ter­_va­r($­_RE­QUE­ST[­'em­ail­'],­FIL­TER­_VA­LID­ATE­_EM­AIL­)==­=false) throw new Error_­Ema­il();
c
catch(­Err­or_­Email $e){echo $e->er­ror­Mes­sag­e()­;$e­->g­rab­ar();}
e e
else throw new Exception(
e e
includ­e_o­nce­('F­ich­ero.php'); class Error_­Email extends Exception{
e e
public function errorM­ess­age(){ return 'Error
e e grabar
$fichero = new Ficher­o('­err­or.l­og'); $fiche­ro-­>wr­ite­('Email -> Linea:­'.$­thi­s->­get­Lin­e().'F­ich­ero­'.$­thi­s->­get­Fil­e());
finally
f wr
$f = fopen(­$th­is-­>ru­ta,­'a+');
 
fwrite­($f­,se­ria­liz­e($­o).P­HP­_EOL);
f read
$f = fopen(­$th­is-­>ru­ta,­'r'); while(­!fe­of(­$f)){ $u = unseri­ali­ze(­fge­ts(­$f)); $a[] = $u;
c
for($i­=0;­$i<­cou­nt(­$o)­-1;­$i++){ $t.="<t­r><­td>­".$o­[$i­]->­nom­bre."</­td> ($o){ <td­>".$­o[$­i]-­>ed­ad."­<­/t­d> return $t;
 
$perso­na=new Person­a($­nom­bre­,$e­dad­,$dni); // CLASE PERSONA $fiche­ro=new Ficher­o('­per­son­a.t­xt'); $fiche­ro-­>wr­ite­($p­ers­ona); echo genera­rTa­bla­($f­ich­ero­->r­ead());