Search This Blog

2017/01/26

PHP upload file to server

<form method="post" enctype="multipart/from-data">
<input type="file" name="upload_file" >
</form>
Attr:
$_FILES['upload_file']
$_FILES['upload_file']['name']
$_FILES['upload_file']['tmp_name']
$_FILES['upload_file']['type']
$_FILES['upload_file']['size']
Check is file uploaded success:
if(!is_uploaded_file($_FILES['upload_file']['tmp_name'])) {
    unlink($_FILES['upload_file']['tmp_name']); // error
} else {
    // continue do something.
}
Check if file too big:
$maxsize = 10240;
if($_FILES['upload_file']['size'] > $maxsize) {
    unlink($_FILES['upload_file']['tmp_name']); // error
} else {
    // continue do something.
}
Check type:
if($_FILES['upload_file']['type'] != "image/gif") 
Move file:
move_uploaded_file($_FILES['upload_file']['tmp_name'], "path/" . $_FILES['upload_file']['name']);
Full sample(upload by one button):

2017/01/25

Integrating Google Sign-In error: status code 12501

When I try to integrating Google Sign-in activity into my app, happened to a error:
statusCode=unknown status code: 12501, resolution=null
It's caused by the wrong Server Client ID which saved in @values/strings.xml.
<string name="server_client_id">xxx-xxx.com</string>
It's web client id,not the android client id.

2017/01/19

Read file by php which forbidden by .htaccess

Folder "file" is forbidden to access by .htaccess file as below:
 Options -Indexes 
 # Controls who can get stuff from this server. 
 Order Deny,Allow 
 Deny from all 
 #Allow from localhost

Try to get content from /file/1.html which is forbidden to access directly through php.
Code:
echo "Read 1.html file in folder 'file':";
$file_handle = fopen($_SERVER['DOCUMENT_ROOT'] . "/file/1.html", "r"); 
//$file_handle = fopen("http://www.randinblogger.blogspot.com/", "r");
while (!feof($file_handle)) {
 $line = fgets($file_handle);
 echo $line;
}
fclose($file_handle);

2017/01/16

Android showAsAction: always doesn't work

If you use Android Support, it will no work as below.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:id="@+id/action_1"
 android:icon="@drawable/ic_action_1"
 android:showAsAction="ifRoom"
 android:title="@string/menu_1"></item>
</menu>

Solution:

Add xmlns:app="http://schemas.android.com/apk/res-auto", and use app:showAsAction instead of android:showAsAction

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" >
 <item android:id="@+id/action_1"
  android:icon="@drawable/ic_action_1"
  app:showAsAction="ifRoom"
  android:title="@string/menu_undo"></item>
</menu>

2017/01/13

Dynamic SQL in Oracle

Had a case which need to use daynamic SQL and execute immediate statement. Share and memo a solution.

Customer's Needs:
There're two tables, A and B.
1. Update some columns in table B
2. The name of columns which will be updated are saved in table A(colname)
3. The columns' new value are saved in table A(colvalue)
4. Before get data from table A, we don't know which column will be updated.

Method:
1. Get data from table A and put in an cursor.
2. Create a dynamic SQL
3. Execute dynamic SQL

Code:

2017/01/11

Chrome page refresh: normal/hard/hard with cache cleared up

Quick keys
Normal: Ctrl+R
Hard: Ctrl+Shift+R

Hold left click to refresh button or right click refresh button to call out menu, then left click the option.

Detection: if device is iPad/iPhone or Android or PC

Use navigator to get information of device.

Code:
<script>
var _device = navigator.userAgent;
var isMobile = _device.indexOf("Mobile") > 0;
var isAndroid = _device.indexOf("Android") > 0;
var isPad = _device.indexOf("iPad") > 0;
var isiPhone = _device.indexOf("iPhone") > 0;
if(isMobile && !isPad){
  // device is mobile
  if(isiPhone){
    // device is iPhone
  } else if(isAndroid){
    // device is Android
  }
} else if(isPad){
  // device is iPad
} else {
  // device is PC
}
</script>

2017/01/05

Tab select sample without redirect


Html:
<div class="tabs">
<ul>
<li><a id="tab_1" onclick="showTabContent(this.id);">Tab 1</a></li>
<li><a id="tab_2" onclick="showTabContent(this.id);">Tab 2</a></li>
<li><a id="tab_3" onclick="showTabContent(this.id);">Tab 3</a></li>
</ul>
</div>
<div class="tabContent" id="tabContent1">Content 1</div>
<div class="tabContent" id="tabContent2" style="display:none;">Content 2</div>

2017/01/04

Fix rigidbody2D.velocity not suported in Unity 5.*

Use
GetComponent<Rigidbody2D>()
instead of
rigidbody2D.velocity

Example:
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x,0);