php 上传图片时获取图片名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19510641/
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
get the name of image when i upload it
提问by Anton
When I upload image :
当我上传图片时:
<h3 id="txt_i2">Img</h3><input type="file" accept="image/jpeg, image/jpg" name="picture" size="chars">
How to get the name of the uploaded img using php ? I don't mean "picture". For example if the img is called cow.jpg the get that "cow"
如何使用php获取上传的img的名称?我的意思不是“图片”。例如,如果 img 被称为 cow.jpg,则获取“cow”
回答by Ram Sharma
Anton, you can use the code below to get the image or any file name without extension.
安东,您可以使用下面的代码来获取图像或任何没有扩展名的文件名。
$filename = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);
回答by Anthony
$_FILES['userfile']['name']
The original name of the file on the client machine.
$_FILES['userfile']['name']
客户端计算机上文件的原始名称。
So using the above, if you want the actual file name cow.jpg
, it is stored in
所以使用上面的,如果你想要实际的文件名cow.jpg
,它存储在
$_FILES['picture']['name'];
If you want the name of the file without the extension, you can extrapolate that from the filename provided with pathinfo()
by setting the PATHINFO_FILENAME
flag, which would return cow
. If the image is named cow.moo.jpg
it will return cow.moo
:
如果你想要不带扩展名的文件名,你可以pathinfo()
通过设置PATHINFO_FILENAME
标志从提供的文件名中推断出来,这将返回cow
. 如果图像被命名cow.moo.jpg
,它将返回cow.moo
:
$picture_filename = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);
回答by Prateek
Use this
用这个
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Uploade Script
上传脚本
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
?>
This give you file name, file type and file size.
这将为您提供文件名、文件类型和文件大小。
回答by BlackScorp
回答by Allen Chak
$filename = explode(".", $_FILES['picture']['name']); //split by '.'
array_pop($filename); //remove the last segment
$filename = implode(".", $filename); //concat it by '.'