file uploading is corrupted [error]

1. Data URIs represent the data of the image. put all the data URIs the user drags into the box into an array, and post it to a PHP file. This PHP file will process the URIs and upload them to the server.

$randomName = substr_replace(sha1(microtime(true)), ”, 12).’.’.$mime;
if(file_put_contents($uploaddir.$randomName, $decodedData)) {
echo $randomName.”:uploaded successfully”;
else {
// Show an error message should something go wrong.
echo “Something went wrong. Check that the file isn’t corrupted”;

reason: the folder which holds the uploaded file, has no permission to be written by any user.

resolution: chmod a+w uploadfolder/

2. “I know how to submit data into a mySQL table through a form. Now I want to upload the image file to the remote directory (say, ‘images/’) but at the same time save the file-name in the table. ”

Often you want a user to be able to upload a photo, but you don’t want to bog down your database space by saving all the images directly into the database. You instead save the image to your server, but keep a record in the database of what file was saved so you can easily reference the image when needed.

First let’s create a database:

CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo VARCHAR(30)) 

Creating a Form:

This is simply an HTML form that you would use to collect information to be added to the database.

<form enctype="multipart/form-data" action="add.php" method="POST"> 
 Name: <input type="text" name="name"><br> 
 E-mail: <input type="text" name = "email"><br> 
 Phone: <input type="text" name = "phone"><br> 
 Photo: <input type="file" name="photo"><br> 
 <input type="submit" value="Add"> 

Processing the Data: add.php

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 

 // Connects to your Database 
 mysql_connect("", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("Database_Name") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 else {  
 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
  • only allowing photo uploads, you might consider limiting the allowed file types to jpg, gif and png.
  •  check if the file already exists: not overwrite
  • simply rename each file with a unique ID

Uniqid (prefix, more_entropy)

  • prefix: what will be appended to the beginning of each ID.
  • more_entropy. If this is false or not specified it will return 13 characters, if it is true then 23 characters will be returned.

//creates a unique id with the ‘about’ prefix

$a = uniqid(about); echo $a;   echo “<br>”;

//creates a longer unique id with the ‘about’ prefix

$b = uniqid (about, true); Echo $b; echo “<br>”;

//creates a unique ID with a random number as a prefix – more secure than a static prefix

$c = uniqid (rand (),true); echo $c; echo “<br>”;

//this md5 encrypts the username from above, so its ready to be stored in your database

$md5c = md5($c); echo $md5c; ?>

crypt () In Action

$password = crypt('mypassword'); 
 print $password . “ is the encrypted version of mypassword”;
$password = crypt('mypassword' , 'd4'); print $password . " is the CRYPT_STD_DES version of mypassword<br>"; 
$password = crypt('mypassword' , 'k783d.y1g'); print $password . " is the CRYPT_EXT_DES version of mypassword<br>"; 
$password = crypt('mypassword' , '$1$d4juhy6d$'); print $password . " is the CRYPT_MD5 version of mypassword<br>"; 
$password = crypt('mypassword' , '$2a$07$kiuhgfslerd...........$'); print $password . " is the CRYPT_BLOWFISH version of mypassword<br>"; ?> 
d4/qPbCcJ5tD. is the CRYPT_STD_DES version of mypassword
k7xEagYCDPPSc is the CRYPT_EXT_DES version of mypassword
$1$d4juhy6d$a.jIPYnvne1FWF2V6mGQR0 is the CRYPT_MD5 version of mypassword
$2a$07$kiuhgfslerd...........6k0kSI76CqJ/RWGnSp9MWRDF91gJZfW is the CRYPT_BLOWFISH version of mypassword
As long as you always use the same salt the encrypted password should always be the same, making it a good solution for password storage.

Testing uploaded Data

 // Connects to your Database 
 mysql_connect("", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("Database_Name") or die(mysql_error()) ; 

 //Retrieves data from MySQL 
 $data = mysql_query("SELECT * FROM employees") or die(mysql_error()); 

//Puts it into an array

while($info = mysql_fetch_array( $data )) {

//Outputs the image and other data

Echo "<img src=".$info['photo'] ."> <br>";

Echo "<b>Name:</b> ".$info['name'] . "<br> ";

Echo "<b>Email:</b> ".$info['email'] . " <br>";

Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";

} ?>

To show the image, we just use normal HTML for the image, and only change the last part (the actual image name) with the image name stored in our database.


latin1_swedish_ci & iso-8859-1 [charset] [phpmyadmin]

latin1_swedish_ci is iso-8859-1, you need to set utf8.

Tables can be of a different character set than default database character set, specially if the character set has been changed after you created the created your database.

If you look on the front page of phpMyAdmin, you’ll notice that when you create a new database, it has a setting for “MySQL connection collation:”. Be sure when you create a new database that both that the database collation are set to utf8. It may be as simple as recreating a new database, copying your current one to the new one, then renaming it.

The collation is only used for sorting and comparison of text (that’s why there’s a language term in there). The actual character set may be different. The most common failure is not in the database but rather in the connection between PHP and MySQLThe default charset for the connection is usually ISO-8859-1. You need to change that the first thing you do after connectiong, using either the SQL query SET NAMES 'utf-8'; or the mysql_set_charset function.

 check to make sure that everything is properly UTF-8:

  • Make sure that you are serving your HTML as UTF-8:
    • header(“Content-Type: text/html; charset=utf-8”);
  • Change your PHP default charset to utf-8:
    • ini_set(“default_charset”, ‘utf-8’);
  • If your database doesn’t ALWAYS talk in utf-8, then you may need to tell it on a per connection basis to ensure it’s in utf-8 mode, in MySQL you do that by issuing:
    • charset utf8
  • You may need to tell your webserver to always try to talk in UTF8, in Apache this command is:
    • AddDefaultCharset UTF-8
  • Finally, you need to ALWAYS make sure that you are using PHP functions that are properly UTF-8 complaint. This means always using the mb_* styled ‘multibyte aware’ string functions. It also means when calling functions such as htmlspecialchars(), that you include the appropriate ‘utf-8’ charset parameter at the end to make sure that it doesn’t encode them incorrectly.

If you miss up on any one step through your whole process, the encoding can be mangled and problems arise. Once you get in the ‘groove’ of doing utf-8 though, this all becomes second nature. And of course, PHP6 is supposed to be fully unicode complaint from the getgo, which will make lots of this easier (hopefully)

via stackoverflow

how to use phpMyAdmin

phpMyAdmin 是一个用PHP编写的,可以通过 web方式控制  phpMyAdmin和操作 MySQL数据库。通过 phpMyAdmin 可以完全对数据库进行操作,例如建立、复制、删除数据等等。如果使用合适的工具,MySQL 数据库的管理就会变得相当简单。应用 MySQL 命令行方式需要对 MySQL 知识非常熟悉,对 SQL语言也是同样的道理。不仅如此,如果数据库的访问量很大,列表中数据的读取就会相当困难。

当前出现很多 GUI MySQL客户程序,其中最为出色的是基于 Web 的 phpMyAdmin 工具。这是一种 MySQL 数据库前台的基于PHP的工具。


1. 到phpmyadmin官方网站下载phpmyadmin,下载地址:

2. 将phpmyadmin目录拷贝到web目录下,如果使用的apache 目录为:Apache/htdocs

3. 输入: http://域名/phpadmin/index.php 访问, 若成功安装phpmyadmin则显示如下界面:


Came cross some problems:

Extension mysqli is missing, phpmyadmin doesn’t work

ubuntu 12.04. installed apache2, php5, mysql and phpmyadmin, In the php.ini file, I uncommented line, but doesn’t work:

The mysqli extension is missing. Please check your PHP configuration. <a href=”Documentation.html#faqmysql” target=”documentation”><img src=”./themes/pmahomme/img/b_help.png” width=”11″ height=”11″ alt=”Documentation” title=”Documentation” /></a>

Latest phpMyAdmin versions require mysqli extension and will no longer work with mysql one (note the extra “i” at the end of it’s name).

sudo apt-get install php5-mysql //Will install package containing both old one and the new one, so afterwards all you need to do is to add in /etc/php5/apache2/php.ini: //用/ 查找的时候,可以用n来继续查找下一个

You need to restart apache server before navigating to phpmyadmin page:

sudo /etc/init.d/apache2 restart


Android – PHP – MySQL Scheme

Android Mysql PHP connect

  • connect android device to PHP Script
  • PHP Script will fetch data from the database. It will encode it into json format and send it to the device.
  • Android App will get these encoded data, then parse the data and display it on android device.

The PHP code will be very simple:
– Connect to the database
Run an SQL query, with a WHERE block depending on data from POST/GET values
– Output it in JSON format, the square bracket [ represents starting of an JSONArray node whereas curly bracket { represents JSONObject node.


$sql=mysql_query("select * from FOOD where FOOD_NAME like 'A%'");
while($row=mysql_fetch_assoc($sql)) $output[]=$row;

Android application:
– Use a HttpPost to get the data,
– Convert response to string
– Parse JSON data, and use it as you want

user unique id – I am generating unique user id in php using uniqid(”, true) function. Sample user id will be like 4f074eca601fb8.88015924

Encrypted Password – This password is stored using base64_encode method. Each password will need two columns to store in database. One is to store encrypted passwordand second column is to store salt used to encrypt the password.

Retrieve Data From a MySQL Database

mysql_fetch_array()which fetch a result row as an associative array, a numeric array, or both.

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 


read/send data from MySQL via PHP webservice

What is JSON

  • JSON stands for JavaScript Object Notation
  • JSON is lightweight text-data interchange format
  • JSON is language independent
  • JSON is “self-describing” and easy to understand
    (from w3schools)

1. a PHP web service which encodes a string array in to JSON format:  access that web service using Android http client and then it decodes json data


$data = array('name' => 'Froyo', 'version' => 'Android 2.2');
print (json_encode($data));
Android activity:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("");
TextView textView = (TextView)findViewById(;
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);
String name = object.getString("name");
String verion = object.getString("version");
textView.setText(name + " - " + verion);
catch (JSONException e) {
catch (ClientProtocolException e) {
catch (IOException e) {

2. A Web service to read data from the MySQL database

Java web service deployed on Tomcat server has a method which can run a quarry on database to retrieve data & this method returns results as a string output. To connect web service & database JDBC bridge will be used. The Android application calls that web service method remotely using ksoap library. Then web service runs a query on database table to retrieve data & returns data as a string to Android app. Android app display this data.

import ksoap2 library into project!!

Insert data to a database.

private final String NAMESPACE = "";

    private final String SOAP_ACTION = "";
    private final String METHOD_NAME = "insertData";

forget mysql root password in ubuntu

1. linux mysql的配置文件


find / -name 你要找的文件(my.cnf)
mysql 的配置文件 在/etc/my.cnf 是对的

$ locate my.cnf

ls -l /etc/mysql/my.cnf
-rw-r–r– 1 root root 3504 Dec 7 20:01 /etc/mysql/my.cnf

# vi /etc/my.cnf

  • :w!  若文件属性为只读,强制写入该文件
  • :wq  保存后退出,若为:wq!,则为强制保存后退出(常用)
  • o,O   插入新的一行:从光标所在处的下一行行首开始输入字符(常用)


sudo /etc/init.d/mysql stop
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql stop

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) utility, e.g. stop mysql
mysql stop/waiting

sudo service mysql restart

Start mysql client

mysql -u root

Flush privileges and reset the password

mysql> USE mysql ; 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user SET Password = password ( ‘new-password’ ) WHERE User = ‘root’ ; 
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0

mysql> flush privileges ; 
Query OK, 0 rows affected (0.01 sec)

mysql> quit


# vi /etc/mysql/my.cnf


# /etc/init.d/mysqld restart

sudo service mysql restart

mysql stop/waiting
mysql start/running, process 18577


chmod go-rw //表示删除xxx.xxx中组群和其他人的读和写的权限

  • u 代表所有者(user)
  • g 代表所有者所在的组群(group)
  • o 代表其他人,但不是u和g (other)
  • a 代表全部的人,也就是包括u,g和o
  •  +表示添加权限, – 表示删除权限,  = 表示使之成为唯一的权限

ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘db’

via Linux平台:忘记mysql的root密码解决办法

Reset lost/forgot MYSQL root password ubuntu

2. Azure 强密码需要以下 4 项中的 3 项

  • 小写字符
  • 大写字符
  • 数字 (0-9)
  • 符号(请参阅前面提到的密码限制)

默认情况下,用户在更改其密码时需要创建强密码。 管理员能够通过 适用于 Windows Power Shell 的 Windows Azure Active Directory 模块在用户级别启用和禁用此设置。

connect mysql via sql developer


grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’;


  • mysql>grant select,insert,update,delete,create,drop on mydb.mytable to lzpddd@ identified by ‘mypassword’; //给来自192.168.1.88的用户lzpddd分配可对数据库 mydb的mytable表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为’mypassword’;
  • mysql>grant all privileges on *.* to lzpddd@’%’ identified by ‘mypassword’; //给来自任何IP地址的用户lzpddd分配可对所有数据库的所有表进行所有操作的权限限,并设定口令为’mypassword’;


进入命令行,windows cmd,连接:

mysql -u 用户名 -p密码 -h 服务器IP地址 -P 服务器端MySQL端口号 -D 数据库名






mysql -u lzpddd -pmypassword -h -P 3306 -D mydb

或者: mysql -u 用户名 -p // 回车后要求输入密码,密码不可见


首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是如果-p后带有用户密码,那么-p与密码之间必须没有空格,否则让你重新输入密码.例如以下都是合法的登陆:(帐号:root   密码:123)
mysql -u root -p
mysql -uroot -p
mysql -uroot -p123

如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是: mysql>



Just another site

Jing's Blog

Just another site

Start from here......







Just another site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision


Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness



Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

John's Weblog

Just another weblog

I Am That I Am

Chasing tomorrow's sunrise.