1

I´m trying to upload one file to the localhost server, but it doesnt work. I'm trying to give permissions to the folder where the file is to be located using chmod, but I do not get it. The form is printed dynamically in one file called index.php and, when the submit is clicked, the datas are recieved to the same php file called recExpediente.php.

PHP

    <? 
ini_set('error_reporting', E_ALL);

    require('conectaDemo.php');   

    if (is_uploaded_file($_FILES['Foto']['tmp_name'])) {
            $cSQL="SELECT NIF FROM CLIENTES WHERE ID_CLIENTE=?";
            $stmt=$oConni->prepare($cSQL) or die($oConni->error);
            $stmt->bind_param('i',$_POST['selDesc2']);
            $stmt->execute();                               
            $stmt->bind_result($DNI);
            if ($stmt->fetch()) {
                chmod('/var/www/pracRemesas/'.$DNI.'/', 0777);
                echo '/var/www/pracRemesas/'.$DNI.'/';
                $uploaddir='/var/www/pracRemesas/'.$DNI.'/';
             }
             $stmt->close();
            $uploadfile = $uploaddir . basename($_FILES['Foto']['name']); 
            move_uploaded_file($_FILES['Foto']['tmp_name'], $uploadfile);
            $cFoto=basename($_FILES['Foto']['name']);
            $xSQL = "INSERT INTO EXPEDIENTES (ID_CLIENTE, DOCUMENTO, 
            OBSERVACIONES) VALUES(?,?,?)";
            $stmt = $oConni->prepare($xSQL);
            $stmt->bind_param('iss', $_POST['selDesc2'], $cFoto, $_POST['observ']);
            $stmt->execute();
            $stmt->close();

}


   if (isset($_POST['newid'])) {  
            $cSQL="SELECT ID_CLIENTE, NOMBRE FROM CLIENTES";
            $stmt=$oConni->prepare($cSQL) or die($oConni->error);
            $stmt->execute();                               
            $stmt->bind_result($ID_CLIENTE, $NOMBRE);
            echo'<form name="nuevoExp" id="frmLogin" action="recExpediente.php" method="POST" enctype="multipart/form-data">';
            echo'<select id="selDesc2" name="selDesc2">';
            echo'<option value=-1>Seleccione cliente</option>';
             while ($stmt->fetch()) {
                echo'<option value="'.$ID_CLIENTE.'">'.$NOMBRE.'</option>';
            }   
            $stmt->close();
            echo'</select><br>'; 
            echo'<input type="file" name="Foto" id="Foto"><br>';
            echo'Observaciones<input type="text" name="observ" id="observ"><br>';
            echo'<input  type="submit" name="butEnv" id="butEnv" value="Enviar"/>';
            echo'</form>';
}?>
jarab
  • 139
  • 8
  • what debugging have you done? Does the values get submitted to the database? – UnholyRanger Feb 13 '13 at 18:04
  • is the file directory to be uploaded to being echo'd? If not, then `$uploaddir` is not being set. – UnholyRanger Feb 13 '13 at 18:10
  • maybe. what does `echo $_FILES['Foto']['error'];` give? – UnholyRanger Feb 13 '13 at 18:15
  • this is the result: /var/www/pracRemesas/99250251M/0 – jarab Feb 13 '13 at 18:17
  • Try uploading in your local temp folder. Also do you have some basedir restrictions on ? Take a look at here: http://stackoverflow.com/questions/10262104/realpath-open-basedir-restriction-in-effect/13614147#13614147 – Lucian Depold Feb 13 '13 at 18:19
  • no, i haven´t restrictions – jarab Feb 13 '13 at 18:20
  • Since you echo out the directory `/var/www/pracRemesas/99250251M/` is the first part and then the error is `0` which means it was successful. Are you sure the file is not there? – UnholyRanger Feb 13 '13 at 18:22
  • yes, i just check, the directory is empty – jarab Feb 13 '13 at 18:23
  • does the uploaded directory exist that you're uploading into? so does `99250251M` exist in `/var/www/pracRemesas`? Add `ini_set('display_errors',1); error_reporting(E_ALL);` to top of your code. – UnholyRanger Feb 13 '13 at 18:24
  • Notice: Undefined variable: uploaddir in /var/www/pracRemesas/recExpediente.php on line 18 Warning: move_uploaded_file(_DNI.pdf): failed to open stream: Permission denied in /var/www/pracRemesas/recExpediente.php on line 20 Warning: move_uploaded_file(): Unable to move '/tmp/phprEyOhH' to '_DNI.pdf' in /var/www/pracRemesas/recExpediente.php on line 20 0 – jarab Feb 13 '13 at 18:30

1 Answers1

1

Your problem is variable scope. You define $uploaddir in an if statement where it will live only for that statement. You need to define it before and you can modify it inside the if statement.

This is causing your code to actually be

$uploadfile = basename($_FILES['Foto']['name']);

I suggest more checks in your code and fail safes. You can include the upload code and your insert in the if ($stmt->fetch()) {. If you move everything in the if statement, you wont have to change anything for the $uploaddir issue.

UnholyRanger
  • 1,977
  • 14
  • 24