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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料