10/10/2018, 11:13

Ban ve upload trong PHP.

Trong cuon sach cua tac gia Nguyen Huu Khang co mot doan trinh bay ve upload...nhung rat tiec no khong chay.????!!!
Hien minh dang can mot doan upload file bang PHP, ban nao co the giup minh duoc khong???
Xin hoi them, upload co phu thuoc vao may chu chay Linux/Unix hay Windows khong vay?? Neu co thi` lam the nao de up load thanh cong day???
Xin cam on da doc bai nay, neu ban co cau tra loi thi lai thuc su thanks!
hanavncom viết 13:14 ngày 10/10/2018
Sọan 1 file php với nội dụng như phía dưới và upload vào nơi mà bạn muốn upload file . chúc vui vẻ:

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
</head> 
<body> 
<?php 
//một script giúp bạn upload file lên server 
$site_name $_SERVER***91;'HTTP_HOST'***93;; 
$url_dir "http://".$_SERVER***91;'HTTP_HOST'***93;.dirname($_SERVER***91;'PHP_SELF'***93;); 
$url_this "http://".$_SERVER***91;'HTTP_HOST'***93;.$_SERVER***91;'PHP_SELF'***93;; 

$upload_dir ""
$upload_url $url_dir."/"
$dir opendir("."); 
$message =""

if (
$_FILES***91;'userfile'***93;) { 
$message do_upload($upload_dir$upload_url); 


print 
$message

function 
do_upload($upload_dir$upload_url) { 

$temp_name $_FILES***91;'userfile'***93;***91;'tmp_name'***93;; 
$file_name $_FILES***91;'userfile'***93;***91;'name'***93;; 
$file_type $_FILES***91;'userfile'***93;***91;'type'***93;; 
$file_size $_FILES***91;'userfile'***93;***91;'size'***93;; 
$result $_FILES***91;'userfile'***93;***91;'error'***93;; 
$file_url $upload_url.$file_name
$file_path $upload_dir.$file_name

//Kiểm tra tên file 
if ( $file_name =="") { 
$message "Tên file không hợp lệ"
return 
$message

//Kiểm tra dung lượng file upload 
// ở đây tôi sét là 5mb 
else if ( $file_size 5000000) { 
$message "The file size is over 5mb."
return 
$message

//Kiểm tra kiểu file upload 
else if ( $file_type == "text/plain" ) { 
$message "Sorry, Bạn không thể upload script file" 
return 
$message

//Upload file 
$result move_uploaded_file($temp_name$file_path); 
$message = ($result)?"Url file đã upload <a href='.$file_url.'>$file_url</a>" 
"Có lỗi trong quá trình upload file."

return 
$message

?> 
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post"> 
File upload :<input type="file" id="userfile" name="userfile"> 
<input type="submit" name="upload" value="Upload"> 
</form> 
<?php 
// hiển thỉ danh sách các file trong thư mục uploads 

while (FALSE !== ($file readdir($dir))) { 
if (
is_dir($file)) { 
//doc tên thư mục 
echo "<a href=\"$file\"><span class=\"directory\">$file/</span></a><br>\n"
} else 
//doc ten file 
$non_dirs***91;***93; = "<a href=\"$file\">$file</a><br>\n"


foreach (
$non_dirs as $value) { 
echo 
$value

closedir($dir); 
?> 
</body> 
</html>
hanavncom viết 13:19 ngày 10/10/2018
Tôi định post thêm 1 bài nữa, song đã gặp lỗi giới hạn về ký tự bài viết nên chịu, bạn thông cảm.

The text that you have entered is too long (32418 characters). Please shorten it to 15000 characters long.
hanavncom viết 13:13 ngày 10/10/2018
Mới tìm thêm cho bạn 1 cái khá hay:

PHP Code:
<?php 
#------------------------------------------------------------------ 
# Php script : upload_url.php 
# Description : upload from url to your host 
# Author : Do Duy Cop - ***91;email***93;doduycop@yahoo.com***91;/email***93; 
# Modify : 9/21/2003-8:03 PM 
#------------------------------------------------------------------ 

#------------------------------------------------------------------ 
# function name : upload_url 
# @url : url of file want copy 
# @save_to : path of file will be save 
# @over_write : over_write=0: if exist file(save_to) then do nothing 
# @$block_read : default is 1048576 bytes ( = 1 MB) 
#------------------------------------------------------------------ 
function upload_url($url$save_to$over_write=0$block_read=1048576){ 
if(!
$fr=@fopen("$url","rb")){ 
echo
"Can not open <a href='$url'><b>URL</b></a>"
return 
0


echo
"Open <a href='$url'><b>URL</b></a> for reading: <b>OK</b><br />\n"

if(@
is_file("$save_to")) 
if(!
$over_write){ 
echo
"Exist file <b>$save_to</b> ".@filesize("$save_to")." bytes<br />\n"
return 
0
}else 
echo
"<b>Ready Over-write</b>: "
else 
echo
"<b>Ready Write</b>: "

if(!
$fw=@fopen("$save_to","wb")){ 
echo
"But can not create file <b>$save_to</b>\n"
return 
0


echo
"Each block limit is $block_read bytes. Symbol status <b>W</b>: write OK, <b>E</b>: write ERROR<br />\n"

while(!
feof($fr)){ 
$data=@fread($fr,$block_read); 
if(
$len=strlen($data)) 
echo((
$len==@fwrite($fw,$data,$len)) ? 'W' 'E'); 
if((@
$count_loop++ % 56)==55)echo"<br />\n"


@
fclose($fr); 
@
fclose($fw); 

echo
"<br />\n<b>Finish</b> Upload from <a href='$url'><b>URL</b></a> To file <b>$save_to</b> : ".@filesize("$save_to")." bytes<br />\n"
flush(); 
return 
1
}
//end function 
#--Only get vars from FORM POST; work though register_globals=Off-- 
$safe_get_vars = array('url''path''ow''block'); 
foreach (
$safe_get_vars AS $key)unset($$key); 
foreach (
$safe_get_vars AS $key)$$key = @$HTTP_POST_VARS***91;$key***93;; 
if(
get_magic_quotes_gpc())// Overrides GPC variables 
foreach ($safe_get_vars AS $key)$$key stripslashes($$key); 
#---------------Format vars---------------------------------------- 
if(!@$block)$block=1048576
$block=intval($block); 
if(!
is_int($block))$block=1048576
if(
$block<1)$block=1048576
if(!
$ow)$ow=0
#---------------Form html------------------------------------------ 

<!doctype html public "-//w3c//dtd html 4.0 transitional//en"
<
html
<
head
<
style text/css><!-- 
body,table,input
color:black
background-color:#ffffff; 
font-size:12px
font-style:normal
font-family:verdanatahoma
text-decoration:none

input#t{ 
width:106px
background-color:#D4D0C8; 

//--></style> 
<title>Upload from URL</title></head
<
body
<
form method=post action="<?=basename($_SERVER***91;"REDIRECT_URL"***93;)" 
onsubmit="return(this.url.value!='' && this.path.value!='')"
<
table border=0 align=center
<
tr><th colspan=2>Upload from URL</th></tr
<
tr><td>URL</td><td><input type="text" name="url" size=70 value="<?=$url?>"></td></tr
<
tr><td>Save to</td><td><input type="text" name="path" size=70 value="<?=$path?>"></td></tr
<
tr><td>Block read</td><td><input type="text" name="block" size=10 value="<?=$block?>"bytes (default value1048576 bytes 1024 Kb 1 Mb)</td></tr
<
tr><td>Option</td><td><input type="checkbox" value=1 name="ow"<?=($ow?' checked':''id=o1><label for=o1>Over-write if exist file on destination</label></td></tr
<
tr><td>&nbsp;</td><td
<
input type="submit" value='Submit' id=t
<
input type="reset" id=t
<
input type="button" value="Clean URL" onclick="this.form.url.value=''" id=t
<
input type="button" value="Clean save_to" onclick="this.form.path.value=''" id=t
</
td></tr
</
table
</
form
<? 
#-----------Execute upload from url to file------------------------ 
if($url && $path)upload_url($url$path$ow$block); 
#------------------------------------------------------------------ 

</body
</
html?>
vinhtd1311 viết 13:25 ngày 10/10/2018
Bài viết hay quá, cảm ơn hana nhìu nhìu nhé
Cứ vừa viết vừa giải thích dễ đọc thật
Chứ có mấy người cứ viết xong một đống CODE rồi xuống dười mới nói, mình đã gà + dốt thì chớ cứ làm loạn thêm lên.
He he
huupb viết 13:24 ngày 10/10/2018
em copy code 1 về làm demo nhưng nó báo lỗi này...các anh chỉ giùm em với : Parse error: syntax error, unexpected '*' in F:\VertrigoServ\www\demo\demo_upload1.php on line 9 / thức là nó báo sai cú pháp chỗ
$site_name = $_SERVER***91;'HTTP_HOST'***93;; ..vậy là sao nhỉ..ai giải thích và config giùm em với
redhill viết 13:13 ngày 10/10/2018
Bạn thay các ký tự "***91;" thành dấu mở ngặc vuông và "***93;" thành đóng ngoặc vuông và chạy lại thử. Do post bài trong editor của 4room nên nó bị replace thôi.
huupb viết 13:14 ngày 10/10/2018
Em đã up được rồi nhưng ở đây em thấy không giới hạn kiểu file cho up nên nếu mà áp dụng thì hacker có thể upfile phá hủy lên thì chắc là nguy hiểm lắm

[=========> Bổ sung bài viết <=========]

em mới tìm được một hàm upload cũng hay hay sau đây em xin up share để mọi người sử dụng
/* function upload */
<?php
function uploadfile($url,$file,$alow=1,$maxsize=1024000){
if($alow==1){
$alow_type = array('jpeg', 'jpg','gif', 'png', 'pjpeg', 'bmp', 'x-png' );
} else if ($alow==2) {
$alow_type = array('opd', 'otp', 'sxi', 'sti', 'ppt', 'pot', 'odt', 'ott', 'sxw', 'stw', 'doc', 'docx', 'rtf', 'txt', 'sdw', 'vor', 'sdw', 'vor', 'pdb', 'html', 'htm', 'xml', 'psw', 'uot', 'pdf', 'chm', 'ods', 'ots', 'sxc', 'stc', 'xlsx', 'xls', 'xlt', 'pxl', 'rar', 'zip', 'jpeg', 'jpg','gif', 'png', 'pjpeg', 'bmp', 'x-png' );
} else {
return 0;
}
$_tmpfile=$file['tmp_name'];
$_size=$file['size'];
$_name=str_replace(" ","-",convertPost($file['name']));
$_name=uploadConvertName($_name);
$type=strtolower(getExtent($_name));
$error="";
if($_name!=""){

if(!in_array($type,$alow_type)){
//move_uploaded_file($_tmpfile,$url);
return -1;
}

else if($_size>$maxsize||$_size==0){
return 2;
}else {
$_name="_".date("y-m-d-H-i-s")."_".$_name;
$url=$url.$_name;
@chmod($url,0766);
if(move_uploaded_file($_tmpfile,$url))
{return $_name;}
else {return -1;}
}
}
}
?>
Bài liên quan
0