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.