Skip to content

Instantly share code, notes, and snippets.

@valdiney
Created March 24, 2013 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valdiney/5230222 to your computer and use it in GitHub Desktop.
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.
<?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>
<?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