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"> 
 </form>

Processing the Data: add.php

<?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 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("your.hostaddress.com", "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.
<?php

//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

<?php
$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>"; ?> 
output:
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

<?php 
 // Connects to your Database 
 mysql_connect("your.hostaddress.com", "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=http://www.yoursite.com/images/".$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.

 

Advertisements

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的工具。

php环境搭建!

1. 到phpmyadmin官方网站下载phpmyadmin,下载地址:http://www.phpmyadmin.net/home_page/downloads.php

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

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

输入您的数据库用户名和密码,通常用户名为root,密码为你的安装mysql时自己设置的密码
成功登陆后进到如下界面:

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 extension=mysql.so 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:

extension=mysqli.so //用/ 查找的时候,可以用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.

food.php:

<?php
mysql_connect("127.0.0.1","root","12345");
mysql_select_db("FOOD");
$sql=mysql_query("select * from FOOD where FOOD_NAME like 'A%'");
while($row=mysql_fetch_assoc($sql)) $output[]=$row;
print(json_encode($output));
mysql_close();
?>

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)) 

via 

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

<?php

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

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://codeincloud.tk/json_android_example.php");
TextView textView = (TextView)findViewById(R.id.textView1);
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) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

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 = "http://ws.login.com";

    private final String SOAP_ACTION = "http://ws.login.com/insertData";
    private final String METHOD_NAME = "insertData";

forget mysql root password in ubuntu

1. linux mysql的配置文件

windows下是my.ini
linux下是my.cnf

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

$ locate my.cnf
/etc/mysql/my.cnf

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

修改MySQL的登录设置:
# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables 
保存并且退出vi。

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

重新启动mysqld/mysql

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

将MySQL的登录设置修改回来 

# vi /etc/mysql/my.cnf
将刚才在[mysqld]的段中加上的skip-grant-tables删除 
保存并且退出vi。

重新启动mysqld 

# /etc/init.d/mysqld restart

sudo service mysql restart

mysql stop/waiting
mysql start/running, process 18577

改变文件权限:

chmod go-rw xxx.xxx //表示删除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命令:

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

例子:

  • mysql>grant select,insert,update,delete,create,drop on mydb.mytable to lzpddd@192.168.1.88 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 数据库名

注意:

(1)服务器端口标志-P一定要大些以区别于用户-p,如果直接连接数据库标志-D也要大写;

(2)如果要直接输入密码-p后面不能留有空格如-pmypassword;

(3)命令结束段没有’;’分号

例子:

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

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

连接到本机上的MYSQL命令行

首先打开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>

via http://blog.csdn.net/rly101112/article/details/8566237

http://rpbouman.blogspot.co.uk/2007/01/oracle-sql-developer-11-supports-mysql.html

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com 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

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

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