Reputation: 7800
I have created an application, using which I can upload an image stored at the gallery of my phone to the server. The server side language that I am using is PHP. The issue that I am facing is little bit strange. I am able to upload photos of size less that 2 MB, which I have taken using the camera of my phone. But certain photos are not getting uploaded. Here are the details of photos which were not getting uploaded:
First photo:
Title: 2013-03-15 09.17.22.jpg
Type: JPEG
Size: 2645Kb
Second photo:
Title: 2013-03-12 11.48.04.jpg
Type: JPEG
Size: 2781Kb
After the unsuccessful uploading of above photos I tried to upload another photo and it worked. Here are the details of the photo that I was able to upload:
Title: 2013-03-14 10.20.16.jpg
Type: JPEG
Size: 1238Kb
Then tried with few other photos less that 2000 kb and all those photos got uploaded.
Then I download a jpg image of 6 MB from net and tried to upload it. Surprisingly it got uploaded. Now I am totally confused, what the real issue is. Finally I have reached a conclusion that, the problem is only for those photos which are taken using the camera of my phone and having a size more than 2000 kb.
Here is the android function that I am using to upload photos in to the server:
public String doFileUpload(String selectedPath,String page)
{
String response=null;
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = Connector.URL+page;
try
{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.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);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data;name=uploadedfile;filename=" + selectedPath + "" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try
{
inStream = new DataInputStream ( conn.getInputStream() );
response = inStream.readLine();
inStream.close();
}
catch (IOException ioex){
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
return response;
}
Here is the PHP code for the page that I use to upload images:
<?php
$tt=$_GET["id"];
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
include("../../classes/Registration.php");
$Obj=new register();
$Obj->insert_photo_data($tt,basename( $_FILES['uploadedfile']['name']));
}
else
{
echo '[{"flag":"0"}]';
}
?>
What I am getting back when the image uploading problem occurs is:
[{"flag":"0"}]
Upvotes: 0
Views: 1028
Reputation: 7800
I found the solution. The issue was related to wamp. I have opened php.ini and changed the line
upload_max_filesize = 2M
to
upload_max_filesize = 10M
Now images are getting uploaded without any problem.
Upvotes: 0
Reputation: 11224
Change. dos.write(buffer, 0, bufferSize); to dos.write(buffer, 0, bytesRead). What reaches the server if a picture is not uploaded?
Upvotes: 1