Search This Blog

2017/02/07

Insert two rows into db with one sql

MySQl
insert into table(columns...)values(value1...),(value2...),(value3...)
add more rows by ,(value...)
Oracle
insert all
into table(columns...)values(value1...)
into table(columns...)values(value2...)
select 1 from dual

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>