簡體   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