How to upload an image from a cell phone to the PHP server?

1

I have a problem uploading images to the server from a mobile device. When I run my web application from the computer everything works correctly, the images go up without problems. But when I do it from a cell phone, I do not load the images that are selected.

This code is not directly the one I want to solve, but it is practically the same with fewer validations, but still, the image does not go up when I open the page from the cell phone (With S.O Android).

$(document).ready(function(){
	$('form[name="frmname[]"]').on("submit", function(e){
		e.preventDefault();
		var nombre = $("#filename").val();
		if (nombre =! "") {
		$.ajax({
        type: 'POST',
        url: 'enlace.php',
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success: function(data) {
        	alert(data);
    },
    fail: function(error){
    	alert(error);
    }
      });
		}
	});
});
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="icon" href="http://i0.wp.com/www.hidromasa.com/wp-content/uploads/2016/08/icono-gota-agua-Hidromasa.png?fit=512%2C512" type="image/ico" />

    <title></title>
  </head>
  <!-- Bootstrap -->
  <link href="assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
  <body>
    <div class="container">
      <div class="row">
        <form name="frmname[]"  enctype="multipart/form-data">
          <div class="form-group ">
            <label>Nombre:</label>
            <input type="text" id="filename[]" name="frmname" required="required">
          </div>
          <div class="form-group ">
            <label>Subir archivo:</label>
            <input type="file" id="file" name="file" multiple>
          </div>
          <button type="submit" class="btn btn-primary">Guardar</button>
        </form>
      </div>
    </div>
 
    <!-- jQuery -->
  <script src="assets/jquery/dist/jquery.min.js"></script>
  <!-- Bootstrap -->
  <script src="assets/bootstrap/dist/js/bootstrap.min.js"></script>
  <!-- JS -->
  <script src="assets/js/main.js"></script>
 </body>
</html>

if ($_FILES["imagen"]['error']==0) {

    if ($_FILES["imagen"]['type']=="image/png" || $_FILES["imagen"]['type']=="image/jpg" || $_FILES["imagen"]['type']=="image/jpeg") {

        $target_path = "../";
        $target_path = $target_path . basename( $_FILES['imagen']['name']); 

        if(move_uploaded_file($_FILES['imagen']['tmp_name'], $target_path)) 
            { 
                echo "El archivo ". basename( $_FILES['imagen']['name']). " ha sido subido";

            } else{
                echo "Ha ocurrido un error con el archivo ". basename( $_FILES['imagen']['name']) ;
            }   
    }else{echo $_FILES["imagen"]['type']." Solo se pueden subir archivos tipo JPG, PNG Y JPEG.";}

    }else{

        echo "Por favor seleccione un archivo para subir";
    }
?>
    
asked by Edison 0001 17.09.2018 в 20:32
source

1 answer

1

I would have the form call the PHP file directly since it is usually more secure. You'll have to make sure that the file path you're calling in the form's action is fine (important).

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="icon" href="http://i0.wp.com/www.hidromasa.com/wp-content/uploads/2016/08/icono-gota-agua-Hidromasa.png?fit=512%2C512" type="image/ico" />
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <title></title>
  </head>
  <!-- Bootstrap -->
  <link href="assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
  <body>
    <div class="container">
      <div class="row">
        <form action="enlace.php" method="post" enctype="multipart/form-data">
          <div class="form-group ">
            <label>Nombre:</label>
            <input type="text" id="filename" name="filename" required="required">
          </div>
          <div class="form-group ">
            <label>Subir archivo:</label>
            <input type="file" id="file" name="file" multiple>
          </div>
          <button type="submit" class="btn btn-primary">Guardar</button>
        </form>
      </div>
    </div>
 
    <!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 </body>
</html>

Now we go with the php file. I'm going to put in several points the commands "echo" to see that up to that point the data and the "die" arrive so that the program stops and it is easier to see the errors.

<?php

$nombre = $_POST['filename'];
//echo $nombre; die;  //Descomenta esto para ver que el nombre el input nombre llega bien.

if ($_FILES["file"]['error']==0) {

//echo "La imagen no da errores"; die;  //Descomenta esto para ver que no da error.

   if ($_FILES["file"]['type']=="image/png" || $_FILES["file"]['type']=="image/jpg" || $_FILES["file"]['type']=="image/jpeg") {

    //echo "Formato correcto"; die;  //Descomenta esto para ver que el formato es correcto.

    $target_path = "../";
    $target_path = $target_path . basename( $_FILES['file']['name']); 

       if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) 
        { 
            echo "El archivo ". basename( $_FILES['file']['name']). " ha sido subido"; //die;   //Descoemnta esto pa ver el mensaje anterior

        } else{

            echo "Ha ocurrido un error con el archivo ". basename( $_FILES['file']['name']) ; //die;   //Descoemnta esto pa ver el mensaje anterior

        }   
   }else{
     echo $_FILES["file"]['type']." Solo se pueden subir archivos tipo JPG, PNG Y JPEG.";  //die;   //Descoemnta esto pa ver el mensaje anterior
   }

}else{

    echo "Por favor seleccione un archivo para subir"; //die;   //Descoemnta esto pa ver el mensaje anterior
}
?>

I recommend that you go flit and see where you are staying. And if I find the error, I'll try to give you another solution.

    
answered by 19.09.2018 в 09:19