php– 改进2在Postman和Localhost中工作但不在亚马逊AWS Live Server上工作

php– 改进2在Postman和Localhost中工作但不在亚马逊AWS Live Server上工作,第1张

概述我的改造实例与localhost(包括android代码和邮递员)一起工作正常,并且在邮递员工作正常.但是当我去亚马逊AWS实例(在线a.k.a)时,android部分无效.请参阅下面的代码.来自android的错误日志是D/响应:{“error”:true,“message”:“所需参数不可用”}有趣的是问题是大文件大小2M,小文

我的改造实例与localhost(包括android代码和邮递员)一起工作正常,并且在邮递员工作正常.但是当我去亚马逊AWS实例(在线a.k.a)时,androID部分无效.请参阅下面的代码.来自androID的错误日志是D /响应:{“error”:true,“message”:“所需参数不可用”}

有趣的是问题是大文件大小2M,小文件大小24KB正在上传罚款.我已经检查了PHP.ini文件,最大大小有25MB

我正在学习本教程:https://www.simplifiedcoding.net/retrofit-upload-file-tutorial/

public interface API {    //the base URL for our API    //make sure you are not using localhost    //find the ip usinc ipconfig command    String BASE_URL = "http://creative-thinker.com/files/images/secret_crushes/";    //this is our multipart request    //we have two parameters on is name and other one is description    @Multipart    @POST("API.PHP?APIcall=upload")    Call<MyResponse> uploadImage(@Part("image\"; filename=\"myfile.jpg\" ")                                         Requestbody file, @Part("desc") Requestbody desc);}

我的对象类

public class MyResponse {    public boolean error;    String message;}

我的片段

ivImageVIEw.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {        // Open The file Choose And Send To Activity For Result        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);        startActivityForResult(i, 100);    }});@OverrIDe    public voID onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (requestCode == 100 && data != null) {            //the image URI            Uri selectedImage = data.getData();            //calling the upload file method after choosing the file            uploadfile(selectedImage, "My Image");        }    }    private voID uploadfile(Uri fileUri, String desc) {        //creating a file        file file = new file(getRealPathFromURI(fileUri));        //creating request body for file        Requestbody requestfile = Requestbody.create(MediaType.parse(getContext().getContentResolver().getType(fileUri)), file);        Requestbody descBody = Requestbody.create(MediaType.parse("text/plain"), desc);        //The gson builder        Gson gson = new GsonBuilder()                .setLenIEnt()                .create();        //creating retrofit object        Retrofit retrofit = new Retrofit.Builder()                .baseUrl(API.BASE_URL)                .addConverterFactory(GsonConverterFactory.create(gson))                .build();        //creating our API        API API = retrofit.create(API.class);        //creating a call and calling the upload image method        Call<MyResponse> call = API.uploadImage(requestfile, descBody);        //finally performing the call        call.enqueue(new Callback<MyResponse>() {            @OverrIDe            public voID onResponse(Call<MyResponse> call, Response<MyResponse> response) {                if (!response.body().error) {                    Toast.makeText(getContext().getApplicationContext(), "file Uploaded Successfully...", Toast.LENGTH_LONG).show();                } else {                    MyResponse res= response.body();                    Log.d("response", new Gson().toJson(res));                    Toast.makeText(getContext().getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show();                }            }            @OverrIDe            public voID onFailure(Call<MyResponse> call, Throwable t) {                Toast.makeText(getContext().getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();            }        });    }    /*     * This method is fetching the absolute path of the image file     * if you want to upload other kind of files like .pdf, .docx     * you need to make changes on this method only     * Rest part will be the same     * */    private String getRealPathFromURI(Uri contentUri) {        String[] proj = {MediaStore.Images.Media.DATA};        CursorLoader loader = new CursorLoader(getContext(), contentUri, proj, null, null, null);        Cursor cursor = loader.loadInBackground();        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);        cursor.movetoFirst();        String result = cursor.getString(column_index);        cursor.close();        return result;    }

fileHandler.PHP

<?PHPclass fileHandler{    private $con;    public function __construct()    {        require_once dirname(__file__) . '/DbConnect.PHP';        $db = new DbConnect();        $this->con = $db->connect();    }    public function savefile($file, $extension, $desc)    {        $name = round(microtime(true) * 1000) . '.' . $extension;        $filedest = dirname(__file__) . UPLOAD_PATH . $name;        move_uploaded_file($file, $filedest);        $url = $server_ip = gethostbyname(gethostname());        $stmt = $this->con->prepare("INSERT INTO images (description, image) VALUES (?, ?)");        $stmt->bind_param("ss", $desc, $name);        if ($stmt->execute()) {            return true;        }        return false;    }    public function getAllfiles()    {        $stmt = $this->con->prepare("SELECT ID, description, image FROM images ORDER BY ID DESC");        $stmt->execute();        $stmt->bind_result($ID, $desc, $url);        $images = array();        while ($stmt->fetch()) {            $temp = array();            $absurl = 'http://' . gethostbyname(gethostname()) . '/files/images/secret_crushes' . UPLOAD_PATH . $url;            $temp['ID'] = $ID;            $temp['desc'] = $desc;            $temp['url'] = $absurl;            array_push($images, $temp);        }        return $images;    }}

API.PHP

<?PHPrequire_once dirname(__file__) . '/fileHandler.PHP';$response = array();if (isset($_GET['APIcall'])) {    switch ($_GET['APIcall']) {        case 'upload':            if (isset($_POST['desc']) && strlen($_POST['desc']) > 0 && $_fileS['image']['error'] === UPLOAD_ERR_OK) {                $upload = new fileHandler();                $file = $_fileS['image']['tmp_name'];                $desc = $_POST['desc'];                if ($upload->savefile($file, getfileExtension($_fileS['image']['name']), $desc)) {                    $response['error'] = false;                    $response['message'] = 'file Uploaded Successfullly';                }            } else {                $response['error'] = true;                $response['message'] = 'required parameters are not available';            }            break;        case 'getallimages':            $upload = new fileHandler();            $response['error'] = false;            $response['images'] = $upload->getAllfiles();            break;    }}echo Json_encode($response);function getfileExtension($file){    $path_parts = pathinfo($file);    return $path_parts['extension'];}

我试图捕获API.PHP中的项目值

$response [‘message’] = $string;只是在哪里$response [‘message’] =’所需参数不可用’;

并获得以下结果

    $string = $_POST['desc'];    D/response: {"error":true,"message":"My Image"}    $string = $_POST['desc'];    $string = strlen($string);    D/response: {"error":true,"message":"8"}    $string = $_fileS['image']['error'];    D/response: {"error":true}    $string = UPLOAD_ERR_OK;    D/response: {"error":true,"message":"0"}

并删除&& $_fileS [‘image’] [‘error’] === UPLOAD_ERR_OK将数据输入数据库但没有图像仍然上传

解决方法:

最后在搜索网络后找到解决方案…问题是文件大小

我在PHP 7和Apache上.所以配置需要在两个地方改变

配置

upload_max_filesize = 25Mpost_max_size = 25Mmax_execution_time = 300max_input_time = 300memory_limit = 128M

位置

sudo nano /etc/PHP/7.0/fpm/PHP.inisudo nano /etc/PHP/7.0/apache2/PHP.ini

显然,需要在Amazon AWS上为PHP和Apache更改设置.希望它能帮助别人.

总结

以上是内存溢出为你收集整理的php – 改进2在Postman和Localhost中工作但不在亚马逊AWS Live Server上工作全部内容,希望文章能够帮你解决php – 改进2在Postman和Localhost中工作但不在亚马逊AWS Live Server上工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1100797.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存