使用数组上传文件

时间:2015-03-05 13:15:49

标签: php arrays file-upload

$file_path = "files/";
$file_temps = array("temp_1_file.txt", "temp_2_file.txt");
$file_names = array("new_file.txt", "temp_1_file.txt");

foreach($file_temps as $file_temp){
    foreach($file_names as $file_name){
        move_uploaded_file($file_temp, $file_path."/".$file_name);
        break;
    }<br>
}
<br>

这只上传一个文件,我不明白。请帮助,提前致谢。

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您在第二个break内使用了foreach。你可以使用类似的东西:

$files = array(
  "temp_1_file.txt" => "files/new_file.txt",
  "temp_2_file.txt" => "files/temp_1_file.txt"
);

foreach($files as $fileTemp => $fileName){
    move_uploaded_file($fileTemp, $fileName);
}

如果您想使用2个数组:

$filePath = 'files/';
$fileTemps = array("temp_1_file.txt", "temp_2_file.txt");
$fileNames = array("new_file.txt", "temp_1_file.txt");

foreach($fileTemps as $key => $fileTemp){
    move_uploaded_file($fileTemp, $filePath . $fileNames[$key]);
}