简体   繁体   English

在不同的目录中获取未知的文件名,php

[英]get a unknown file name in different dir, php

Here is my directory structure,这是我的目录结构,

C:\xampp\htdocs\..

C:\download\20150923abc.xls  //abc is a random value

how can I attach the file 20150923abc.xls in php?如何在 php 中附加文件20150923abc.xls

Also, how to change the filename after I got it?另外,我得到文件后如何更改文件名?

Thanks.谢谢。

Use the glob ability to find references all files of type .xls and then you can use the file name references as you wish.使用 glob 功能查找所有 .xls 类型文件的引用,然后您可以根据需要使用文件名引用。 This sidesteps the issue of you not knowing the specific file name.这回避了您不知道特定文件名的问题。

$files = glob("c:/download/*.xls");

This will produce an array of all .xls files with their full filepath.这将生成所有 .xls 文件及其完整文件路径的数组。 If you wish to rename or attach these files then you can do this using the glob reference:如果你想重命名或附加这些文件,那么你可以使用glob引用来做到这一点:

rename($files[0], "c:/download/somenewname.xls");

etc. Read more at:等阅读更多信息:

PHP Glob Function PHP 全局函数

EDIT:编辑:

From Comment below:来自下面的评论:

foreach (glob( $old_folder."*.xls") as $filename) 
{
     $names = explode('/', $filename); 
     $just_file_name = end($names); 
     echo $just_file_name . "----\n"; 
     $new_folder = dirname(FILE)."\\prm\\att\\";
      //rename_win($old_folder, $new_folder); 
     rename($filename, $new_folder.$just_file_name); <== this line changed.
}
unset($filename);

To fix the above code in your comment, you need to change the incorrect variables referenced (there was no array $files[0] ) to the ones used in the foreach loop.要修复注释中的上述代码,您需要将引用的不正确变量(没有数组$files[0] )更改为 foreach 循环中使用的变量。

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

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