duandu9260 2013-12-30 05:29
浏览 14
已采纳

如何从文本文件中为变量赋值? [关闭]

I am using a script to back up files from ftp. code is below.

include "recurseZip.php";
//Source file or directory to be compressed.
$src='source/images/black.png';
//Destination folder where we create Zip file.
$dst='backup';
$z=new recurseZip();
echo $z->compress($src,$dst);

Now I want to get values to $src from source/files.txt which contains a list of file names.

My .txt file:

index.php.bk-2013-12-02
index.php.bk-2013-12-07
index.php.bk-2013-12-10
index.php.bk-2013-12-20
index.php.bk-2013-12-26
function.php.bk-2013-12-20
function.php.bk-2013-12-23
contact.php.bk-2013-12-23
contact.php.bk-2013-12-30

my source/files.txt contains 10 file names those need to be assigned as values to the variable $src I am using this script http://ramui.com/articles/php-zip-files-and-directory.html

how can I do that.?

any help will be very much appreciated.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongta5747 2013-12-30 05:39
    关注

    Okay, you want to get the file name from each line of the .txt file.

    <?php
        $myFile = "files.txt";
        $lines = file($myFile);
        foreach($lines as $line){
        $file = basename($line);
        echo $file; 
        }
    ?>
    

    Answer to your old question variant

    You can use the basename() function. The manual says, "given a string containing the path to a file or directory, this function will return the trailing name component".

    Now, you said "I want to get file name to $src from source/files.txt", so assuming from this, you are looking to get the file name i.e. black.png. This could be achieved using the basename() function as mentioned before.

    <?php
    $src='source/images/black.png';
    $file = basename($src);
    echo $file;
    ?>
    

    Output

    black.png

    http://ideone.com/p2b4sr

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误