天天看點

php 實作多檔案上傳程式代碼

php??浠朵?浼?涓?澶???浠朵?浼??跺??娌℃??澶?澶х???哄??锛?澶???浠朵?浼???????浠???琛ㄥ?????規???扮?褰㈠?锛????峰???╃??oreach????灏卞??浠ュ???闆???浠朵?浼?浜?锛??ㄦ??澶???浠朵?浼???瑕???s??涓?涓??ㄦ??澧???澶???浠朵?浼?妗?锛????峰??hp澶????跺氨????涓?涓??扮?灏辨??浜???

??绠?????瀹?渚?濡?涓?

?浠g??濡?涓? 澶??朵唬??

<form action="" method="post" enctype="multipart/form-data">

<p>Pictures:

<input type="file" name="pictures[]" />

<input type="file" name="pictures[]" />

<input type="file" name="pictures[]" />

<input type="submit" value="Send" />

</p>

</form>

?????? <?php

foreach?($_FILES["pictures"]["error"] as $key => $error) {

??????? if ($error == UPLOAD_ERR_OK) {

??????????? $tmp_name = $_FILES["pictures"]["tmp_name"][$key];

??????????? $name = $_FILES["pictures"]["name"][$key];

??????????? move_uploaded_file($tmp_name, "data/$name");

??????? }

}

?>

?

涓??㈠??浜??跺????????渚?瀛?

渚?1

?浠g??濡?涓? 澶??朵唬??

<?

//filename:multi_upload.php

if($ifupload)

{

$path=AddSlashes(dirname($PATH_TRANSLATED))."upload";

for($i=1;$i<=8;$i++)

{

$files="afile".$i;

if(${$files}!="none")

{

if(copy(${$files},$path.${$files."_name"}))

{

}

}

}

print "<b>You have uploaded files successfully</b><br>";

print "<a href="multi_upload.php" target="_blank" rel="external nofollow" >Return</a>";

exit;

}

?>

<html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">

<title>澶?涓???浠朵?浼?</title>

<style type="text/css">

<!--

BODY

{

PADDING-RIGHT: 0px;

MARGIN-TOP: 0px;

PADDING-LEFT: 0px;

FONT-SIZE: 8px;

MARGIN-LEFT: 0px;

CURSOR: default;

COLOR: black;

MARGIN-RIGHT: 0px;

PADDING-TOP: 0px;

FONT-FAMILY: Arial;

BACKGROUND-COLOR: transparent;

TEXT-ALIGN: center

}

.TxtInput

{

FONT-SIZE: 8pt;

WIDTH: 100%;

CURSOR: default;

COLOR: black;

FONT-FAMILY: Arial;

HEIGHT: 21px;

BACKGROUND-COLOR: white;

TEXT-ALIGN: left

}

.FieldLabel

{

FONT-WEIGHT: normal;

FONT-SIZE: 9pt;

WIDTH: 100%;

COLOR: black;

FONT-FAMILY: Arial;

BACKGROUND-COLOR: transparent;

TEXT-ALIGN: left

}

.HeadBtn

{

BORDER-RIGHT: black 1px solid;

BORDER-TOP: white 1px solid;

FONT-SIZE: 8pt;

OVERFLOW: hidden;

BORDER-LEFT: white 1px solid;

WIDTH: 70px;

COLOR: black;

BORDER-BOTTOM: black 1px solid;

FONT-FAMILY: Arial;

HEIGHT: 21px;

BACKGROUND-COLOR: #8e8dcd;

TEXT-ALIGN: center

}

.TransEx

{

BORDER-RIGHT: black 1px solid;

PADDING-RIGHT: 8px;

BORDER-TOP: white 1px solid;

PADDING-LEFT: 8px;

FONT-SIZE: 8pt;

PADDING-BOTTOM: 3px;

BORDER-LEFT: white 1px solid;

WIDTH: 720px;

PADDING-TOP: 3px;

BORDER-BOTTOM: black 1px solid;

FONT-FAMILY: Arial;

BACKGROUND-COLOR: #c0c0c0;

TEXT-ALIGN: center

}

-->

</style>

<script language="javascript">

function window.onload()

{

document.forms[0].btnOk.慰nclick=btn_ok;

}

function btn_ok()

{

for(var i=1;i<=8;i++)

{

if(eval("document.forms[0].afile"+i+".value!=''"))

document.forms[0].submit();

return true;

}

alert("None of file have been?selected");

return false;

}

</script>

</head>

<body>

<form method="post" action="multi_upload.php" name="frmUpload" enctype="multipart/form-data" >

<table id="divContainer" style="HEIGHT: 100%; WIDTH: 380" border="0">

<tr height="35"><td align="right" valign="bottom">澶???浠朵?浼? </td></tr>

<tr><td align="center" valign="top">

<table class="Transex" border="0" cellspacing="0" cellpadding="0" style="WIDTH: 360px">

<tr style="HEIGHT: 10px" >

<td style="WIDTH: 5px"></td>

<td colspan="2"></td>?

<td style="WIDTH: 5px"></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="1" name="afile1" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="2" name="afile2" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="3" name="afile3" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="4" name="afile4" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="5" name="afile5" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="6" name="afile6" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="7" name="afile7" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr>

<td></td>

<td nowrap><label class="FieldLabel"> ??浠?</label></td>

<td><input type="file" class="TxtInput" tabindex="8" name="afile8" style="WIDTH: 282px"></td>

<td></td>

</tr>

<tr style="HEIGHT: 5px">

<td style="WIDTH: 5px">

<td style="WIDTH: 350px" colspan="2"><hr width="100%"></td>?

<td style="WIDTH: 5px"></td>

</tr>

<tr>

<td></td>

<td colspan="2" align="left">

<button tabindex="5" class="headbtn" align="center" name="btnOk" id="btnOk" accesskey="O">纭?瀹?(<ins>O</ins>)</button>

<input type="hidden" name="ifupload" value=1>

<button tabindex="5" class="headbtn" align="center" name="btnCancel" id="btnCancel" accesskey="C" 慰nclick="window.close();">??娑?(<ins>C</ins>)</button></td>?

<td></td>

</tr>

<tr style="HEIGHT: 5px">

<td style="WIDTH: 5px">

<td style="WIDTH: 350px" colspan="2"></td>?

<td style="WIDTH: 5px"></td>

</tr>

</table>

</td>

</tr>

</table>

</form>

</body>

</html>

濡?????浠?瑕??ㄦ??涓?纭?瀹??х??澶???浠朵?浼???涔?瀹??頒??????瀹?渚?

??浠朵?浼?浠g??

?浠g??濡?涓? 澶??朵唬??

view plaincopy to clipboardprint??

<!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=gb2312" />?

<title>??妗d?浼?</title>?

</head>?

<body>?

<script language="javascript"><!--?

?ㄦ??娣誨????浠堕???╂?т歡-->?

function AddRow()?

{?

var eNewRow = tblData.insertRow();?

for (var i=0;i<1;i++)?

{?

var eNewCell = eNewRow.insertCell();?

eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";?

}?

}?

// --></script>?

<form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >?

<table id="tblData" width="400" border="0">?

<!-- 灏?涓?浼???浠跺?椤葷??ost???規???enctype="multipart/form-data" -->?

<!-- 灏???椤電??缃???浼?缁?uploadfile.php-->?

<input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />?

<tr><td>??浠朵?浼???琛??

<input type="button" name="addfile" 慰nclick="AddRow()" value="娣誨????琛? /></td></tr>?

<!-- filelist[]蹇?椤繪??涓?涓??扮?-->?

<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>?

</table>?

<input type="submit" name="submitfile" value="??浜ゆ??浠? />?

</form>?

</body>?

</html>?

<!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=gb2312" />?

<title>??妗d?浼?</title>?

</head>?

<body>?

<script language="javascript"><!--?

?ㄦ??娣誨????浠堕???╂?т歡-->?

function AddRow()?

{?

var eNewRow = tblData.insertRow();?

for (var i=0;i<1;i++)?

{?

var eNewCell = eNewRow.insertCell();?

eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";?

}?

}?

// --></script>?

<form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >?

<table id="tblData" width="400" border="0">?

<!-- 灏?涓?浼???浠跺?椤葷??ost???規???enctype="multipart/form-data" -->?

<!-- 灏???椤電??缃???浼?缁?uploadfile.php-->?

<input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />?

<tr><td>??浠朵?浼???琛??

<input type="button" name="addfile" 慰nclick="AddRow()" value="娣誨????琛? /></td></tr>?

<!-- filelist[]蹇?椤繪??涓?涓??扮?-->?

<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>?

</table>?

<input type="submit" name="submitfile" value="??浜ゆ??浠? />?

</form>?

</body>?

</html>?

??浜ゆ??浠朵唬???

view plaincopy to clipboardprint??

<!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=gb2312" />?

<title>??浠朵?浼?缁???</title>?

</head>?

<body>?

<?php?

if ($_POST["submitfile"]!="")?

{?

$Path="./".date('Ym')."/";?

if (!is_dir($Path))//??寤鴻礬寰??

{ mkdir($Path); }?

echo "<div>";?

for ($i=0;$i<count($filelist);$i++)?

{ //$_FILES["filelist"]["size"][$i]??????椤哄?涓???浠ュ??锛???涓?ileist??涓?涓?浜?缁存?扮??

if ($_FILES["filelist"]["size"][$i]!=0)?

{?

$File=$Path.date('Ymdhm')."_".$_FILES["filelist"]["name"][$i];?

if (move_uploaded_file($_FILES["filelist"]["tmp_name"][$i],$File))?

{ echo "??浠朵?浼????? ??浠剁被??:".$_FILES["filelist"]["type"][$i]." "."??浠跺??:"?

.$_FILES["filelist"]["name"][$i]."<br>"; }?

else?

{ echo "??浠跺??:".$_FILES["filelist"]["name"][$i]."涓?浼?澶辮觸</br>"; }?

}?

}?

echo "</div><br><a href="$postadd" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" href="$postadd" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >杩???</a></div>";?

}?

?>?

</body>?

</html>

?????璇?淇℃??璇存??

浠? PHP 4.2.0 寮?濮?锛?PHP 灏?????浠朵俊???扮?涓?璧瘋???涓?涓?瀵瑰?????璇?浠g????璇ヤ唬????浠ュ?ㄦ??浠朵?浼??剁????????浠舵?扮?涓??? error 瀛?娈典腑琚??懼?幫?涔?灏辨?? $_FILES['userfile']['error']???

UPLOAD_ERR_OK

?跺?間負 0锛?娌℃????璇?????锛???浠朵?浼????????

UPLOAD_ERR_INI_SIZE

?跺?間負 1锛?涓?浼?????浠惰?杩?浜? php.ini 涓? upload_max_filesize ??椤歸???剁???箋???

UPLOAD_ERR_FORM_SIZE

?跺?間負 2锛?涓?浼???浠剁??澶у?瓒?杩?浜? HTML 琛ㄥ??涓? MAX_FILE_SIZE ??椤規??瀹????箋?? UPLOAD_ERR_PARTIAL

?跺?間負 3锛???浠跺?????ㄥ??琚?涓?浼????

UPLOAD_ERR_NO_FILE

?跺?間負 4锛?娌℃????浠惰?涓?浼????

UPLOAD_ERR_NO_TMP_DIR

?跺?間負 6锛??句??頒複?舵??浠跺す??PHP 4.3.10 ?? PHP 5.0.3 寮?杩????

UPLOAD_ERR_CANT_WRITE

?跺?間負 7锛???浠跺???ュけ璐ャ??PHP 5.1.0 寮?杩????

娉ㄦ??: 浠ヤ??煎??PHP 4.3.0 涔???????浜? PHP 甯擱????