如何为文本文件中的变量分配值? [英] How to assign a value to a variable from a text file?

查看:103
本文介绍了如何为文本文件中的变量分配值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用脚本从ftp备份文件.代码在下面.

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);

现在,我想从包含文件名列表的 source/files.txt 中获取 $ src 的值.

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

我的.txt文件:

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

我的source/files.txt 包含10个文件名,这些文件名需要作为值分配给变量$ src,我正在使用此脚本

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

我该怎么做??

任何帮助将不胜感激.

谢谢.

推荐答案

好的,您想从.txt文件的每一行中获取文件名.

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; 
    }
?>

回答您的旧问题变体

您可以使用 basename()函数.该手册说:给定一个包含文件或目录路径的字符串,此函数将返回结尾名称部分".

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".

现在,您说我想从source/files.txt中将文件名获取到$ src",因此假设从此获取文件名,即 black.png .可以使用前面提到的basename()函数来实现.

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;
?>

输出

black.png

black.png

http://ideone.com/p2b4sr

这篇关于如何为文本文件中的变量分配值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆