寫一個 PHP 網頁上傳範例
請先關閉 SELinux
# yum install php
# vi /etc/php.ini
upload_max_filesize = 1000M
post_max_size = 1000M
max_execution_time = 36000
# md /var/www/html/upload
# cd /var/www/html
# chmod -R 777 /var/www/html/upload
# chown apache:apache /var/www/html/upload
# cd /var/www/html/upload
# firewall-cmd --add-service=http --permanent
# firewall-cmd --reload
# vi file.php
程式碼:
-------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>檔案上傳</title>
</head>
<body>
<form action="file.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<table>
<tr>
<td align="left">
<?php
$fileNum=1;
for($i=0;$i<$fileNum;$i++){?>
<p>附加檔案<?php echo $i+1;?>:
<input type="file" name="file[]" />
</p>
<?php }?>
</td>
</tr>
<tr>
<td><input type='submit' name="Upload" value="確定" /></td>
</tr>
</table>
<p>建議欲上傳檔案名稱為英文或數字</p>
</form>
</body>
</html>
<?php
if(isset($_FILES["file"])){
//處理多個檔案
for($i=0;$i<$fileNum;$i++){
$name=mb_convert_encoding($_FILES["file"]["name"][$i],"big5","utf8");
$tmp=$_FILES["file"]["tmp_name"][$i];
if(!empty($name)){ //上傳檔案
copy($tmp,$name);
echo "File was successfully uploaded";
}
}
}
?>
-------------------------------------------------------------------------------
# systemctl start httpd.service
存檔後,啟動 apache 服務,就可以連線到此 IP,做上傳檔案測試
檔案上傳預設路徑會存放在 /var/www/html/upload 該目錄底下
留言