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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程