繁体   English   中英

PHP Upload脚本在本地IIS服务器上与Android一起使用,但在Godaddy的服务器上托管时不起作用

[英]PHP Upload script working with Android on local IIS server but not when it's hosted on Godaddy's servers

所以我有一个PHP文件上传脚本:

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';
            }

    }

?>

使用HttpURLConnection从我的Android应用发送图像:System.out.println(“ file name is:” + fileName);

    String iFileName = fileName;
    String lineEnd = "\r\n";
    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;
        }
    }

当在本地IIS服务器上托管文件上传脚本并使用IP地址(例如192.168.0.3/androidImageUploader.php)创建connectURL对象时,如下所示:

connectURL = new URL(urlString);

正确上传了文件,并且收到了正确的响应。 但是,当Godaddy在我的域上托管上传脚本(URL像这样-http://dish5.com/androidImageUploader.php )时,该文件不会上传,并且响应是html和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>

我可以验证服务器上是否已安装PHP,因为我可以使用类似的文件上传PHP脚本从我的网站通过javascript上传图像。 对于造成此问题的原因,我确实感到困惑。 谢谢。

因此,我终于设法使其工作并在这里将解决方案发布给可能面临类似问题的其他任何人。

问题是PHP脚本不允许应用程序执行它,因为该应用程序未托管在相同的IP地址上。 因此,有必要将其附加在php脚本的顶部:

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

另外(尽管与问题不直接相关),将其包含在php.ini文件中会很有帮助:

upload_max_filesize = 10M

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM