Created
April 19, 2012 15:49
-
-
Save asjustas/2421893 to your computer and use it in GitHub Desktop.
simple php file upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//patikriname ar vartotojas ákelia bylà | |
if ($_FILES['byla']) { | |
//tikriname ar ákelta byla yra iðtiesø paveikslëlis | |
if (exif_imagetype($_FILES['byla']['tmp_name'])) { | |
//jei paveikslëlis toliau tikriname ar tinkamas tipas | |
$pav_attr = getimagesize($_FILES['byla']['tmp_name']); | |
$width = $pav_attr[0]; | |
$height = $pav_attr[1]; | |
$type = $pav_attr['mime']; | |
$galimi_bylu_tipai = array('image/png', 'image/jpeg', 'image/gif'); | |
if (!in_array($type, $galimi_bylu_tipai)) { | |
$klaidos[] = 'Netinkamas Tipas'; | |
} | |
//galime tikrint ir paveikslëlio dydá | |
$max_plotis = 1024; | |
$max_aukstis = 768; | |
if (($max_plotis < $width) || ($max_aukstis < $height)) { | |
$klaidos[] = 'Paveikslëlio iðmatavimai netinkami'; | |
} | |
//tikrinam ar ákelta byla nëra per didelë | |
$max_dydis = 300*1024; //300KB | |
if (intval($max_dydis) < intval($_FILES['byla']['size'])) { | |
$klaidos[] = 'Netinkamas dydis'; | |
} | |
//galiausiai patikrinam ar leidziama ikelti paveiksleli | |
if (!is_array($klaidos)) { | |
//prijungiu funkcij1 time() kad nebutø pasikartojanèiu bylø | |
$filename = 'upload/'.time().$_FILES['byla']['name']; //nustatom failui varda | |
$result = move_uploaded_file($_FILES['byla']['tmp_name'], $filename); | |
if (!$result) { | |
echo 'Klaida! Paveikslëlis nebuvo ákeltas'; | |
} | |
else { | |
//failas buvo sekmingai ikeltas tai galim sukurti irasa mysql ir faila priskirti useriu | |
//$user_id = cia priskirti vartotojo id, kad galetum atskirt kuriam vartotojui failas priklauso | |
mysql_query("INSERT INTO `files` (`id`, `user_id`, `file`) VALUES ('.$user_id.', '".$filename."') "); | |
echo 'Byla ákelta.'; | |
} | |
} | |
else { | |
foreach ($klaidos as $klaida) { | |
echo $klaida.'<br />'; | |
} | |
} | |
} | |
else { | |
//jei ákelta byla nëra paveikslëlis atspausdiname klaidà | |
echo 'Klaida! Ákelta byla nëra paveikslëlis'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment