dtio35880438 2016-06-03 12:59
浏览 59
已采纳

PHP上载脚本在本地IIS服务器上使用Android,但在Godaddy的服务器上托管时则不行

So I have a PHP file upload script:

if ( 0 < $_FILES['file']['error'] ) {
        echo '0';
    }
    else {

        $uid = uniqid();
        //move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
            if(file_exists($_FILES['uploadedfile']['tmp_name'])){

                move_uploaded_file($_FILES['uploadedfile']['tmp_name'], 'uploads/'.$uid.'.jpg');
                echo 'http://dish5.com/uploads/'. $uid. '.jpg';
            }else {
                echo '0';
            }

    }

?>

Which gets sent an image from my Android app using an HttpURLConnection: System.out.println("file Name is :"+fileName);

    String iFileName = fileName;
    String lineEnd = "
";
    String twoHyphens = "--";
    String boundary = "*****";
    String Tag="fSnd";
    try
    {
        Log.e(Tag,"Starting Http File Sending to URL");

        // Open a HTTP connection to the URL
        if(connectURL!=null){
            //Log.d("connectURL", "Not null");
        }else{
            //Log.d("connectURL", "Null");
            return false;
        }
        HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();

        // Allow Inputs
        conn.setDoInput(true);

        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(Title);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);

        dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(Description);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);

        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);

        Log.e(Tag,"Headers are written");

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();

        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[ ] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable,maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0,bufferSize);
        }
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();

        dos.flush();

        Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));

        InputStream is = conn.getInputStream();

        // retrieve the response from server
        int ch;

        StringBuffer b =new StringBuffer();
        while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
        String s=b.toString();
        //responseString = s;
        Log.i("Response",s);
        dos.close();



        if(String.valueOf(conn.getResponseCode()).equals("200"))
        {

            if(storeLink && imageUploadActivity!=null){
                imageUploadActivity.imageUploaded(s);
            }

            return true;
        }else{
            return false;
        }
    }

When hosting the file upload script on my local IIS server and creating the connectURL object with the ip address (eg 192.168.0.3/androidImageUploader.php) like so :

connectURL = new URL(urlString);

The file is uploaded properly and the correct response is received. However, when Godaddy hosts the upload script on my domain (The url is like so - http://dish5.com/androidImageUploader.php), the file does not upload and the response is a strange combination of html and javascript :

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("505a0f7392ece3f917539691009ba5b1");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://www.dish5.com/androidImageUploader.php?ckattempt=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

I can verify that PHP is installed on the server since I am able to upload images through javascript from my website using a similar file upload PHP script. I'm really confused as to what is causing this issue. Thanks.

  • 写回答

1条回答 默认 最新

  • drmy1050 2016-06-04 11:57
    关注

    So I've finally managed to get it to work and am posting the solution here for anyone else who may be facing a similar issue.

    The problem was that the PHP script was not allowing the app to execute it as the app was not hosted on the same ip address. So it became necessary to append this at the top of the php script :

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');
    

    Additionally (Though not directly relevant to the question), it is helpful to have this in the php.ini file:

    upload_max_filesize = 10M
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵