jarab
jarab

Reputation: 139

I´m trying to upload one file to the localhost, but not working

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>';
}?>

Upvotes: 1

Views: 242

Answers (1)

UnholyRanger
UnholyRanger

Reputation: 1971

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.

Upvotes: 1

Related Questions