Show picture sent Phonegap + PHP

1
Hello, I'm using the following code to upload the image of a phonegap app to a server:

    // Wait for PhoneGap to load
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    function onDeviceReady() {
    // Do cool things here...
    }

    function getImage() {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(uploadPhoto, function(message) {
        alert('get picture failed');
    },{
        quality: 100, 
        destinationType: navigator.camera.DestinationType.FILE_URI,allowEdit: true,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
    }
        );

    }

    function uploadPhoto(imageURI) {
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpeg";

        var params = new Object();
        params.value1 = "test";
        params.value2 = "param";


        options.params = {
        email : localStorage.getItem('email')
        }
        options.chunkedMode = false;

        var ft = new FileTransfer();
        ft.upload(imageURI, "http://localhost/app/photo.php", win, fail, options);
    }

    function win() {

        location.href="settings.html";
    }

    function fail(error) {
        alert("Erro ao enviar foto!");
    }

    </script>

Apparently it works without problems. However, I'm using the php code below to check whether the file exists or not, so if it exists it will put the photo otherwise it puts a default image:

  $setphoto =     "http://localhost/app/_profile_photo/$userid.jpg";
  $photodefault = "http://localhost/app/_profile_photo/default-image.png";



if (file_exists($setphoto)){

   echo "<img src=\"$setphoto\" width=\"80\" height=\"80\">";

}

else{

    echo "<img src=\"$photodefault\" width=\"80\" height=\"80\">";
}

The problem is that the file_exists () function is not recognizing the uploaded file and always takes the default photo.

What I've tested:

1- Check if the $ userid is correct in the path of the photo. 2- I used the function as! File_exists () and it shows the right image. 3- Right-click on the html image src="http://localhost/app/_profile_photo/aquiOIdDoUsuario.jpg" > - Show the image.

I need to know how to make the file_exists () function verify that the file is.

Thank you.

    
asked by anonymous 16.06.2016 / 19:30

1 answer

0

Try something like this:

$filename= 'http://localhost/app/_profile_photo/$userid.jpg';
$file_headers = @get_headers($filename);

if($file_headers[0] == 'HTTP/1.0 404 Not Found'){
      echo "O file $filename não existe";
} else if ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'){
      echo "O file $filename não existe, e redirecionou para uma custom 404 page..";
} else {
      echo "The file $filename exists";
}

Retired from here link

    
16.06.2016 / 22:32