Androidデバイスからphpサーバーにファイルをアップロードしようとしましたが、「通知:未定義のインデックス:3行目のupload_file.phpのファイル。これは何でしょうか?
これは、upload_file.php です。
<?php
if(isset($_POST))
{
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
echo $_FILES["file"]["type"] . ($_FILES["file"]["size"] < 2000000);
if (((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("files/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"files/" . $_FILES["file"]["name"]);
echo "Stored in: " . "files/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
else
{
echo "nothing posted";
}
?>
これは私の AndroidDevice で実行されます
/**
* POSTphoto
**/
private class POSTphoto
extends AsyncTask<String,String,String>
{
@Override
/** doInBackground */
protected String doInBackground( String... filename )
{
File file = new File( filename[0] );
HttpPost request;
MultipartEntity mpe = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE );
FileBody cbFile;
try
{
cbFile = new FileBody( file , "image/jpeg" );
mpe.addPart( "file" , cbFile );
// Creating the request.
String resource = APIcontroller.buildUrl( "" );
request = new HttpPost( resource );
request.addHeader( "Content-Type" , "multipart/form-data" );
request.setEntity( mpe );
return APIcontroller.execute( request );
}
catch ( Exception e )
{
e.printStackTrace();
return "some error";
}
}// doInBackground
@Override
/** Shows the content of the result.
* @param result is what the server returns in doInBackground.
**/
protected void onPostExecute( String result )
{
// CLose clients connection pool.
APIcontroller.closeClient();
if ( !result.equals( "" ) )
{ // Result not empty.
Log.d( "result" , result );
Toast.makeText( FilebrowserActivity.this , result ,
Toast.LENGTH_LONG ).show();
}
else
{ // Result empty.
Toast.makeText( FilebrowserActivity.this , "FAILED" ,
Toast.LENGTH_LONG ).show();
}
} // onPostExecute
} // POSTphoto