星期五, 11月 12, 2010

PHP&MySQL--10 2010年11月課程講義預覽

PHP&MySQL--10
一、 上傳多個檔案
【提供表單網頁upload4.htm及接收資料網頁upload4.php,再請修改】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉處理上傳多個檔案〈/title〉〈/head〉 〈body〉〈?
$uploaddir='./upload/';
$i=count($_FILES["fileupload"]["name"]);
for ($j=0;$j〈$i;$j++){
$tmpfile=$_FILES["fileupload"]["tmp_name"][$j];
$file2=mb_convert_encoding($_FILES["fileupload"]["name"][$j],"big5","utf8");
if(move_uploaded_file($tmpfile,$uploaddir.$file2)){
echo "上傳成功〈br〉";
echo "檔案名稱:".$_FILES["fileupload"]["name"][$j]."〈br〉";
echo "檔案類型:".$_FILES["fileupload"]["type"][$j]."〈br〉";
echo "檔案大小:".$_FILES["fileupload"]["size"][$j]."〈br〉";
}else{
echo $_FILES["fileupload"]["tmp_name"][$j]."上傳失敗!〈br /〉";
switch ($_FILES["fileupload"]["error"][$j]){
case 1:
echo "失敗原因:大小超過php.ini內設定 upload_max_filesize"."〈br〉";
break;
case 2:
echo "失敗原因:大小超過表單設定 MAX_FILE_SIZE"."〈br〉";
break;
case 3:
echo "失敗原因:上傳不完整"."〈br〉";
break;
case 4:
echo "失敗原因:沒有檔案上傳"."〈br〉";
break;
case 6:
echo "失敗原因:暫存資料夾不存在"."〈br〉";
break;
case 7:
echo "失敗原因:上傳檔案無法寫入"."〈br〉";
break;
case 8:
echo "失敗原因:上傳停止"."〈br〉";
break;
} } } ?〉
〈/body〉〈/html〉




二、 繪製圖片—graph.php


〈? $height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, 'Sales', $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);?〉



三、 如何呼叫已經產生圖片的網頁? (請自行設計)


〈img src="graph.php" height=200 width=200 alt="顯示php的內容"〉



四、 輸入資料檢驗驗證碼
1. 表單部分


〈html〉 〈head〉〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉〈body〉〈form action="input.php" method="post"〉
〈input type="text" name="graph" maxlength="7" size="7"〉
〈input type="submit"〉〈/form〉
〈img src="graph.php" width="200" height="200" alt="show image"/〉
〈/body〉〈/html〉



2. 驗證碼部分(graph.php,請修改網頁)


〈? session_start( );
$_SESSION['test']=generatorPassword( );
$height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, $_SESSION['test'], $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);
function generatorPassword( )
{ $password_len = 7;
$password = '';
$word = '0123456789';
$len = strlen($word);
for ($i = 0; $i 〈 $password_len; $i++) {
$password .= $word[rand( ) % $len]; }
return $password; } ?〉



3. 接收資料

〈?session_start( ); ?〉〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉驗證資料〈/title〉〈/head〉〈body〉
〈? if ($_POST['graph']==$_SESSION['test'])
{ ?〉
〈script language="JavaScript"〉
alert("〈? echo "輸入資料正確,轉往google"; ?〉");
location.href="http://www.google.com";
〈/script〉〈? }
else
{ ?〉
〈script language="JavaScript"〉
alert("輸入資料不正確,請重新輸入");
history.back( );
〈/script〉 〈?
} ?〉 〈/body〉〈/html〉




五、 簡單資料操作
1. 新增


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉新增〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");

mysql_query("insert into student (account,username,sex,date1) values('$account','$username','$sex','$temp_date') ") or die("error");

header("location:index.php");
?〉〈/body〉〈/html〉



2. 列表

〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉表列資料〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$res =mysql_query("select * from student") or dir("sql error");
?〉〈a href="add.htm"〉新增〈/a〉〈br〉 〈?
while($row=mysql_fetch_array($res)){
echo "姓名:".$row["username"]."帳號:".$row["account"]."性別:";
if($row["sex"] == 1)
echo "男";
else
echo "女";
echo "日期:".$row["date1"];?〉
〈a href="edit.php?id= 〈? echo $row["id"]; ?〉"〉編輯〈/a〉
〈a href="del.php?id= 〈? echo $row["id"]; ?〉"〉刪除〈/a〉
〈br〉 〈?
}
?〉〈/body〉〈/html〉




3. 編輯

〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉

〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉




〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉





4. 刪除


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



六、 CSV檔案匯出與匯入
1. 匯入【接收端網頁】


〈? include("server.php"); ?〉〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉匯入 CSV 檔〈/title〉〈/head〉〈body〉
〈?
$uploaddir='./';
if ($_FILES["CSVfile"]["name"] ) {
$temp_SQL = "";
$file2=mb_convert_encoding($_FILES["CSVfile"]["name"],"big5","utf8");
if(!move_uploaded_file($_FILES['CSVfile']['tmp_name'],$uploaddir.$file2)){
echo "上傳失敗〈br〉";
}
$fp = fopen($_FILES["CSVfile"]["name"] , "r");
while ( $ROW = fgetcsv($fp,$_FILES["CSVfile"]["size"] ) ) {
if ( strlen($ROW[0]) ) {
if ( strlen($temp_SQL) ) $temp_SQL .= ", ";
$temp_SQL .= "('" . $ROW[0] . "', '" . $ROW[1] . "', '" . $ROW[2] . "', '" .
$ROW[3] . "') ";
}
}
fclose($fp);
$sql = "INSERT INTO student1(sid, username,address,birthday) VALUES " . $temp_SQL;
mysql_query($sql) or die(mysql_error());
}
else {
echo "Error!";
}?〉〈/body〉〈/html〉



2. 匯出


〈?
include("server.php");
$file = 'export';
$result = mysql_query("show columns from student1");
$i = 0;
if (mysql_num_rows($result) 〉 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("select * from student1");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j〈$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
};
ini_set("date.timezone","Asia/Taipei");
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?〉



七、 上傳與解壓縮


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉zip檔案上傳〈/title〉〈/head〉〈body〉〈?
include ('pclzip.lib.php');
$tmp_folder = basename($_FILES['myfile']['name']);
if(is_dir($tmp_folder))
{
echo "資料夾已存在"."〈br〉";
}
else
{
//echo "資料夾不存在"."〈br〉";
if(mkdir($tmp_folder, 0700))
echo "成功建立資料夾"."〈br〉";
else
echo "建立資料夾失敗"."〈br〉";
}
if($_FILES['myfile']['size']〉0){
if(is_uploaded_file($_FILES['myfile']['tmp_name'])&&$_FILES['myfile']['type']=='application/zip'){
if(is_writeable($tmp_folder)){
if(move_uploaded_file($_FILES['myfile']['tmp_name'],$tmp_folder.'/'.$_FILES['myfile']['name'])){
echo '檔案上傳成功!〈p〉';
$hzip = new PclZip($tmp_folder.'/'.$_FILES['myfile']['name']);
if($hzip-〉extract($tmp_folder.'/'))
echo '檔案解壓縮成功!';
else
echo '檔案解壓縮失敗!';
} else
echo '檔案上傳失敗!〈p〉';
} else
echo '檔案無法寫入!';
} else
echo '沒有上傳檔案!';
}
?〉〈/body〉〈/html〉




八、 其他

沒有留言:

張貼留言