重命名文件&保持原始 - PHP

时间:2014-01-09 12:55:32

标签: php file-rename

我想创建一个副本&使用参数$ file重命名可执行文件并保留原始可执行文件。我制作的片段重命名了原始文件但杀死了原始副本

//extract title from navbar  ex: download.php?file=setup.exe
$file= $_GET['file'];

//assign the futur copy $file to $newfile (not sure if its the best way)
$newfile = '$file';

//assign variable $original to the original executable
$original ='Setup_1.2.exe';

//make a copy of the orignal file so we alway keep original .exe
copy($original, $newfile);

// The exe source is in original.exe
readfile('Setup_1.2.exe');

//rename Setup_1.2.exe to parameter $file
rename ("Setup_1.2.exe", "$file");
你可以请教导我吗? 非常感谢你

2 个答案:

答案 0 :(得分:0)

//extract title from navbar  ex: download.php?file=setup.exe
$file= $_GET['file'];

//assign the futur copy $file to $newfile (not sure if its the best way)
$newfile = '$file';

您没有分配文件 - 您的var $ file值是一个带文件名的字符串。 确切地说,你正在分配字符串[$ file]而不是值,因为你使用单个quots。

//assign variable $original to the original executable
$original ='Setup_1.2.exe';

你只是分配一个字符串!

//make a copy of the orignal file so we alway keep original .exe
copy($original, $newfile);

然而,复制使用字符串值

复制找到的文件
// The exe source is in original.exe
readfile('Setup_1.2.exe');

为什么你在这里阅读文件?

//rename Setup_1.2.exe to parameter $file
rename ("Setup_1.2.exe", "$file");

以下应该做你想做的事:

//read new filename from GET - myabe path is missing - not safe!
$new_file_name = $_GET['file'];

//set original filename - myabe path is missing
$original_file_name ='Setup_1.2.exe';

//make a copy of the orignal file so we alway keep original .exe - myabe path is missing
copy($original_file_name, $new_file_name);

答案 1 :(得分:-1)

//extract title from navbar  ex: download.php?file=setup.exe
$file= $_GET['file'];

//assign the futur copy $file to $newfile (not sure if its the best way)
$newfile = $file;

//assign variable $original to the original executable
$original ='5.exe';

//make a copy of the orignal file so we alway keep original .exe
copy($original, $newfile);

在将变量分配给另一个变量时必须使用双引号,而不需要在此处使用“重命名”方法。