使用相同的功能php上传两次时的问题

时间:2014-05-05 22:52:25

标签: php unset

我的网站文件上传脚本出现问题。我复制了一个简单的上传脚本,我正在设置它以便我可以上传多个文件。我必须首先为一个文件创建一个表单,然后为下一个表单创建一个表单副本。 (我首先使用表单中的“multiple”标签设置页面,但后来我发现工作中的IE是版本8,没有闪存。所以我必须这样做)

脚本上传正常。问题是第二个文件名为file1.jpgfile2.jpg,第三个文件名为file1.jpgfile2.jpgfile3.jpg,依此类推。

这是我的表单,然后是表单#2文件#2的代码,然后通过相同的表单:

<form method="post" enctype="multipart/form-data" action="rediger.php?choice=addpicturetoproc">
              <input type="hidden" name="uploadpath" value="<? echo "Prosedyrer/$hovedkategori/$underkategori/$navn/$mappe_navn/" ?>">
              Bildefil: <input type="file" name="fileup" value=""><br>
              <input type="submit" name="submit" value="Upload">
              </form>
              <br>

              <?
}


elseif ($choice=="addpicturetoproc")
{

$max_size = 2000;          // maximum file size, in KiloBytes
$alwidth = 1900;            // maximum allowed width, in pixels
$alheight = 1800;           // maximum allowed height, in pixels
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        // allowed extensions

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       // gets the file name
  $sepext = explode('.', strtolower($_FILES['fileup']['name']));
  $type = end($sepext);       // gets extension
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
  $err = '';         // to store the errors

  // Checks if the file has allowed type, size, width and height (for images)
  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;

  // If no errors, upload the image, else, output the errors
  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
      echo '<h1>Filen: <b>'. basename( $_FILES['fileup']['name']). '</b> ble lastet opp!</h1>';

    }
    else echo '<b>Feil ved opplastning.</b>';
  }
  else echo $err;
}
    ?><p>Do you need another upload?</p>
<form method="post" enctype="multipart/form-data" action="rediger.php?choice=addpicturetoproc">
              <input type="hidden" name="uploadpath" value="<? echo $uploadpath; ?>">
              Bildefil: <input type="file" name="fileup" value="Sett inn bildefilen her"><br>
              <input type="submit" name="submit" value="Last opp bildefil">
              </form>
              <br>
              <form method="post" enctype="multipart/form-data" action="rediger.php?choice=menu">
               <input type="submit" name="submit" value="Nei, jeg er ferdig">
              </form>

我尝试过使用unset($ fileup);在第二次运行之前,但仍然是filename1.jpgfilename2.jpg。

任何明显的解决方案?

1 个答案:

答案 0 :(得分:0)

我相信这是因为您要附加到$ uploadpath

$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);

因此,第一个文件将是file1.jpg,然后第二个文件将是file1.jpgfile2.jpg,依此类推。

我建议将基本上传路径设置为其他变量并尝试类似

$uploadpath = $baseuploadpath . basename( $_FILES['fileup']['name']);