issue for received image in php with upload script Android

一曲冷凌霜 提交于 2019-12-25 17:46:31

问题


I use this code for upload image: http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

I replaced

bm = BitmapFactory.decodeFile("/data/data/fshizzle.com/files/image.jpg");

and

HttpPost postRequest = new HttpPost("http://10.0.2.2/upload.php");

it's all!

I can not receved info in php file I use this code php work if use html code

<form method="POST" action="upload.php" enctype="multipart/form-data">
     <!-- On limite le fichier à 100Ko -->
     <input type="hidden" name="MAX_FILE_SIZE" value="100000">
     Fichier : <input type="file" name="avatar">
     <input type="submit" name="envoyer" value="Envoyer le fichier">
</form>

CODE PHP:

$dossier = './upload/';
$fichier = basename($_FILES['sfsdfsdf']['name']);
if(move_uploaded_file($_FILES['sfsdfsdf']['tmp_name'], $dossier . $fichier)) 
//Si la fonction renvoie TRUE, c'est que ça a fonctionné...
{
     echo 'Upload effectué avec succès !';
}
else //Sinon (la fonction renvoie FALSE).
{
     echo '<br>Echec de l\'upload !';
}

a simple if(isset($_FILES['sfsdfsdf'])) don't work what is a good code?


回答1:


If I understand your problem then I had the same thing yesterday. From the code you provided it seems like they have left out some crucial parts, such as the enctype. If you google upload image to php from android there are some better examples.

If you chose to do it this way make sure that your server-side permissions are set to allow php to create, write, and/or read files for whatever directory you are using. I found this method to be a real pain, and found it was much simpler to convert the image to base64 and send that as a string to the server, and have php create a file using the base64 string. If you can't figure out how to do that (google) let me know and I'll see if I could send you some code.




回答2:


a simple upload example to help you better understand the process.

<form method="POST" action="upload.php" enctype="multipart/form-data">
     <!-- On limite le fichier à 100Ko -->
     <input type="hidden" name="MAX_FILE_SIZE" value="100000">
     Fichier : <input type="file" name="avatar">
     <input type="submit" name="envoyer" value="Envoyer le fichier">
</form>

upload.php

<?php 
$dossier = './upload/';
//check for image mime types & under 100kb
if ($_FILES["avatar"]["type"] == "image/jpeg" ||
$_FILES["avatar"]["type"] == "image/png" ||
$_FILES["avatar"]["type"] == "image/gif"
&& $_FILES["avatar"]["size"] < 100000){

    //if server rejects the upload (http://php.net/manual/en/features.file-upload.errors.php)
    if($_FILES["avatar"]["error"] > 0){
        $uploadSuccess='<br>Echec de l\'upload !';
    }else{
        //check if file already exists
        if (file_exists($dossier.$_FILES['avatar']['name'])){
            $uploadSuccess=$_FILES["avatar"]["name"]." already exists.";
        }else{
            //move the uploaded file
            move_uploaded_file($_FILES["avatar"]["tmp_name"],$dossier.$_FILES['avatar']['name']);
            $uploadSuccess='Upload effectué avec succès !';
            //success
        }
    }
}else{$uploadSuccess='<br>Echec de l\'upload !';}

echo $uploadSuccess;
?>



回答3:


Solved i test with Mydns and my real phone and it's work.



来源:https://stackoverflow.com/questions/5558469/issue-for-received-image-in-php-with-upload-script-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!