codice HTML:
<form class="form-horizontal" role="form" id="form" action="process/slide_proc.php?mode=insert" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="titolo" class="col-sm-2 control-label">Categoria</label>
<div class="col-sm-5">
<select name="categoria" id="categoria" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="form-group">
<label for="titolo" class="col-sm-2 control-label">Titolo</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="titolo" name="titolo" placeholder="Inserisci qui il titolo" required >
</div>
</div>
<div class="form-group">
<label for="descrizione" class="col-sm-2 control-label">Descrizione</label>
<div class="col-sm-5">
<textarea type="text" class="form-control" id="descrizione" name="descrizione" placeholder="Inserisci qui la descrizione" ></textarea>
</div>
</div>
<div class="form-group">
<label for="immagine" class="col-sm-2 control-label">Immagine</label>
<div class="col-sm-5">
<input type="file" id="immagine" accept="image/*" name="immagine" required >
<span class="nome-file"></span>
</div>
</div>
<div class="form-group">
<label for="foto" class="col-sm-2 control-label">Immagini Correlate</label>
<div class="col-sm-5">
<input type="file" id="foto" accept="image/*" name="foto[]" multiple>
<span class="nome-file"></span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" value="submit" >Procedi</button>
</div>
</div>
</form>
Codice PHP:
if($_REQUEST['mode'] == 'insert'){
$titolo = $_POST['titolo'];
$descrizione = $_POST['descrizione'];
$categoria = $_POST['categoria'];
$link = $_POST['link'];
$file_name = $_FILES['immagine']['name'];
$file_tmp = $_FILES['immagine']['tmp_name'];
$file_size = $_FILES['immagine']['size'];
$file_type = $_FILES['immagine']['type'];
if($file_name != ""){
$tipo_file = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file_tmp);
if($tipo_file == "image/jpeg" || $tipo_file == "image/png" || $tipo_file == "image/gif"){
if($file_size > $providerMaxFileSize){
header("location:../index.php?messaggio=imgSizeError");
die();
}
$file_name = time()."_".$file_name;
move_uploaded_file($file_tmp, "../upload_image/".$file_name);
smart_resize_image("../upload_image/".$file_name,
120,//larghezza
120,//altezza
true,//proporziona
"../upload_image/thumb-" . $file_name,//output
false//cancella originale
);
}else{
header("location:index.php?page=1&messaggio=imgTypeError");
die();
}
}
$sql = "INSERT INTO temp (categoria, titolo, link, descrizione, immagine ) VALUES ('$categoria', '$titolo', '$link', '$descrizione', '$file_name');";
collega_db();
$result = mysql_query($sql,$conn);
echo mysql_error();
$idInsert = mysql_insert_id();
if(!empty($_FILES['foto'])){
$errors= array();
foreach($_FILES['foto']['tmp_name'] as $key => $tmp_name ){
$foto_name = $_FILES['foto']['name'][$key];
$foto_size = $_FILES['foto']['size'][$key];
$foto_tmp = $_FILES['foto']['tmp_name'][$key];
$foto_type= $_FILES['foto']['type'][$key];
if($foto_size > $providerMaxFileSize){
$errors[]='File size must be less than 4 MB';
}
if(empty($errors)==false){
print_r($errors);
}else{
if(is_dir($user_data)==false){
mkdir("$user_data", 0777);
}
if(file_exists($user_data.$foto_name)==false){
move_uploaded_file($foto_tmp,"$user_data".$foto_name);
smart_resize_image($user_data.$foto_name,//input
120,//larghezza
120,//altezza
true,//proporziona
$user_data . "thumb-".$foto_name,//output
false//cancella originale
);
}else{
$name = substr($foto_name, 0, strrpos($foto_name, '.'))
$new_dir="$user_data";
$new_name = $name . time();
$ultimoPunto = strrpos($foto_name,".");
$estensione=substr($foto_name,$ultimoPunto + 1);
$foto_name = $new_name . "." . $estensione;
move_uploaded_file($foto_tmp, "$new_dir".$foto_name);
smart_resize_image($new_dir.$foto_name,//input
120,//larghezza
120,//altezza
true,//proporziona
$user_data . "thumb-".$foto_name,//output
false//cancella originale
);
}
}
$sql_foto = "INSERT INTO immagini_corr_temp(idImage,immagine_corr) VALUES ('$idInsert','$foto_name');";
$result_foto = mysql_query($sql_foto,$conn);
echo mysql_error();
}
if(empty($errors)){
header("location:../page/resize_image.php?id=$idInsert");
die();
}
}else{
header("location:../page/resize_image.php?id=$idInsert");
die();
}