Location: PHPKode > scripts > Easy upload resize thumb image > resizeupload/inserir.php
<?php
require_once('Connections/cnn_bdschool.php'); 

/***************************************** Changes by Ubaid *******************************************/
// you need to include these two classes to resize and upload image
// you can change its page eg. if you create a director with name "classes" and put these two file in that then change the path to
//  include('classes/upload_class.php');
//  include('classes/imageresizer.class.php');
include('classes/upload_class.php');
include('classes/imageresizer.class.php');
/***************************************** End of Changes by Ubaid *******************************************/

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "cadaluno")) {

  $insertSQL = "INSERT INTO tbalunos (idaluno, nome, telefone, celular, email, telefonecomercial, rg, cpf, estadocivil, sexo, datanasc, cidadenasc, estadonasc, endereco, casanumero, cep, bairro, cidade, estado, profissao, empresa, convenio, mensalidade, escola, status, idturma, dataincio, dataconclusao, conheceuescolapor, obs, criadoem) VALUES (
                       '".GetSQLValueString($_POST['nome'], "text")."',
                       '".GetSQLValueString($_POST['telefone'], "text")."',
                       '".GetSQLValueString($_POST['celular'], "text")."',
                       '".GetSQLValueString($_POST['email'], "text")."',
                       '".GetSQLValueString($_POST['telefonecomercial'], "text")."',
                       '".GetSQLValueString($_POST['rg'], "text")."',
                       '".GetSQLValueString($_POST['cpf'], "text")."',
                       '".GetSQLValueString($_POST['estadocivil'], "text")."',
                       '".GetSQLValueString($_POST['sexo'], "text")."',
                       '".GetSQLValueString(Convert_Data_Port_nasc($_POST['datanasc']), "date")."',
                       '".GetSQLValueString($_POST['cidadenasc'], "text")."',
                       '".GetSQLValueString($_POST['estadonasc'], "text")."',
                       '".GetSQLValueString($_POST['endereco'], "text")."',
                       '".GetSQLValueString($_POST['casanumero'], "text")."',
                       '".GetSQLValueString($_POST['cep'], "text")."',
                       '".GetSQLValueString($_POST['bairro'], "text")."',
                       '".GetSQLValueString($_POST['cidade'], "text")."',
                       '".GetSQLValueString($_POST['estado'], "text")."',
                       '".GetSQLValueString($_POST['profissao'], "text")."',
                       '".GetSQLValueString($_POST['empresa'], "text")."',
                       '".GetSQLValueString(isset($_POST['convenio']) ? "true" : "", "defined","'Y'","'N'")."',
                       '".GetSQLValueString($_POST['mensalidade'], "double")."',
                       '".GetSQLValueString($_POST['escola'], "text")."',
                       '".GetSQLValueString($_POST['status'], "text")."',
                       '".GetSQLValueString($_POST['idturma'], "int")."',
                       '".GetSQLValueString(Convert_Data_Port_nasc($_POST['datainicio']), "date")."',
                       '".GetSQLValueString(Convert_Data_Port_nasc($_POST['dataconclusao']), "date")."',
                       '".GetSQLValueString($_POST['conheceuescolapor'], "text")."',
                       '".GetSQLValueString($_POST['obs'], "text")."',
                       '".GetSQLValueString($_POST['criadoem'], "date")."'");

  mysql_select_db($database_cnn_bdschool, $cnn_bdschool);


/* ******************************************* changes made by Ubaid *****************************************/
	//we first check seccessful insertion of record in db. if statment will check if record is seccessfuly inserted
	//in db, if yes then we will upload the image  	
	if(mysql_query($insertSQL, $cnn_bdschool))
	{
		
		$ID = mysql_insert_id(); //get last inserted id which will be use to rename the picture. so picture will be save with id rather then name
		
		$UF_obj = new Upload();		//Create the object of class that will upload the image
		
		//given the file control to class object. that will use to upload the image that come from "comic" field of submited form
		$UF_obj -> File			=	$_FILES['comic'];
		
		// this is path where our images will be saved. its mean all images upload will be saved in picture folder of server
		//if you want to save pictures some where else eg. you want to save images in profile folder which is inside users folder then
		// the path will be change like this 
		//$UF_obj -> SavePath		=	'users/profile';
		
		$UF_obj -> SavePath		=	'pictures'; 	// PLACE where you want to save images.
		
		// here we create thumbnail of picture that is going to upload right now.
		//you can give any path you wish, where you want to create thumb of picture.
		//if you don't specify or comment bellow line, no thumb will be created of pictur
		$UF_obj -> ThumbPath	=	'pictures/thumb'; //if not specify will not create thumbnil
		
		//here you can rename or change the uploaded file name. eg.if the upload file name is computer.jpg in your pc
		//but you want to rename that to mycomputer.jpg then you will provide new name here
		//eg. $UF_obj -> NewName		=	mycomputer.jpg;
		//if you comment this line. image will save with orignal name. I recomend to not comment this line and save the image with
		//ID of record which is beaning inserted. eg if I come to your site and register and the id of my record in db is 5210 then
		// provide there 5210 so if i upload my jpg image then image will be saved with my id like 5210.jpg, which will be very helpfull to you to
		//find related image to record you are going to display on website. 
		//in file file i am demonstrating with id
		$UF_obj -> NewName		=	$ID //here picture will be rename with id eg. computer.jpg will be 5210.jpg
		
		
		//width and height of large image which will save in "pictures/" folder
		$UF_obj -> NewWidth		= 300;
		$UF_obj -> NewHeight	= 300;	
		////width and height of thumb image which will save in "pictures/thumb/" folder
		$UF_obj -> TWidth		= 50;
		$UF_obj -> THeight		= 50;

		/* 		
		*		if you want to name image something other then upload image name then use bellow formate
		* 		for example you upload two images then
		*
		* $UF_obj -> NewName	=	array('NewName1.jpg', 'NewName2.jpg');
		*/
		
		$UF_obj -> NameCase		=	'lower'; //default no change. upper for upper case
		$UF_obj -> OverWrite	=	true; //default = true. replace existing image
		
		//UploadFile() function upload and resize image.
		//function return error message if any.
		//error variable is in array form. so you can get more then one error/warning messages.
		// or you can also access error message by class object varialbe like $UF_obj -> Error
		$Error = $UF_obj -> UploadFile();
		
		if(count($Error) > 0 and is_array($Error))
		{
		
			//if you get there some how. its mean unsuccessful updation of image file
			//so i am going to delete inserted record from db too
			// replace UserId with ID field in your table tbalunos
			mysql_query("delete from tbalunos where idaluno = '".$ID."'");
			
			foreach($Error as $key=>$val)
			{
				//echo $val . '<br>';
			}
		}
		else
		{
			//as we change the name of picture from it orignal name to ID in table we will also update the name in table here
			mysql_query("update tbalunos set ProfilePicture = '". $UF_obj->RNewName ."' where  idaluno = '".$ID."'");
		}
	}
}

mysql_select_db($database_cnn_bdschool, $cnn_bdschool);
$query_rs_alunot = "SELECT idturma, turma FROM tbturmas ORDER BY turma ASC";
$rs_alunot = mysql_query($query_rs_alunot, $cnn_bdschool) or die(mysql_error());
$row_rs_alunot = mysql_fetch_assoc($rs_alunot);
$totalRows_rs_alunot = mysql_num_rows($rs_alunot);
function Convert_Data_Port_nasc($entradata){
    $conv1 = explode("/",$entradata);
    $conv2 = array_reverse($conv1);
    $saidata = implode("-",$conv2);
    return $saidata;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>INSERIR REGISTRO</title>
<script>

function formatar(src, mask)
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida)
  {
                src.value += texto.substring(0,1);
  }
}

</script>

<link href="css/css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
	background-color: #666;
}
body,td,th {
	color: #CCC;
}
-->
</style></head>

<body>
<h1 class="titulo">Registrar Aluno<br />
<span class="desccaixadetexto">Digite os dados do aluno nos campos abaixo| <a href="listaaluno.php">CANCELAR INCLUSÃO</a></span></h1>
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="cadaluno" id="cadaluno">
  <table width="350" border="0" cellspacing="2" cellpadding="0">
    <tr>
      <td colspan="4" valign="middle"><span class="desccaixadetexto">Nome do aluno
        
      </span></td>
    </tr>
    <tr>
      <td colspan="4" valign="top"><input name="nome" type="text" class="input" id="nome" size="62" /></td>
    </tr>
    
    <tr>
      <td colspan="2"><span class="desccaixadetexto">Telefone1</span></td>
      <td colspan="2"><span class="desccaixadetexto">e-mail</span></td>
    </tr>
    <tr>
      <td colspan="2"><input name="telefone" type="text" class="input" id="telefone" /></td>
      <td colspan="2"><input name="email" type="text" class="inputEMAil" id="email" size="35" /></td>
    </tr>
    <tr>
      <td colspan="2"><span class="desccaixadetexto">Celular</span></td>
      <td colspan="2"><span class="desccaixadetexto">Telefone Comercial</span></td>
    </tr>
    <tr class="unbord">
      <td colspan="2"><input name="celular" type="text" class="input" id="celular" /></td>
      <td colspan="2"><input name="telefonecomercial" type="text" class="input" id="telefonecomercial" /></td>
    </tr>
    
    <tr>
      <td colspan="2"><span class="desccaixadetexto">RG</span></td>
      <td colspan="2"><span class="desccaixadetexto">CPF</span></td>
    </tr>
    <tr>
      <td colspan="2"><input name="rg" type="text" class="input" id="rg" /></td>
      <td colspan="2"><input name="cpf" type="text" class="input" id="cpf" size="30" /></td>
    </tr>
    <tr>
      <td colspan="2"><span class="desccaixadetexto">Estado Civil</span></td>
      <td colspan="2"><span class="desccaixadetexto">Sexo</span></td>
    </tr>
    <tr>
      <td colspan="2"><select name="estadocivil" class="select" id="estadocivil">
          <option value="Solteiro">Solteiro</option>
          <option value="Casado">Casado</option>
      </select></td>
      <td colspan="2"><select name="sexo" class="select" id="sexo">
        <option value="Masculino">Masculino</option>
        <option value="Feminino">Feminino</option>
      </select></td>
    </tr>
    
    <tr>
      <td colspan="2"><span class="desccaixadetexto">Data de Nascimento</span></td>
      <td width="199"><span class="desccaixadetexto">Natural de</span></td>
      <td width="127"><span class="desccaixadetexto">Estado</span></td>
    </tr>
    <tr>
      <td colspan="2"><input  name="datanasc" type="text" class="input" id="datanasc" size="20" /></td>
      <td><input name="cidadenasc" type="text" class="input" id="cidadenasc" /></td>
      <td><select name="estadonasc" class="select" id="estadonasc">
        <option value="SP">SP</option>
        <option value="MG">MG</option>
        <option value="AC">AC</option>
        <option value="AL">AL</option>
        <option value="AP">AP</option>
        <option value="AM">AM</option>
        <option value="BA">BA</option>
        <option value="CE">CE</option>
        <option value="DF">DF</option>
        <option value="GO">ES</option>
        <option value="MA">MA</option>
        <option value="MT">MT</option>
        <option value="MS">MS</option>
        <option value="PA">PA</option>
        <option value="PB">PB</option>
        <option value="PE">PR</option>
        <option value="PI">PI</option>
        <option value="RJ">RJ</option>
        <option value="RN">RN</option>
        <option value="RS">RS</option>
        <option value="RO">RO</option>
        <option value="RR">RR</option>
        <option value="SC">SC</option>
        <option value="SE">SE</option>
        <option value="TO">TO</option>
        <option value="EXT">EXT</option>
      </select></td>
    </tr>
    
    <tr >
      <td colspan="3"><span class="desccaixadetexto">Foto</span></td>
      <td>&nbsp;</td>
    </tr>
    
    <tr >
      <td colspan="3"><label>
	  
	  <!-- *********************************** Comment by Ubaid *********************************** -->
	  <!-- I have change the name of field from "comic[]" to "comic". if you are going to upload only one image then you 
	  		don't have need to create array of this form control -->
        <input type="file" name="comic" id="comic" />
      </label></td>
      <td>&nbsp;</td>
    </tr>
    
    <tr >
      <td colspan="3"><span class="desccaixadetexto">ENDEREÇO</span></td>
      <td><span class="desccaixadetexto">numero</span></td>
    </tr>
    <tr>
      <td colspan="3"><input name="endereco" type="text" class="input" id="endereco" size="46" /></td>
      <td><input name="casanumero" type="text" class="input" id="casanumero" size="6" /></td>
    </tr>
    <tr>
      <td><span class="desccaixadetexto">CEP</span></td>
      <td colspan="2"><span class="desccaixadetexto">Bairro</span></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input name="cep" type="text" class="input" id="cep" size="20" /></td>
      <td colspan="2"><input name="bairro" type="text" class="input" id="bairro" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><span class="desccaixadetexto">Cidade</span></td>
      <td colspan="2"><span class="desccaixadetexto">Estado</span></td>
      <td><span class="desccaixadetexto">Mensalidade</span></td>
    </tr>
    <tr>
      <td width="207"><select name="cidade" class="select" id="cidade">
        <option value="São José dos Campos">São José dos Campos</option>
        <option value="Jacarei">Jacarei</option>
        <option value="Caçapava">Caçapava</option>
        <option value="Taubaté">Taubaté</option>
        <option value="Paraibuna">Paraibuna</option>
      </select></td>
      <td colspan="2"><select name="estado" class="select" id="estado">
        <option value="SP">SP</option>
        <option value="MG">MG</option>
        <option value="AC">AC</option>
        <option value="AL">AL</option>
        <option value="AP">AP</option>
        <option value="AM">AM</option>
        <option value="BA">BA</option>
        <option value="CE">CE</option>
        <option value="DF">DF</option>
        <option value="GO">ES</option>
        <option value="MA">MA</option>
        <option value="MT">MT</option>
        <option value="MS">MS</option>
        <option value="PA">PA</option>
        <option value="PB">PB</option>
        <option value="PE">PR</option>
        <option value="PI">PI</option>
        <option value="RJ">RJ</option>
        <option value="RN">RN</option>
        <option value="RS">RS</option>
        <option value="RO">RO</option>
        <option value="RR">RR</option>
        <option value="SC">SC</option>
        <option value="SE">SE</option>
        <option value="TO">TO</option>
        <option value="EXT">EXT</option>
      </select></td>
      <td><input name="mensalidade" type="text" class="input" id="mensalidade" size="10" /></td>
    </tr>
    <tr>
      <td><span class="desccaixadetexto">Empresa</span></td>
      <td colspan="2"><span class="desccaixadetexto">Profissão</span></td>
      <td><span class="desccaixadetexto">Convênio</span></td>
    </tr>
    <tr>
      <td><input name="empresa" type="text" class="input" id="empresa" /></td>
      <td colspan="2">
      <input name="profissao" type="text" class="input" id="profissao" /></td>
      <td>&nbsp;&nbsp;&nbsp;        <input name="convenio" type="checkbox" class="input" id="convenio" value="sim" /></td>
    </tr>
    <tr>
      <td><span class="desccaixadetexto">Escola</span></td>
      <td colspan="2"><span class="desccaixadetexto">&nbsp;&nbsp;&nbsp;&nbsp;Status</span></td>
      <td><span class="desccaixadetexto">TURMA</span></td>
    </tr>
    <tr>
      <td><input name="escola" type="text" class="input" id="escola" /></td>
      <td colspan="2"><span class="desccaixadetexto">
        &nbsp;&nbsp;&nbsp;&nbsp;
        <select name="status" class="select" id="status">
          <option value="Ativo">Ativo</option>
          <option value="Inativo">Inativo</option>
          <option value="Inadimplente">Inadimplente</option>
          <option value="Concluido">Concluido</option>
        </select>
      </span></td>
      <td><select name="idturma" class="select" id="idturma">
        <?php
do {  
?>
        <option value="<?php echo $row_rs_alunot['idturma']?>"><?php echo $row_rs_alunot['turma']?></option>
        <?php
} while ($row_rs_alunot = mysql_fetch_assoc($rs_alunot));
  $rows = mysql_num_rows($rs_alunot);
  if($rows > 0) {
      mysql_data_seek($rs_alunot, 0);
	  $row_rs_alunot = mysql_fetch_assoc($rs_alunot);
  }
?>
      </select></td>
    </tr>
    <tr>
      <td><span class="desccaixadetexto">Conheceu a escola através de:</span></td>
      <td colspan="2"><span class="desccaixadetexto">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DT Início </span></td>
      <td><span class="desccaixadetexto">DT Conlcusão</span></td>
    </tr>
    <tr>
      <td><input name="conheceuescolapor" type="text" class="input" id="conheceuescolapor" size="25" /></td>
      <td colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;<input name="datainicio" type="text" class="input" id="datainicio" size="15" /></td>
      <td><input name="dataconclusao" type="text" class="input" id="dataconclusao" size="15" /></td>
    </tr>
    <tr>
      <td><span class="desccaixadetexto">Observações:</span></td>
      <td colspan="2">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="4"><textarea name="obs" cols="60" rows="5" class="textarea" id="obs"></textarea></td>
    </tr>
    <tr>
      <td colspan="4" align="center">
        <p><br />

          <input name="insert" type="submit" class="inputBTN" id="insert" value="Gravar Registro " />
      </p></td>
    </tr>
    
  </table>
  <input type="hidden" name="MM_insert" value="cadaluno" />
<input name="criadoem" type="hidden" id="criadoem" value="<?php echo((isset($_POST["criadoem"]))?$_POST["criadoem"]:"") ?>" />
</form>



</body>
</html>
<?php
mysql_free_result($rs_alunot);
?>
Return current item: Easy upload resize thumb image