Problem when saving image on server [closed]

0
  

PHP upload

<?php
    if($_SERVER["REQUEST_METHOD"]=="POST"){
        include('conexion.php');
            //Agregar reporte de marcadores
            $datos[0] = trim($_POST['usuario']);
            $datos[1] = trim($_POST['fecha']);
            $datos[2] = trim($_POST['hora']);
            $datos[3] = trim($_POST['tipo']);
            $datos[4] = trim($_POST['localizacion']);
            $datos[5] = trim($_POST['descripcion']);
            $datos[6] = trim($_POST['imagen']);

            $decodeImage = base64_decode("$datos[6]");
            file_put_contents("pruebas/".datos[0].".JPG", $decodeImage);

                $addReporte = mysqli_query($con,"INSERT INTO reporte (usuario,fecha,hora,tipo,localizacion,obsUsuario) VALUES ('$datos[0]','$datos[1]','$datos[2]','$datos[3]','$datos[4]','$datos[5]')").mysqli_error($con);
                  if ($addReporte != 1) {
                        //problema al registrar reportes
                        echo "problem-report".mysqli_error($con);
                    }else{
                        echo "report_saved";
                    }
        }else{
        echo "Bloqueado por el Administrador";
        }   
     ?>
  

Android

    ImageView Fotografia;
    String base64;
    Fotografia = new ImageView(this);
    Bitmap Imagen;

....
  

Send Information

HttpClient httpClient;
    HttpPost httpPost;
    httpClient = new DefaultHttpClient();
    httpPost = new HttpPost(getString(R.string.registrar_reporte));//url del servidor

    try {
        List<NameValuePair> nameValuePairs;
        //empezamos añadir nuestros datos
        nameValuePairs = new ArrayList<NameValuePair>(4);
        nameValuePairs.add(new BasicNameValuePair("usuario",Preferencias.getString("ID",""))); //ID de usuario logeado
        nameValuePairs.add(new BasicNameValuePair("fecha",Fecha.format(new Date()))); //Fecha de envio
        nameValuePairs.add(new BasicNameValuePair("hora",Hora.format(new Date()))); //Hora de envio
        nameValuePairs.add(new BasicNameValuePair("tipo",String.valueOf(Tipo))); //Bache,Iluminaria,....
        nameValuePairs.add(new BasicNameValuePair("localizacion",Ubicacion)); //Coordenadas GPS
        nameValuePairs.add(new BasicNameValuePair("descripcion",repDescripcion.getText().toString())); //Descripcion del usuario
        nameValuePairs.add(new BasicNameValuePair("imagen",base64)); //Imagen de reporte


        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpClient.execute(httpPost, responseHandler);

        return true;

    } catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }
    return  false;
  

Send button

Imagen =((BitmapDrawable) Fotografia.getDrawable()).getBitmap();
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    Imagen.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
                    base64 = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
                    new Insertar(Reporte_App.this).execute();
  

Problem!

It is not stored on the server, everything works perfect if I remove it

$datos[6] = trim($_POST['imagen']);
$decodeImage = base64_decode("$datos[6]");
file_put_contents("pruebas/".datos[0].".JPG", $decodeImage);

Everything that carries text to the bdd is stored, my problem is when I try to save the image.

    
asked by DoubleM 31.12.2017 в 00:29
source

1 answer

1

Taking into account that the type of input you should use is:

<input type="file" name="picture" id="picture"></input>

It is not for $ POST that you get the data from the file. This line of code is wrong:

$filename = $_POST['filename'];

Instead of using $ POST you use like in your next line of code $ _FILES to get the data of the file that you sent from the form. Replace the previous line with this one:

$filename=$_FILES['picture']['name'];

Remember also that when you are going to upload a file the type of encryption of the form element must be multipart in the form where you send the file. In this way:

<form action="file.php" method="post" enctype="multipart/form-data">
</form>

Observation: If you want the type of file you are uploading (it is very useful to validate that you do not place .txt when you only want it, for example) you use:

$tipo_archivo = $_FILES['picture']['type'];

For the file size it is:

$tamano_archivo = $_FILES['picture']['size'];

That's it. Greetings!

    
answered by 31.12.2017 в 01:50