Created
March 24, 2013 02:35
-
-
Save valdiney/5230222 to your computer and use it in GitHub Desktop.
upload de imagens para o servidor utilizando a linguagem php. Este é um exemplo muito útil em muitos dos casos e necessidades do dia a dia de um estudante de programação.
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 | |
/*Upload de imagens para o servidor | |
Grupo de estudos: cssshark.wordpress.com | |
Autor:Valdiney França | |
Data:02/01/2013 | |
*/ | |
?> | |
<!doctype html> | |
<html> | |
<head> | |
<script language="javascript" src="js/exemplos.js"></script> | |
<link rel="stylesheet" href="css/style_geral.css"/> | |
<title>Exemplos</title> | |
</head> | |
<body> | |
<?php | |
if(isset($_POST['upload'])){ | |
$pasta ='imgs'; | |
$permitido = array('image/jpg','image/jpeg','image/pjpeg'); | |
$img = $_FILES['img']; | |
$tmp = $img['tmp_name']; | |
$name = $img['name']; | |
$type = $img['type']; | |
require('funcao.php'); | |
//if(!empty($name) && in_array($type,$permitido)){ | |
if($_GET['acao']=="mandar" && in_array($type,$permitido)){ | |
$nome = 'downmaster-'.md5(uniqid(rand(),true)).'.jpg'; | |
carregar($tmp,$nome,219,$pasta); | |
header("Location:exemplos.php"); | |
}else{ | |
echo"Tipo de arquivo invalido. Aceitamos apenas jpg, verifique se o campo está em branco!"; | |
} | |
}//end | |
?> | |
<form action="?acao=mandar" method="post" enctype="multipart/form-data" name="upload"> | |
<input type="file" name="img"/> | |
<button type="submit" name="upload">Enviar</button> | |
</form> | |
</body> | |
</html> |
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 | |
/* | |
Este é o código que fica na página "funcao.php" | |
*/ | |
?> | |
<?php | |
function carregar($tmp,$nome,$largura,$pasta){ | |
$img = imagecreatefromjpeg($tmp); | |
$x = imagesx($img); | |
$y = imagesx($img); | |
$altura = ($largura*$y)/$x; | |
$nova = imagecreatetruecolor($largura,$altura); | |
imagecopyresampled($nova,$img,0,0,0,0, $largura,$altura,$x,$y); | |
imagejpeg($nova, "$pasta/$nome"); | |
imagedestroy($nova); | |
imagedestroy($img); | |
return($nome); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment