2014-05-05 16 views
0

我遇到了用於我的網站的文件上傳腳本的問題。我已經複製了一個簡單的上傳腳本,我正在設置它,以便我可以上傳多個文件。我必須首先爲一個文件創建一個表單,然後爲下一個表單創建一個表單副本,然後創建一個副本。 (我首先用頁面上的「multiple」標籤設置頁面,但是後來我發現工作的IE版本是8版本,並且沒有flash,所以我必須這樣做)使用相同函數上傳兩次時出現的問題php

腳本上傳好。問題是第二個文件叫做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> 

我一直在使用未設置($ fileup)審判;在第二次運行之前,但仍然是filename1.jpgfilename2.jpg。

任何明顯的解決方案?

+0

爲什麼'$ uploadpath'變種正在以第二形式使用? – lsouza

回答

0

我相信這是因爲要追加到$ uploadpath

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

因此,第一個文件將file1.jpg然後第二個文件將file1.jpgfile2.jpg等。

我建議的基礎上傳路徑設置爲不同的變量,並試圖像

$uploadpath = $baseuploadpath . basename($_FILES['fileup']['name']); 
相關問題