简体   繁体   English

PHP复制并重命名文件

[英]PHP Copy and Rename File

I have a file I'm attempting to move and Im able to do so, however I can't seem to change the filename exactly how I need it. 我有一个要移动的文件,我可以移动,但是我似乎无法完全按照需要更改文件名。

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

$dir is the variable with the name of the file I'm calling. $dir是带有我正在调用的文件名的变量。 this returns the file name as FILENAME entry.html and I need the space between them removed. 这将以FILENAME entry.html返回文件名,我需要删除它们之间的空格。

I've tried it without the space as in 我尝试了没有空格的地方

$direntry.html and that moves and creates the file but just names it .html $direntry.html ,它将移动并创建文件,但仅将其命名为.html

Basically I'm replacing where it says FILENAMEentry.html (the capitalized portion) with the name of the filename in $dir 基本上,我用$dir文件名的名称替换FILENAMEentry.html (大写的部分)

考虑使用串联:

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

Actually $dir works while echoing but you have space and if you write them together the word would be $direntry which will be ambiguous for the interpreter so use concatenation. 实际上,$ dir在回显时可以工作,但是您有空间,如果将它们一起编写,该词将是$direntry ,这对于解释器来说是不明确的,因此请使用串联。 change 更改

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

to

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

You should check out these string operators . 您应该检查这些字符串运算符

This should work fine: 这应该工作正常:

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

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

Stephen Clay 斯蒂芬·克莱

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

Use double quotes to concat more than two strings instead of multiple '.' 使用双引号连接两个以上的字符串,而不是多个'。'。 operators. 运营商。 PHP is forced to re-concatenate with every '.' PHP被迫与每个“。”重新连接。 operator. 运营商。

Source 资源

您是否尝试过:

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

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

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