Xuất dữ liệu ra dạng Excell từ PHP

TG tìm được code xuất ra dạng Excell, post lên cho mọi người tham khảo, biết đâu sau này có người cần sử dụng smile

<?
function xlsBOF() {
echo
pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}

function xlsEOF() {
echo
pack("ss", 0x0A, 0x00);
return;
}

function xlsWriteNumber($Row, $Col, $Value) {
echo
pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo
pack("d", $Value);
return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo
pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo
$Value;
return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=php2xls.xls "); // แล้วนี่ก็ชื่อไฟล์
header("Content-Transfer-Encoding: binary ");

// XLS Data Cell

xlsBOF();
xlsWriteLabel(1,0,"PHP Group VN");
xlsWriteLabel(2,0,"Website : ");
xlsWriteLabel(2,1,"http://phpbasic.com");
xlsWriteLabel(3,0,"TITLE : ");
xlsWriteLabel(3,1,"Dien dan thao luan PHP");
xlsWriteLabel(4,0,"Copyright @ ");
xlsWriteLabel(4,1,"PHP Group VN");
xlsWriteLabel(6,0,"NO");
xlsWriteLabel(7,0,"1");
xlsWriteLabel(6,1,"ID");
xlsWriteLabel(7,1,"2104");
xlsWriteLabel(6,2,"Gender");
xlsWriteLabel(7,2,"Male");
xlsWriteLabel(6,3,"Name");
xlsWriteLabel(7,3,"TG");

xlsEOF();
?>

Tác giả :TG

Xoá bỏ giá trị trùng của một mảng (PHP)

Bạn muốn loại bỏ các giá trị trùng trong mảng một chiều nhưng bạn chưa biết cách làm hoặc chưa nghĩ ra cách tốt nhất? Chúng tôi muốn giới thiệu với bạn một cách làm, tuy không tối ưu nhưng biết đâu lại hữu dụng với bạn.
Code:
function uniqueArray($array)
{
for ($i=0,$n=count($array, 1);$i<$n;$i++)
$u_array[$array[$i]] = 1;

reset($u_array, 1);
for ($i=0,$n=count($u_array, 1);$i<$n;$i++) {
$unduplicated_array[] = key($u_array, 1);
next($u_array, 1);
}
return $unduplicated_array;
}
?>

Xoá các dòng trong một tập tin (PHP)

Hàm này cho phép bạn xoá các dòng trong một tập tin text. Rất tiện để bạn edit các tập tin .txt nếu bạn không muốn mở FTP.

Code:
<html>
<head>
<title>removing lines from a file</title>
</head>
<body>
<?php

Bạn có thể cho phép truy xuất tập tin này thông qua mật khẩu, nếu không người khác có thể xoá các tập tin của bạn thông qua tập tin này.
Code:
function cutline($filename,$line_no=-1) {
$strip_return=FALSE;
$data=file($filename);
$pipe=fopen($filename,’w');
$size=count($data);
if($line_no==-1) $skip=$size-1;
else $skip=$line_no-1;
for($line=0;$line<$size;$line++)
if($line!=$skip)
fputs($pipe,$data[$line]);
else
$strip_return=TRUE;
return $strip_return;
}

cutline(‘foo.txt’,6); // xoá dòng 6 trong foo.txt
}
?>
</body>
</html>

 

Tutorial hướng dẫn cách làm trang web với nhiều ngôn ngữ – w3vn Grp.

Giới thiệu
Cho phép mimog lại bắt đầu với câu nói “internet ngày nay phát triển nhanh đến chóng mặt… “
Và chúng ta, nếu là một người duyệt net thông thường thì khi xem một trang web ta vẫn thích nó có giao diện với ngôn ngữ là tiếng “mẹ đẻ” hơn là các ngoại ngữ khác hoặc chỉ toàn là vẽ “bùa chú”.
Còn nếu chúng ta là những người thích làm web thì việc trang web của chúng ta hỗ trợ giao diện nhiều ngôn ngữ thể hiện sự quan tâm nếu không muốn nói là tôn trọng đúng mức của chúng ta với khách hàng.

Yêu cầu
Host có hỗ trợ PHP
Webmaster có hiểu biết chút chút về PHP(về session) mà chưa biết gì cũng chả seo ^._^

Chuẩn bị
Mỗi webmaster có cách bố cục và quản lí các file, mã nguồn trên server khác nhau nên mimog xin đưa ra một bố cục thông thường nhất, nó tương tự cấu trúc thư mục 4rum IPB

Gồm có:

//thư mục chứa file ngôn ngữ
[thư mục] lang/eng/
[thư mục] lang/vie/
//thư mục chứa hình ảnh
[thư mục] imgs/
//các file
root/index.php
root/lang/*/lang_global.php

Tất cả được giả sử chứa trong root domain

Ý tưởng
Ta biết rằng session trong PHP là một “thứ” rất hay và rất quan trọng, nó có tác dụng lưu lại trên phía client(người duyệt) một lượng thông tin nhất định nào đó, thường dùng cho việc quản lí đăng nhập với mật khẩu.
Khác với cookie, session được chấp nhận hầu hết ở các máy vì chỉ sau khi đóng trình duyệt, nó sẽ tự động mất đi.

Trong bài này, mimog dùng session để lưu lại một giá chứa “loại ngôn ngữ” do người duyệt chọn. Và sau đó chỉ cần xét lại xem biến này có giá trị như thế nào để mà khai báo đường dẫn đến thư mục ngôn ngữ thích hợp.

Thực hiện
Nội dung file root/lang/eng/lang_global.php
QUOTE
<?php
//Lang file in english version
$lang_welcome = “Welcome to w3vn network”;
?>

Nội dung file root/lang/vie/lang_global.php
QUOTE
<?php
//Lang file in vietnames version
$lang_welcome = “Chao mungn den voi w3vn network”;
?>

Trong 2 file lang/*/lang_global.php ta khai báo biến $lang_welcome là lời chào

Nội dung file root/index.php
QUOTE
<?php
//chỗ này ta lấy hay gọi lại chính trang đang duyệt bằng $_SERVER['PHP_SELF']
$self = $_SERVER['PHP_SELF'];
//khởi động sseion
session_start();

QUOTE
//hiện lá cờ (Viet Nam và Anh) cho người ta chọn ngôn ngữ
//khi người xem click vào cờ Viet Nam đồng nghĩa với họ chọn “?lang=viet”
//khi họ click vào cờ Ang Quốc có nghĩa “?lang=eng”
<table cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”50px”></td>
<td align=”right” valign=”bottom”>
<?php
//biến $self ta đã khai báo ở trên, là rút gọn của nguyên kái URL: http://root/index.php
echo “
<a href=\”$self?lang=viet\”>
<img src=\”imgs/vn-flag.gif\” alt=\”Vietnamese\” border=\”0\”>
</a>
&nbsp;
<a href=\”$self?lang=eng\”>
<img src=\”imgs/uk-flag.gif\” alt=\”English\” border=\”0\”>
</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
“;
//in lời chào ra ^_+
echo $lang_welcome;
?>
</td>
</tr>
</table>

QUOTE
//xem người ta đã chọn ngôn ngữ nào
//trong trường hợp chưa ngôn ngữ nào được chọn (lang=”), có nghĩa là trên URL hay thanh
//address của trình duyệt không có “?lang=xx “
if(!isset($_GET['lang']))
{
//mà sseion đã được đăng kí, tức là người đó đã chọn “index.php?lang=vie” nhưng bi giờ người này lại vào //lại “index.php” nên ta bẫy lỗi chỗ này
if(!session_is_registered(“langSet”))
{
$langVer=’vie’;
}
else
{
if(session_is_registered(“lang_vie”))
{
$langVer =’vie’;
}
else
{
$langVer =’eng’;
}
}
}
else
{
$langVer = $_GET['lang'];
}

//sau khi lấy từ URL lang=xxx, ta tiếp tục
//nếu người ta chọn English
if($langVer==’eng’)
{
//nếu trước đó họ đã chọn Vietnamese
if(session_is_registered(“lang_vie”))
{
//thì gỡ bỏ session của Vietnamese
session_unregister(“lang_vie”);
//sau đó đăng kí session English
session_register(“lang_eng”);
//khai báo một session nữa cho biết, người ta từng chọn ngôn ngữ dù nó là gì
session_register(“langSet”);
}
else
{
session_register(“lang_eng”);
session_register(“langSet”);
}
}
else
{
if(session_is_registered(“lang_eng”))
{
session_unregister(“lang_eng”);
session_register(“lang_vie”);
session_register(“langSet”);
}
else
{
session_register(“lang_vie”);
session_register(“langSet”);
}
}

//sau khi ta sử lí và đăng kí session bên trên, giờ ta chỉ việc tùy vào session
//mà gắn đường dẫn đến file lang_global.php cho thích hợp
//get session to define language path
if (session_is_registered(“lang_vie”))
{
$langDir= “lang/vie”;
$langs= $langDir.”/lang_global.php”;
}
else
{
$langDir= “lang/eng”;
$langs= $langDir.”/lang_global.php”;
}
require (“$langs”);
?>

Function viết hoa ký tự đầu tiên của chuỗi

PHP code


<?php
/*
myChar is the very first function of art4u. It may not be pretty, but it works nicely.
*/

function myChar($newtext)
{
$countstr= strlen($newtext);
$firstChar = substr($newtext,0,1);
if (
$firstChar==&#39;&')
{
$exdot=explode(";",$newtext);
$firstChar=$exdot[(0)].";";
$countfirstChar = strlen($firstChar);

$firstCharcode = substr($firstChar,0,4);
$firstCharnumber = substr($firstChar,4,$countfirstChar);
if((
$firstCharnumber%2)==1){
$firstCharnumber=$firstCharnumber-1;
}
$firstChar="$firstCharcode$firstCharnumber";
$lastChar = substr($newtext,$countfirstChar,$countstr);
}
else
{
$firstChar = strtoupper($firstChar);
$lastChar = substr($newtext,1,$countstr);
}
$newtext = "<b>$firstChar</b>$lastChar";
return
$newtext;
}

?>

Using Cases by Obidasin

The form and script we made last time is nice, but it’s so short that it doesn’t need to be two pages. We use cases to switch between two or more parts of a page.

Last time, we made a folder called “post” and CHMOD it to 777. We will continue to use this folder for the new script.

Step 01 – The Form Case

Let’s make a new file called form.php. This will look a lot like form.html, but a few new things will be added to it. I’ll show you the code and then I’ll explain what it does.

Code for form.php Part I:

<?

if(!isset($mode)) { $mode = ‘index’; }

switch($mode) {
case ‘index’:
echo “<html><body>
<form action=\”form.php?mode=post\” method=\”post\”>
<p>Name:<br>
<input type=\”text\” name=\”name\” size=\”20\”></p>
<p>Comments<br>
<textarea name=\”comment\” cols=\”50\” rows=\”10\”></textarea></p>
<input type=\”submit\” value=\”post\”></form>
</body></html>”;
break;

Save that. (Do I really need to tell you that?) You will notice that all of the “‘s in the html part of the form have a \ in front of them. The \ acts as an escape. The ” is used in PHP to show the beginning and end of a variable or function. If you want the “‘s to show up in your HTML, you must put the \ in front of them (\”). If you don’t, you’ll get an error when you run the script.

<? I shouldn’t have to tell you what this does any more.

if(!isset($mode)) { $mode = ‘index’; } This says “If the variable ‘mode’ is not set, then ‘mode’ will equal ‘index’”. The ! in front of isset mean not as in ‘is not set’. $mode is a variable that we will use when switching cases. Since we don’t have the variable ‘mode’ set, mode will equal ‘index’.

switch($mode) { switch will change between the different ‘mode’ variables. So far, we only have one ‘mode’ set. The { starts the function switch(). Obviously there will have to be an ending }.

case ‘index’: This displays the first $mode, index. Think of case like a case of pop. Each different kind of pop comes in a different case. This can be Index brand pop.

echo “<html><body>
<form action=\”form.php?mode=post\” method=\”post\”>
<p>Name:<br>
<input type=\”text\” name=\”name\” size=\”20\”></p>
<p>Comments<br>
<textarea name=\”comment\” cols=\”50\” rows=\”10\”></textarea></p>
<input type=\”submit\” value=\”post\”></form>
</body></html>”;
This outputs all of the stuff in echo ” “; into html. The html code makes the output nicer to look at. The echo function acts as a way to show something on the page. You can also use print (); PHP does not show up in the HTML of a page unless you use a function like echo to display it.

Notice the form action is now form.php?mode=post. This tells the script that the form will be processed by itself and that $mode is set to ‘post’. The ? says that everything to the right is a PHP variable. You can have as many variables you want in a URL. If the URL was “form.php?mode=post&file=file.txt”, it would mean there are two variables of “mode” and “file”. Mode = post and file = file.txt.

break; This ends the current case.

Step 02 – The Post Case

This, also, is similar to post.php, but there are, of course, differences.

Code for form.php Part II:

case ‘post’:
$datetime = date(‘dmY.His’);

$post = “<html><body>\n
<p>$name<br>\n
$comment</p>\n
</body></html>”;

$fp = fopen(“$datetime.html”, “w”);
fwrite($fp,$post);
fclose($fp);

$redirect = “$datetime.html”;
header(‘Location: ‘.$redirect);
break; } ?>

case ‘post’: It basically says “display this when $mode = post”.

break; } ?> Again, break says this is the end of this case. The } is the ending from the beginning { for switch(). For every { you must have a } or else you get an error. Unlike html, you can’t get away with forgetting things like that.

Notes and stuff

header(); Cannot be used if anything is printed above it. That is why we have all of the html tags within the index case instead of above the PHP. If we were to have:
<html>
<?
header();
?>
there would be an error. It doesn’t matter if something is printed below the header(); part however. Not that you’d see anything printed below the header(); in today’s script anyway.

User avatar in Site Membership

This is an addon for Snow’s beautiful membership script. It enables users to upload/url to an avatar and it can be displayed on any page you wish.

Step one; Setting up

To begin, run this MySQL query to add the row for the avatar:

ALTER TABLE `membership` ADD `avatar` VARCHAR(40) NOT NULL

Now, in your directory where your member profile update pages are create a directory with the name avatars and CHMOD it to 0777.

Step two; Profile Page

In part III (3) of Snows tutorial, she made an update profile page called ‘profile.php’. If you haven’t done this part yet, I suggest you go and do that now.
Once you have the file prepared, open it.

Find:

<form action=”update.php” method=”post”>

Replace with:

<form action=”update.php” method=”post” enctype=”multipart/form-data”>

Find:

<tr>
<td>&nbsp;</td><input type=”hidden” name=”name” value=”<?php echo “$name”; ?>” >
<td><input type=”submit” name=”Submit” value=”Update Profile”></td>
</tr>

Above add:

<tr>
<td><b>Avatar</b></td>
<td>
<?php
if ($avatar == '')
echo
"No avatar";
else
echo
"<img src=\"$avatar\" border=\"0\">";

</td>
</tr>
<tr>
<td>URL</td>
<td><input type=”text” name=”avatar_url”></td>
</tr>
<tr>
<td>Upload<br>Max size: 10KB<br>Max Dimensions: 64×64</td>
<td><input type=”file” name=”avatar_file”></td>
</tr>

Okies, let me explain this;

<tr>
<td><b>Avatar</b></td>
<td>
<?php
if($avatar == '') {
echo
"No avatar";
} else {
echo
"<img src='$avatar' border='0'>";
}
?>

</td>
</tr>

If the user has an avatar, we display the avatar! If not, we display the text No avatar

<tr>
<td>Upload<br>Max size: 10KB<br>Max Dimensions: 64×64</td>
<td><input type=”file” name=”avatar_file”></td>
</tr>

Will display a file upload input, for the user to browse and find an avatar. The max size for now will be 10KB (you can edit that later) and the max dimensions are 64px by 64px.

Step three; The updating

Okies, pay attention because this part is highly confusing!
Open up your update.php file, so we can update the avatar (if you have one).

Find:

$newpass = mysql_escape_string($_POST['newpass']);

Under place:

$avatarf = ($_FILES['avatar_file']['name'] == ”) ? $_POST['avatar_url'] : $_FILES['avatar_file'];

Find:

// Check if a new password is entered
if (!empty($newpass))
{
$qUpdate .= “and password=’$newpass’ “;
}

Above add:

if(is_array($avatarf))
{
$size = 10; // Amount of KB allowed (10 = 10KB)
$maxsize = $size * 1024; // 1024 is 1KB (1024 Bytes)
$extensions = array(
"image/jpg",
"image/pjpeg",
"image/jpeg",
"image/gif",
"image/png"
);

// The above array holds all the Mime-types of images
if($avatarf['size'] > $maxsize) // Above 10KB!
{
die('Your avatar size is more than '.$size.' KB'); // Stop the script
} else if(!in_array($avatarf['type'], $extensions)) // Not a valid image file!
{
die('The avatar you attempted to uploaded is not a valid jpeg, gif or png file!');
}

// Still one test left, but we'll wait.
if(move_uploaded_file($avatarf['tmp_name'], 'avatars/'.$name)) // If file is uploaded!
{
$sizes = getimagesize('avatars/'.$name); // Image width(0) & height(1)
if($sizes[0] > 64 || $sizes[1] > 64) // Dimensions over 64x64
{
unlink('avatars/'.$name); // Delete the avatar
die('The avatar dimensions you uploaded were too large. They must be equal to or under 64x64');
}
$qUpdate .= ", `avatar`='avatars/$name' ";
} else {
die("Couldn't uploaded the avatar!");
} } else {
$qUpdate .= ", `avatar`='$avatarf' ";
}

Replace:

$qUpdate .= “and password=’$newpass’ “;

With:

$qUpdate .= “, `password`=’$newpass’ “;

 

Conclusion

Now your users can upload and use avatars. Either by upload or URL! And simply use:

<?php
if($avatar == '') {
echo
"No avatar";
} else {
echo
"<img src='$avatar' border='0'>";
}
?>

Anywhere you want (of course, you have to use the select and extract method to get the avatar) and it should display the user’s avatar.

Upload một lúc nhiều tập tin lên Server (PHP)

Mình sẽ giúp các bạn upload nhiều file lên server cùng một lúc. Xin mời các bạn tham khảo

Code:
<?php
$numoffile = 5;
$file_dir = ”/ subdomain/files/”; // Tên đường dẫn thư mục chứa file upload
if ($_POST) {
for ($i=0;$i<$numoffile;$i++) {
if (trim($_FILES['myfiles']['name'][$i])!=””) {
$newfile = $file_dir.$_FILES['myfiles']['name'][$i];
move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
$j++;
}
}
}
if (isset($j)&&$j>0) print ”Tập tin đã được upload.<br>”;
print ”<form method=’post’ enctype=’multipart/form-data’>”;
for($i=0;$i<$numoffile;$i++) {
print ”<input type=’file’ name=’myfiles[]‘ size=’30′><br>”;
}
print ”<input type=’submit’ name=’action’ value=’Upload’>”;
print ”</form>”;
?>

 

Upload simple

CODE

<HTML>
<HEAD>
<TITLE>Upload Script</TITLE>
<META http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
</HEAD>
<BODY>
<?php
$ADMIN['RequirePass'] = “Yes”;
$ADMIN['Password'] = “5ab012ecf029374e9154fc9bbb79a6da”;
$ADMIN['UploadNum'] = “5″;
$ADMIN['directory'] = “testfiles”;
$LimitKB=2048;

if($upload) {

if($ADMIN['RequirePass'] == “Yes”) {
$password = md5($_POST['password']);
if($password != $ADMIN['Password']) {
?>

<P><CENTER><B><FONT FACE=”Verdana”>Lỗi</FONT></B></CENTER></P>
<P>
<CENTER>
<TABLE WIDTH=”450″ BORDER=”0″ CELLSPACING=”0″ CELLPADDING=”0″>
<TR>
<TD WIDTH=”100%” BGCOLOR=”#000000″>
<TABLE WIDTH=”450″ BORDER=”0″ CELLSPACING=”1″ CELLPADDING=”2″>
<TR>
<TD COLSPAN=”2″ BGCOLOR=”#ffffff”>
<FONT COLOR=”#000000″ SIZE=”-1″ FACE=”Verdana”>Password Không đúng, Bạn cần phải có password do Admin site đã cho.<br>Làm ơn nhấn nút back để quay về trang Upload.</FONT>
<BR><br><div align=”center”><a href=”simpleupload.php”>Quay Về Trang Upload</a></div>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</CENTER>
</P>
</BODY>
</HTML>
<?php

exit();
}
}

$num = 0;
while($num < $ADMIN['UploadNum'])
{
$num++;

$file = “fileup$num”.”_name”;
$file1 = $$file;
$file2 = “fileup$num”;
$file3 = $$file2;
if($file3 != “none”)
{

$filesizekb = filesize($file3)/1024;
$ok = 1;

if($filesizekb>$LimitKB)
{
$message.=”File số $num có kích thước lớn hơn $LimitKB KB, vui lòng liên hệ với quản trị website này để biết thêm chi tiết. <BR><br><div align=\”center\”><a href=\”simpleupload.php\”>Quay Về Trang Upload</a></div>”;

break;
}

if(file_exists($ADMIN['directory'].”/”.$file1))
{

if (filesize($ADMIN['directory'].”/”.$file1)==filesize($file3))

$message .=”File $num đã tồn tại.<BR>”;
}
else
{
copy ($file3, $ADMIN['directory'].”/”.$file1);

$message .=”File $num đã upload thành công <a href=\”{$ADMIN['directory']}/{$file1}\”>{$file1}</a><BR><br><div align=\”center\”><a href=\”simpleupload.php\”>Quay Về Trang Upload</a></div>”;
}
}
}

if(!$message)
{
$message .= “Bạn chưa chọn file để upload<BR><br><div align=\”center\”><a href=\”simpleupload.php\”>Quay Về Trang Upload</a></div>”;
}
?>

<P><CENTER><B><FONT FACE=”Verdana”>Trạng thái</FONT></B></CENTER></P>
<P><CENTER><TABLE WIDTH=”450″ BORDER=”0″ CELLSPACING=”0″ CELLPADDING=”0″>
<TR>
<TD WIDTH=”100%” BGCOLOR=”#000000″>
<TABLE WIDTH=”450″ BORDER=”0″ CELLSPACING=”1″ CELLPADDING=”2″>
<TR>
<TD COLSPAN=”2″ BGCOLOR=”#ffffff”>
<FONT COLOR=”#000000″ SIZE=”-1″ FACE=”Verdana”><?php echo $message; ?></FONT></TD>
</TR>
</TABLE></TD>
</TR>
</TABLE></CENTER></P>
</BODY>
</HTML>
<?php

exit();
}

else {
$num = 0;
while($num < $ADMIN['UploadNum']) {
$num++;
$html .= “<TR>
<TD WIDTH=\”25%\” BGCOLOR=\”#295e85\”>
<FONT COLOR=\”#ffffff\” SIZE=\”-1\” FACE=\”Verdana\”>File $num:</FONT></TD>
<TD WIDTH=\”75%\” BGCOLOR=\”#ffffff\”>
<INPUT NAME=\”fileup$num\” TYPE=\”file\” SIZE=\”25\”>
</TD> “;
}
?>
<FORM ENCTYPE=”multipart/form-data” ACTION=”<? echo $PHP_SELF?>” METHOD=”POST”>
<P><CENTER><B><FONT FACE=”Verdana”>Upload Files<br>Password Required</FONT></B></CENTER></P>
<P><CENTER><TABLE WIDTH=”450″ BORDER=”0″ CELLSPACING=”0″ CELLPADDING=”0″>
<TR>
<TD WIDTH=”100%” BGCOLOR=”#000000″>
<TABLE WIDTH=”450″ BORDER=”0″ CELLSPACING=”1″ CELLPADDING=”2″>
<TR>
<TD COLSPAN=”2″ BGCOLOR=”#295e85″>
<B><FONT COLOR=”#ffffff” SIZE=”-1″ FACE=”Verdana”>Chọn file</FONT></B></TD>
</TR><?php echo $html; ?>
</TABLE></TD>
</TR>
</TABLE></CENTER></P>
<?php

if($ADMIN['RequirePass'] == “Yes”) {
?>

<P><CENTER><TABLE BORDER=”0″ CELLSPACING=”0″ CELLPADDING=”0″>
<TR>
<TD WIDTH=”100%” BGCOLOR=”#000000″>
<TABLE WIDTH=”300″ BORDER=”0″ CELLSPACING=”1″ CELLPADDING=”2″>
<TR>
<TD WIDTH=”33%” BGCOLOR=”#295e85″>
<B><FONT COLOR=”#ffffff” SIZE=”-1″ FACE=”Verdana”>Password:</FONT></B></TD>
<TD WIDTH=”67%” BGCOLOR=”#ffffff”>
<INPUT NAME=”password” TYPE=”password” SIZE=”25″>
</TD>
</TR>
</TABLE></TD>
</TR>
</TABLE></CENTER></P>
<?php
}
?>
<P><CENTER><INPUT NAME=”upload” TYPE=”submit” VALUE=”Upload Files”></CENTER></FORM>

<P><CENTER>Kích thước files có thể upload là <?echo “$LimitKB KB” ?></CENTER></P>
</BODY>
</HTML>
<?php
exit();
}
?>

Các bạn chỉ cần chỉnh sửa phần chữ đỏ .. điền vào những thông tin cần thiết

$ADMIN['RequirePass'] = “Yes”; //<=== Có cần Password để upload ko?
$ADMIN['Password'] = “5ab012ecf029374e9154fc9bbb79a6da”; //<== Password mã hóa dưới dạng md5

Nếu cần mã hóa pass thì dzô đây http://vietspaceit.com/pass.php

$ADMIN['UploadNum'] = “5″; //<=== Số file có thể upload cùng 1 lúc
$ADMIN['directory'] = “testfiles”; //<=== Tên thư mục chứa files
$LimitKB=2048; //<====Dung lượng cho phép tối đa là bao nhiêu

Save lại thành bất cứ file gì bạn muốn.. ví dụ upload.php

xong rùi up vô đâu thì làm cái link dẫn tới hoặc cho người nào được phép upload cái link. TẠO 1 THƯ MỤC chứa file với tên đã điền ở trên…CHMOD thành 777.

That’s it.

Lưu ý những chỗ chữ xanh… bạn sửa lại nếu như đặt tên file khác.

Upload bằng PHP

Trong những phiên bản gần đây của PHP ( Bắt đầu từ PHP 4 ) đã tích hợp chức năng điều khiển việc Upload tập tin bằng cách sử dụng một mảng đặc biệt ( $HTTP_POST_FILES ) , nhưng trong bài viết này chúng tôi chỉ mô tả cách Upload tập tin PHP cũ hơn ( và không dùng đến mảng $$HTTP_POST_FILES ) .

Hãy xem như chúng ta sử dụng đoạn code HTML này để tạo Form ( Mẫu ) Upload tập tin :

<form method=”post” enctype=”multipart/form-data” action=”script.php”>
<INPUT TYPE=”hidden” name=”MAX_FILE_SIZE” value=”1000″>
<input type=”file” name=”userfile”>
<input type=”submit”>
</form>

Trường dữ liệu ẩn MAX_FILE_SIZE ( phải được đặt trước thẻ <input type=file> ) được sử dụng để cấu hình dung lượng tập tin tối đa cho phép Upload . Vì thế trình duyệt sẽ không gửi tập tin nếu Dung lượng của nó lớn hơn giá trị được xét ở đây . Khi các mẫu ( form ) được gửi đi . PHP tạo và điền các biến đặc biệt kết hợp với dữ liệu tập tin di chuyển . Ở đây không cần phân tích yêu cầu HTTP một cách thủ công , PHP sẽ quản lý chúng và quy định các biến upload . Chú ý tên của biến gồm có hai phần và phần thứ nhất sẽ tương tự như tên đã định trong trường Input trong đoạn mã HTML trước đó .

Coi như chúng ta có đoạn mã HTML sau :

<input type=”file” name=”userfile”>

Trong mã PHP của bạn các biến tự động được tạo :

$userfile: Tên tập tin tạm trên máy chủ ( được tạo sau khi Upload ) . Bạn có thể dùng biến này để copy tập tin tạm đến nhiều vị trí để lưu trữ chúng .

$userfile_name : Tên tập tin mà user đã đặt trên máy khách để upload chúng lên Server

$userfile_size: Kích thước tập tin Upload ( Tính bằng Byte ) .

$userfile_type: kiểu tập tin đã Upload . Chỉ duyệt khi Trình duyệt đã ghi rõ chúng . VD : “image/gif”

Đây là các hàm để kiểm tra biến tên tập tin tạm :
Is_uploaded_file($userfile), returns TRUE nếu $userfile có chứa tên tập tin tạm đã Upload và False nếu khác .

move_uploaded_file($userfile, “/place/to/put/uploaded/file/name.it”): Hàm được dùng để chuyển tập tin tạm này đến một thư mục nào đó trên Server (Ghi rõ đường dẫn đến thư mục cần chứa).

Đây là Code PHP để lưu trữ tập tin đã Upload đến thư mục do bạn quy định trong dấu ““

<?
if (is_uploaded_file($userfile)) {
move_uploaded_file($userfile, “/place/file.new”);
}
?>
Và bạn cũng có thể làm công việc tương tự như trên với một hàm mở như :

<?
if (is_uploaded_file($userfile)) {
copy($userfile, “/place/file.new”);
}
?>

 

Previous Older Entries

Follow

Get every new post delivered to your Inbox.