php 文件上传php,只获取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28807449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:04:51  来源:igfitidea点击:

File upload php, get only file name

phpfile-upload

提问by Michael Lau

Is it possible to get the filename of a file, without a complete upload

是否可以在没有完整上传的情况下获取文件的文件名

Meaning after the user chose a file, dont upload that file, just get the filename and save to database?

意思是用户选择文件后,不要上传该文件,只需获取文件名并保存到数据库?

回答by priya786

yes it is possible you can use the code as given below

是的,您可以使用下面给出的代码

$filename=$_FILES['nameofyourfileinput']['name'];
echo $filename;

you can echo the $filename;

你可以回显 $filename;

OR You can use jquery to get this value like

或者你可以使用 jquery 来获取这个值

$('#inputid').change(function(){
 var value =$(this).val();
 alert(value);
})

回答by user3386779

ya it is possible.You can also do this before uploading the file basename() is enough for extracting name.

是的,这是可能的。您也可以在上传文件 basename() 足以提取名称之前执行此操作。

                $next=$pfet['f_name']; //fetched file from database 

                $next1 = basename($next); 

回答by jmoreno

The accepted answer doesn't prevent the file upload, it simply provides a way to get the file name independent of the file contents.

接受的答案不会阻止文件上传,它只是提供了一种获取独立于文件内容的文件名的方法。

Preventing file upload, is best looked at the other way: what enables uploading a file. The answer to that is the enctype attribute on the form tag (multipart/form-data).

防止文件上传,最好以另一种方式看待:什么可以上传文件。 答案是表单标签 (multipart/form-data) 上的 enctype 属性。

回答by Carlo Galliano

HTML:

HTML:

<form action="upload.php" method="post" enctype="application/x-www-form-urlencoded">
                                        Select:
                                        <input name="upload[]" type="file" multiple="multiple" />
                                        <input type="submit" value="Update" name="submit">
                                    </form>

PHP:

PHP:

$total = count($_POST['upload']);

// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {
    $fileName = $_POST['upload'][$i];   
}