简体   繁体   中英

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. this returns the file name as FILENAME entry.html and I need the space between them removed.

I've tried it without the space as in

$direntry.html and that moves and creates the file but just names it .html

Basically I'm replacing where it says FILENAMEentry.html (the capitalized portion) with the name of the filename in $dir

考虑使用串联:

$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. 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 '.' operator.

Source

您是否尝试过:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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