2014-05-24 199 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