0

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?

sjagr
  • 15,983
  • 5
  • 40
  • 67

1 Answers1

1
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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    thanks Lightness Races in Orbit, thats was the error. i can´t write $_POST... when i change it, all runs ok. – Ibantxillo Jan 15 '14 at 15:47
  • @Ibantxillo you should accept the answer if it helped you understand and solve your problem (click the checkbox below the upvote/downvote buttons so it turns green) – sjagr Jan 15 '14 at 15:58
  • If `$_POST["cfoto"]` doesn't exist, you should be getting a clear notice about it. If you don't, that means that you haven't configured PHP to display error messages. – Álvaro González Jan 16 '14 at 09:19
  • 1
    @ÁlvaroG.Vicario: True enough. Also I have just confirmed that _nothing_ enters `$_POST` for upload fields, not even with an empty value. – Lightness Races in Orbit Jan 16 '14 at 10:59