2014-05-24 165 views
0

我正在嘗試上傳多個文件到多個文件夾。不幸的是,收效甚微。多個文件上傳到多個文件夾 - PHP

任何幫助,將不勝感激

我有兩個表單字段:

<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>" /> 
<input name="menu" type="file" class="input_event noBorder" title="Upload Menu" /> 


<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>" /> 
<input name="img" type="file" class="input_event noBorder" title="Upload Img" /> 

我想用一個上傳圖像和其他上傳PDF格式的和/或Word文檔。 我還想將這兩個文件保存到相應的文件夾(/ imgs和/ docs)中。

有些事情我已經嘗試

  • 都試圖擁有其中的兩個,每個指向正確的路徑
  • 試圖多類來處理
  • 添加陣列支架固定到每個文件輸入名稱字段(例如:name="menu[]"

我想我需要改變類中的某些東西;然而,我正在學習PHP,這個課程是從3本書,數十個谷歌搜索以及我在這裏找到的一些帖子拼湊在一起的。

正是我需要改變的地方仍然遠遠超出了我。

的對網頁PHP代碼相關部分:

$max = 400000; 
if (isset($_POST['submit'])) { //MAIN IF STATEMENT 

$destination = './uploads/menus_up/'; 
try { 
$upload = new Upload_File($destination); 
$upload->move(); 
$result = $upload->getMessages(); 
} catch (Exception $e) { 
echo $e->getMessage(); 
} 

上傳類:

class Upload_File { 

    protected $_uploaded = array(); 
    protected $_destination; 
    protected $_max = 400000; 
    protected $_messages = array(); 
    protected $_permitted = array('image/gif', 
           'image/jpeg', 
           'image/pjpeg', 
           'image/png'); 
    protected $_renamed = false; 

    public function __construct($path) { 
    if (!is_dir($path) || !is_writable($path)) { 
     throw new Exception("$path must be a valid, writable directory."); 
    } 
    $this->_destination = $path; 
    $this->_uploaded = $_FILES; 
    } 

    public function getMaxSize() { 
    return number_format($this->_max/1024, 1) . 'kB'; 
    } 

    public function setMaxSize($num) { 
    if (!is_numeric($num)) { 
     throw new Exception("Maximum size must be a number."); 
    } 
    $this->_max = (int) $num; 
    } 

    public function move($overwrite = false) { 
    $field = current($this->_uploaded); 
    if (is_array($field['name'])) { 
     foreach ($field['name'] as $number => $filename) { 
     // process multiple upload 
     $this->_renamed = false; 
     $this->processFile($filename, $field['error'][$number], $field['size'][$number], $field['type'][$number], $field['tmp_name'][$number], $overwrite); 
     } 
    } else { 
     $this->processFile($field['name'], $field['error'], $field['size'], $field['type'], $field['tmp_name'], $overwrite); 
    } 
    } 

    public function getMessages() { 
    return $this->_messages; 
    } 

    protected function checkError($filename, $error) { 
    switch ($error) { 
     case 0: 
     return true; 
     case 1: 
     case 2: 
      $this->_messages[] = "$filename exceeds maximum size: " . $this->getMaxSize(); 
     return true; 
     case 3: 
     $this->_messages[] = "Error uploading $filename. Please try again."; 
     return false; 
     case 4: 
     $this->_messages[] = 'No file selected.'; 
     return false; 
     default: 
     $this->_messages[] = "System error uploading $filename. Contact webmaster."; 
     return false; 
    } 
    } 

    protected function checkSize($filename, $size) { 
    if ($size == 0) { 
     return false; 
    } elseif ($size > $this->_max) { 
     $this->_messages[] = "$filename exceeds maximum size: " . $this->getMaxSize(); 
     return false; 
    } else { 
     return true; 
    } 
    } 

    protected function checkType($filename, $type) { 
    if (empty($type)) { 
     return false; 
    } elseif (!in_array($type, $this->_permitted)) { 
     $this->_messages[] = "$filename is not a permitted type of file."; 
     return false; 
    } else { 
     return true; 
    } 
    } 

    public function addPermittedTypes($types) { 
    $types = (array) $types; 
     $this->isValidMime($types); 
    $this->_permitted = array_merge($this->_permitted, $types); 
    } 

    protected function isValidMime($types) { 
     $alsoValid = array('image/tiff', 
          'application/pdf', 
          'application/msword'); 
     $valid = array_merge($this->_permitted, $alsoValid); 
    foreach ($types as $type) { 
     if (!in_array($type, $valid)) { 
     throw new Exception("$type is not a permitted MIME type"); 
     } 
    } 
    } 

    protected function checkName($name, $overwrite) { 
    $nospaces = str_replace(' ', '_', $name); 
    if ($nospaces != $name) { 
     $this->_renamed = true; 
    } 
    if (!$overwrite) { 
     $existing = scandir($this->_destination); 
     if (in_array($nospaces, $existing)) { 
     $dot = strrpos($nospaces, '.'); 
     if ($dot) { 
      $base = substr($nospaces, 0, $dot); 
      $extension = substr($nospaces, $dot); 
     } else { 
      $base = $nospaces; 
      $extension = ''; 
     } 
     $i = 1; 
     do { 
      $nospaces = $base . '_' . $i++ . $extension; 
     } while (in_array($nospaces, $existing)); 
     $this->_renamed = true; 
     } 
    } 
    return $nospaces; 
    } 

    protected function processFile($filename, $error, $size, $type, $tmp_name, $overwrite) { 
    $OK = $this->checkError($filename, $error); 
    if ($OK) { 
     $sizeOK = $this->checkSize($filename, $size); 
     $typeOK = $this->checkType($filename, $type); 
     if ($sizeOK && $typeOK) { 
     $name = $this->checkName($filename, $overwrite); 
     $success = move_uploaded_file($tmp_name, $this->_destination . $name); 
     if ($success) { 
      $message = "$filename uploaded successfully"; 
      if ($this->_renamed) { 
       $message .= " and renamed $name"; 
      } 
      $this->_messages[] = $message; 
     } else { 
      $this->_messages[] = "Could not upload $filename"; 
     } 
     } 
    } 
    } 

}//END CLASS....Upload_Menu 

條紋挫類

protected $_uploaded = array(); 
    protected $_destination; 
    protected $_max = 400000; 
    protected $_messages = array(); 
    protected $_permitted = array('image/gif', 
           'image/jpeg', 
           'image/pjpeg', 
           'image/png'); 
    protected $_renamed = false; 

    public function __construct($path) { 
    if (!is_dir($path) || !is_writable($path)) { 
     throw new Exception("$path must be a valid, writable directory."); 
    } 
    $this->_destination = $path; 
    $this->_uploaded = $_FILES; 
    } 

    public function move() { 
    $field = current($this->_uploaded); 
    $success = move_uploaded_file($field['tmp_name'], $this->_destination . $field['name']); 
    if ($success) { 
     $this->_messages[] = $field['name'] . ' uploaded successfully'; 
    } else { 
     $this->_messages[] = 'Could not upload ' . $field['name']; 
    } 
    } 

    public function getMessages() { 
    return $this->_messages; 
    } 
+0

這是很多上傳文件的代碼。你應該嘗試縮小問題的範圍。 – jeroen

+0

是它有點過分;但我正在修補,然後認爲我會創建一個類,一次做所有的事情......錯誤處理,文件重命名,覆蓋保護等。不可否認,我有點被帶走了。但是,現在我花了這麼多時間在這件事上,我想完全完成它。不幸的是遇到了障礙。大聲笑 – kash101

+0

這是沒有問題的,只是讓基本工作和添加功能(或方法調用),因爲你得到每一步工作。現在你應該擺脫它的大部分(至少在這裏),併發佈一個錯誤發生地點的例子。那究竟出了什麼問題。 – jeroen

回答

0

解決

所以...任何人誰可能會遇到這樣的未來,這裏是一個小於優雅,但最終的解決方案。

pathinfo($string, PATHINFO_EXTENSION) FTW! 

我添加一個排序機制,我processFile方法和Wallah!

protected function processFile($filename, $error, $size, $type, $tmp_name, $overwrite) { 
$OK = $this->checkError($filename, $error); 
if ($OK) { 
    $sizeOK = $this->checkSize($filename, $size); 
    $typeOK = $this->checkType($filename, $type); 
    if ($sizeOK && $typeOK) { 
    $name = $this->checkName($filename, $overwrite); 

    /************************ADDED HERE************************************/ 
    if (pathinfo($filename, PATHINFO_EXTENSION) == 'pdf' || 'doc' || 'docx'){ 
     $this->_destination = './uploads/menus_up/'; 
    if (pathinfo($filename, PATHINFO_EXTENSION) == 'png' || 'jpeg' || 'gif'){ 
     $this->_destination = './uploads/eventBG_up/'; 
    /**********************************************************************/ 

    $success = move_uploaded_file($tmp_name, $this->_destination . $name); 
    if ($success) { 
     $message = "$filename uploaded successfully"; 
     if ($this->_renamed) { 
      $message .= " and renamed $name"; 
     } 
     $this->_messages[] = $message; 
    } else { 
     $this->_messages[] = "Could not upload $filename"; 
    } 
    } 
    } 
} 
} 
} 
0

我將採取略有不同的方法:發送一個特定的文件來構造,當你創建你的對象,如:

$upload = new Upload_File($_FILES['menu'], $destination); 

然後,你必須在你的類所需要的所有信息,你沒有進入全球來自你班級的變量。

然後,您可以遍歷$_FILES數組來添加每個文件或將其放入不同的類中。

這將需要在課堂上進行一些重寫,但它會使它更加靈活,並且可以滿足您的需求。

+0

這將需要我設置兩個實例來處理這兩個文件,對吧?例如:$ upload = new Upload_File($ _ FILES ['menu'],$ destination);和$ upload = new Upload_File($ _ FILES ['img'],$ destination); – kash101