Reputation: 5
With this form:
<form id="form1" name="form1" method="post" action="insertartrabajo.php" enctype="multipart/form-data">
<legend>formato vertical</legend>
<br />
<label for="cserv">Servicio:</label>
<select name="cserv">
<option value="NULL">Seleccione un servicio</option>
<?php
$sql="SELECT * FROM servicios GROUP BY servicios.nombre";
$resultado=mysql_query($sql);
while($fila=mysql_fetch_array($resultado)){
?>
<option value="<?php echo $fila["nombre"]; ?>"><?php echo $fila["nombre"]; ?></option>
<?php } ?>
</select>
<br>
<label for="cdirv">Direccion:</label>
<input name="cdirv" type="text">
<br>
<label for="cfoto">Foto:</label>
<input type="file" name="cfoto">
<br>
<label for="cobserv">Observaciones:</label>
<textarea name="cobserv" cols="10" rows="3"></textarea>
<br>
<input type="submit" name="Insertarv" value="Insertar"/>
</form>
and this PHP code:
<?php session_start();
include("includes/conexiones.php");
$sql = "SELECT * FROM trabajos ORDER BY id DESC LIMIT 1" ;
$resultado = mysql_query($sql);
$fila = mysql_fetch_array($resultado);
$lastid = $fila["id"];
$apendice = $lastid + 1;
if ($_POST["cserv"] != "") {
$servicio=$_POST["cserv"];}
if ($_POST["cdirv"] != "") {
$direccion=$_POST["cdirv"];}
if ($_POST["cobserv"] != "") {
$observaciones=$_POST["cobserv"];}
if ($_POST["cfoto"]!=""){
$foto=$_FILES["cfoto"]["name"];
ini_set('post_max_size','100M');
ini_set('upload_max_filesize','100M');
ini_set('max_execution_time','1000');
ini_set('max_input_time','1000');
$fototmp=$_FILES["cfoto"]["tmp_name"];
list($ancho, $alto) = getimagesize($fototmp);
$nuevoancho = 600;
$nuevoalto = 600 * $alto / $ancho;
$nuevaimg = imagecreatetruecolor($nuevoancho, $nuevoalto);
$idnuevaimg = imagecreatefromjpeg($fototmp);
imagecopyresized($nuevaimg, $idnuevaimg, 0, 0, 0, 0, $nuevoancho, $nuevoalto, $ancho, $alto);
imagejpeg ($nuevaimg,"imagenes/grandes/".$prefijo.$foto);
$fototmp = $_FILES["cfoto"]["tmp_name"];
list($ancho, $alto) = getimagesize($fototmp);
$nuevoancho = 144;
$nuevoalto = 144 * $alto / $ancho;
$nuevaimg = imagecreatetruecolor($nuevoancho, $nuevoalto);
$idnuevaimg = imagecreatefromjpeg($fototmp);
imagecopyresized($nuevaimg,$idnuevaimg,0,0,0,0,$nuevoancho,$nuevoalto,$ancho,$alto);
imagejpeg ($nuevaimg,"imagenes/peques/".$prefijo.$foto);}
$nombrefoto=$apendice.$foto;
$sql="INSERT INTO trabajos (servicio, direccion, observaciones, foto) VALUES ('$servicio', '$direccion', '$observaciones', '$nombrefoto')";
mysql_query($sql);
$idtrabajo=mysql_insert_id();
header("location:insertartrabajo2.php?vid=$idtrabajo");
?>
My problem is the $foto
variable. All that code runs, my computer doesn't give me any error, but $foto
is empty and in the database "name" column only appears the $prefijo
variable.
What is the problem?
Upvotes: 0
Views: 98
Reputation: 385144
if ($_POST["cfoto"]!=""){ $foto=$_FILES["cfoto"]["name"];
You are assuming that, when you submit a file with the name "cfoto"
, it'll show up in both $_FILES
and $_POST
, but it does not.
To be fair, the documentation doesn't make this clear, but what would its value be in $_POST
? $_FILES
exists precisely because these form fields need special handling.
Check isset($_FILES["cfoto"])
, instead.
Upvotes: 1