Show posted image Phonegap + PHP

Asked

Viewed 236 times

2

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 fine. However, I am 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 already tested:

1- I checked if $userid is correct in the photo path. 2- I used the function as ! file_exists() and it shows the right image. 3- Directly place the image src="http://localhost/app/_profile_photo/aquiOIdDoUsuario.jpg"> - Display the image.

I need to know how to do for the file_exists() function to check that the file is there.

Thank you.

1 answer

1

Try something like that:

$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";
}

Removed from here https://stackoverflow.com/questions/10444059/file-exists-returns-false-even-if-file-exist-remote-url

  • Hello Lucas, I made the changes according to the code passed and in this case it appears that the file exists. However, I deleted the file and even.

  • Switching from HTTP/1.0 404 Not Found to HTTP/1.1 404 Not Found works. Someone would know why and whether it is reliable in the sense of working?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.