在没有数组的PHP上传两个文件

时间:2016-03-01 07:15:43

标签: php

我在尝试重命名文件之前上传它们而不使用数组,但我面临的问题是该文件的名称不符合我在这里提到的是我的代码

$path = "mande/";
  $file_name = $_FILES["file"]["name"];
  $file2 = $_FILES['file1']["name"];
  $time = date('is'); // Minutes and seconds
  $newname = abc . '-' . $time; 
  $newname2 = 'letter' . 'abc' . '-' . $time; 
  $ext = strrchr($file_name, ".");
  $filename = $newname . $ext;
  $filename2 = 'letter' . $newname . $ext;
  $path = $path . $newname . $ext;
  $path2 = $path . $newname2 . $ext;

使用插入查询后,我试图通过使用这些语句来移动这两个

move_uploaded_file($_FILES["file"]["tmp_name"], $path);
move_uploaded_file($_FILES["file1"]["tmp_name"], $path2);

上传的第一个文件根据我的需要获取确切名称,但第二个文件存在一些问题,例如 第一个文件名:abc-1234.pdf 第二个文件名:abc-1234.pdfletterabc-1234.pdf INSTEAD OF letterabc-1234.pdf

2 个答案:

答案 0 :(得分:1)

有两个问题:

1)当您一次又一次地使用相同的变量时,您的路径会混淆。设置路径$storage_path并将其用作基本路径

2)将$filename2 = 'letter' . $newname . $ext;更改为$filename2 = 'letter' . $newname2 . $ext;

$storage_path = "mande/";  // FIRST PROBLEM WAS HERE
$file_name = $_FILES["file"]["name"];
$file2 = $_FILES['file1']["name"];
$time = date('is'); // Minutes and seconds
$newname = abc . '-' . $time; 
$newname2 = 'letter' . 'abc' . '-' . $time; 
$ext = strrchr($file_name, ".");
$filename = $newname . $ext;
$filename2 = 'letter' . $newname2 . $ext; // SECOND PROBLEM WAS HERE
$path = $storage_path . $newname . $ext; // FIRST PROBLEM WAS HERE
$path2 = $storage_path . $newname2 . $ext; // FIRST PROBLEM WAS HERE

答案 1 :(得分:0)

$abspath = "mande/";
$file_name = $_FILES["file"]["name"];
$file2 = $_FILES['file1']["name"];
$time = date('is'); // Minutes and seconds
$newname = abc . '-' . $time; 
$newname2 = 'letter' . 'abc' . '-' . $time; 
$ext = strrchr($file_name, ".");
$filename = $newname . $ext;
$filename2 = 'letter' . $newname . $ext;
$path = $abspath . $newname . $ext;
$path2 = $abspath . $newname2 . $ext;