dongyuchen0214 2013-08-02 17:07
浏览 27

PHP使用变量名称上载文本文件

So right now I am trying to upload a .txt file and send it to mysql. That part works fine. But I have my code looking for a set file name, like text.txt. The txt file is being ftp'd into a directory, then a button in a php file is pressed and it looks for that file, reads it and sends it to the db. However, the file is going to be ftp'd with different names everday, like date. It will be uploaded like this: test20130802.txt

How do I get my code to look for that date variable? It won't always be today's date either.

Here is part of my current code:

$handle = @fopen("test.txt", "r"); 

$values='';

while (!feof($handle)) 
{
$buffer = fgets($handle, 4096); 
//MYSQL QUERY......
}

Any help is greatly appreciated. Thank you.

  • 写回答

1条回答 默认 最新

  • douyimin1083 2013-08-02 17:41
    关注

    It is fairly straightforward I think.

    First you take the name of the file "test20130802.txt" into a variable. e.g.

    $readTestFile = "test20130802.txt"; //Or however you are taking the file name as input
    

    Hopefully, you are getting a standard file name. (By standard I mean that the first few letters are consistently the same always.)

    In case of standard file name:

    $dateYearChar = array();
    $dateMonthChar = array();
    $dateDateChar = array();
    
    for($i=0;$i<4;$i++)
    $dateYearChar[$i] = $readTestFile[$i+4];
    for($i=0;$i<2;$i++)
    $dateMonthChar[$i] = $readTestFile[$i+8];
    for($i=0;$i<2;$i++)
    $dateDateChar[$i] = $readTestFile[$i+10];
    
    $dateYear = intval($dateYearChar);
    $dateMonth = intval($dateMonthChar);
    $dateDate = intval($dateDateChar);
    

    Edit: And you can do the reverse thing if you want to look for a filename with the specified format.

    e.g.

    $fileName = "test" . $dateYear . $dateMonth . $dateDate . ".txt"
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效