How to assign a value to a variable from a text file? [closed]

泄露秘密 提交于 2019-12-12 04:38:31

问题


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:


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



来源:https://stackoverflow.com/questions/20833843/how-to-assign-a-value-to-a-variable-from-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!