简体   繁体   English

如何从php中的表单获取文件名? $_FILES[“filename”][“name”] 似乎不起作用

[英]How do I get the filename from a form in php? $_FILES[“filename”][“name”] doesn't seem to work

I have a form below which I'm trying to use to extract some details into a mySQL database.我在下面有一个表单,我试图用它来将一些详细信息提取到 mySQL 数据库中。 However, upon trying to retrieve the file in the form, everything always comes up as null.但是,在尝试检索表单中的文件时,所有内容始终为空。 I'm not so sure why this is happening, so any help would be greatly appreciated!我不太确定为什么会这样,所以任何帮助将不胜感激!

 <form method="post" action="myfile.php" enctype="multipart/form-data"> <table class="form-table"> <tr> <td><b>Document (Optional)</b></td> <td> <select name="d" class="form-control input input-sm"> <option value="companyDoc">Company Registration Form</option> <option value="businessCard">Business Card</option> </select> </td> </tr> <tr> <td><b>Upload Document (Optional)</b></td> <td><input type="file" class="form-control input input-sm" id="ud" name="ud"></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit" name="submit" class="btn btn-primary pull-right"></td> </tr> </table> </form>

Now my php snippet is below, where I try and call the results of the form, in the same file as the html.现在我的 php 片段在下面,我尝试在与 html 相同的文件中调用表单的结果。

 if(isset($_POST['submit'])) { $target_dir = "../../public_html/clientarea/docs/"; $fileName = basename($_FILES["ud"]["name"]); $target_file = $target_dir . $fileName; if($fileName!="") { $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); $errorMsg = ""; if (file_exists($target_file)) { $errorMsg = "Some file already exists. "; $uploadOk = 0; } else { if (move_uploaded_file($_FILES["ud"]["tmp_name"], $target_file)) { $errorMsg = "The file ". basename( $_FILES["ud"]["name"]). " has been uploaded. "; } else { $errorMsg = "There was an error uploading your some of the file."; } } } }

Tested your HTML and PHP code.测试了您的 HTML 和 PHP 代码。 Worked just fine with $_FILES['ud']['name'] to get the filename.$_FILES['ud']['name']一起工作得很好来获取文件名。

You can try to print_r($_FILES) or maybe print_r($_FILES['ud']) to find out the values of your form that you sent through the POST method.您可以尝试使用print_r($_FILES)print_r($_FILES['ud'])找出通过 POST 方法发送的表单的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM