繁体   English   中英

PHP复制并重命名文件

[英]PHP Copy and Rename File

我有一个要移动的文件,我可以移动,但是我似乎无法完全按照需要更改文件名。

$file1 = "/../../../../../../rooms.GENRE.FILENAME/FILENAMEentry.html";
$newfile1 = "/../../../../creative/$path/ $dir entry.html";
copy($file1, $newfile1);

$dir是带有我正在调用的文件名的变量。 这将以FILENAME entry.html返回文件名,我需要删除它们之间的空格。

我尝试了没有空格的地方

$direntry.html ,它将移动并创建文件,但仅将其命名为.html

基本上,我用$dir文件名的名称替换FILENAMEentry.html (大写的部分)

考虑使用串联:

$newfile1 = "/../../../../creative/$path/" . $dir . 'entry.html';

实际上,$ dir在回显时可以工作,但是您有空间,如果将它们一起编写,该词将是$direntry ,这对于解释器来说是不明确的,因此请使用串联。 更改

$newfile1 = "/../../../../creative/$path/ $dir entry.html";

$newfile1 = "/../../../../creative/$path/".$dir."entry.html";

您应该检查这些字符串运算符

这应该工作正常:

$file1 = "/../../../../../../rooms.GENRE.FILENAME/FILENAMEentry.html";
$newfile1 = "/../../../../creative/$path/".$dir."entry.html";

if (!copy($file1, $newfile1)) {
    echo "failed to copy file.";
}

斯蒂芬·克莱

 <?php "{$str1}{$str2}{$str3}"; // one concat = fast $str1. $str2. $str3; // two concats = slow ?> 

使用双引号连接两个以上的字符串,而不是多个'。'。 运营商。 PHP被迫与每个“。”重新连接。 运营商。

资源

您是否尝试过:

$newfile1 = "/../../../../creative/$path/{$dir}entry.html";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM