PHP的單檔案上傳
html檔案創建表單
<form action="test7-2.php" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<th align="center" bgcolor="gray" colspan="2">檔案上傳實體</th>
</tr>
<tr>
<td>
<span>*</span>檔案上傳地址:
</td>
<td>
<input type="file" name="file"> (大小《2M為宜)
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="up" value=https://www.cnblogs.com/hleisurely/p/"提交">
php檔案
if (isset($_POST['up'])) {
if ($_FILES['file']['type'] == 'image/jpg' || $_FILES['file']['type'] == 'image/png' || $_FILES['file']['type'] == "image/png") {
if ($_FILES['file']['error'] > 0) {
echo "錯誤!:" . $_FILES['file']['error'];
} else {
$tmp_filename = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$dir = "../test/";
if (is_uploaded_file($tmp_filename)) {
if (move_uploaded_file($tmp_filename, "$dir.$filename")) {
echo "檔案上傳成功!<br>";
echo "檔案大小為:" . ($_FILES['file']['size'] / 1024) . "KB";
} else {
echo "上傳失敗...";
}
}
}
} else {
echo "檔案格式非jpg、png、gif格式!請重新上傳~";
}
PHP的多檔案上傳,
html檔案創建表單
<form action="test7-3.php" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<th align="center" bgcolor="gray" colspan="2">檔案上傳實體</th>
</tr>
<tr>
<td width="150px;">
<span>*</span>檔案上傳地址:(大小《2M為宜)
</td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000">
// 因為是多檔案上傳,這里的name值里邊,需要弄成陣列形式,方便php代碼中回圈遍歷檔案
// multiple是控制多檔案一起上傳,即打開檔案時,同時選中多個需要上傳的檔案
<input type="file" name="file[]" multiple="multiple">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="up" value=https://www.cnblogs.com/hleisurely/p/"提交">
php檔案
if (isset($_POST['up'])) {
for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) {
copy($_FILES['file']['tmp_name'][$i], '../test/' . $_FILES['file']['name'][$i]);
echo "檔案上傳成功!<br>";
echo "檔案存盤在:" . "../test/" . $_FILES['file']['name'][$i] . "<br>";
var_dump($_FILES['file']['name'][$i]);
}
} else {
echo "檔案上傳錯誤,請重新上傳~";
}
關于檔案的格式問題
后期將會整理關于php上傳格式的問題,目前不做詳細處理
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/40936.html
標籤:PHP
上一篇:PHP 框架 Hyperf 實作處理超時未支付訂單和延時佇列
下一篇:php陣列中元素的操作
