我使用wordpress中的表单上传文件,我想打印上传的文件的名称但是我得到空的结果。
以下是我的代码
HTML
<form id="myform" action="" method="post" enctype='multipart/form-data'>
<span style="margin-right:20px;">SELECT THE CATEGORY:</span>
<select name="category" id="category" style="background-color:brown;color:#ffffff;">
<option value="" disabled="disabled" selected="selected">Please select a competition category</option>
<option value="Cutie Pie">Cutie Pie</option>
<option value="Chubby">Chubby</option>
<option value="Dimples">Dimples</option>
</select> <br><br>
<input type="file" name="file" style="margin-top:12px;">
<br><br>
<input name="upload" type="submit" value="klk"/>
</form>
<div id="result"></div>
php代码我用来获取和打印文件名
$file=$_FILES['file'];
$name = $file['name'];
echo basename($name);
但是我得到空洞的结果......请帮忙
答案 0 :(得分:0)
首先你需要正确接收文件,然后就可以得到它的名字:
档案up.php
$uploaddir = './upload/'; //give this dir write permissions
$uploadfile = $uploaddir . basename($_FILES['uploadFile']['name']);
if (move_uploaded_file($_FILES['uploadFile']['tmp_name'], $uploadfile)) {
//now you can get the filename
$file = basename($uploadfile);
echo "<div id='result'>File <b>$file</b> is valid, and was successfully uploaded.</div>";
} else {
echo <<< LOB
<form id="myform" action="up.php" method="post" enctype='multipart/form-data'>
<span style="margin-right:20px;">SELECT THE CATEGORY:</span>
<select name="category" id="category" style="background-color:brown;color:#ffffff;">
<option value="" disabled="disabled" selected="selected">Please select a competition category</option>
<option value="Cutie Pie">Cutie Pie</option>
<option value="Chubby">Chubby</option>
<option value="Dimples">Dimples</option>
</select> <br><br>
<input type="file" name="uploadFile" style="margin-top:12px;">
<br><br>
<input name="upload" type="submit" value="klk"/>
</form>
LOB;
}
这只是一个简短的示例,您应该改进此代码,方法是验证文件扩展名是否正确,这样您就不会获得php
个文件上传到您的服务器......