2016-09-21 44 views
0

我有一些代码在我的网站上运行,很适合从文件输入表单元素上传单个文件 - 但我现在需要一个多文件输入表单元素来接受超过1个文件并全部上传服务器和存储在一个逗号分隔字符串上传的文件名的详细信息...如何使与代码这项工作任何想法我使用下面:PHP上传多个文件的代码帮助

表单字段:

<input name="logoexamples[]" id="blogoexamples" type="file" class="textInput" value="notrelevant" multiple> 

PHP代码(工作接受1个文件上传,但不超过1 ....?):

<?php 
// initialize output; 
$output = true; 

// valid extensions 
$ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf', 'jpg', 'jpeg', 'png', 'eps', 'svg', 'gif', 'ai'); 

// create unique path for this form submission 
//$uploadpath = 'assets/uploads/'; 

// you can create some logic to automatically 
// generate some type of folder structure here. 
// the path that you specify will automatically 
// be created by the script if it doesn't already 
// exist. 

// UPLOAD TO FOLDER IN /ASSETS/UPLOADS/ WITH ID OF THE PARENT PROJECT FOLDER RESOURCE 

// Get page ID 
// $pageid = $modx->resource->get('id'); 
// $uploadpath = 'assets/uploads/'.$pageid.'/'; 

// Get parent page title 
$parentObj = $modx->resource->getOne('Parent'); 
$parentpageid = $parentObj->get('pagetitle'); 
$uploadpath = 'assets/uploads/'.$parentpageid.'/'; 

// get full path to unique folder 
$target_path = $modx->config['base_path'] . $uploadpath; 

// get uploaded file names: 
$submittedfiles = array_keys($_FILES); 

// loop through files 
foreach ($submittedfiles as $sf) { 


// Get Filename and make sure its good. 
$filename = basename($_FILES[$sf]['name']); 

// Get file's extension 
$ext = pathinfo($filename, PATHINFO_EXTENSION); 
$ext = mb_strtolower($ext); // case insensitive 

// is the file name empty (no file uploaded) 
if($filename != '') { 

    // is this the right type of file? 
    if(in_array($ext, $ext_array)) { 

     // clean up file name and make unique 
     $filename = mb_strtolower($filename); // to lowercase 
     $filename = str_replace(' ', '_', $filename); // spaces to underscores 
     $filename = date("Y-m-d_G-i-s_") . $filename; // add date & time 

     // full path to new file 
     $myTarget = $target_path . $filename; 

// JWD - save uploaded filenames as a session var to get it on the redirect hook 
$_SESSION['briefing_submittedfiles_' . $sf] = 'http://www.example.com/assets/uploads/'.$parentpageid.'/'.$filename; 


     // create directory to move file into if it doesn't exist 
     mkdir($target_path, 0755, true); 

     // is the file moved to the proper folder successfully? 
     if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) { 
      // set a new placeholder with the new full path (if you need it in subsequent hooks) 
      $modx->setPlaceholder('fi.'.$sf.'_new', $myTarget); 
      // set the permissions on the file 
      if (!chmod($myTarget, 0644)) { /*some debug function*/ } 

     } else { 
      // File not uploaded 
      $errorMsg = 'There was a problem uploading the file.'; 
      $hook->addError($sf, $errorMsg); 
      $output = false; // generate submission error 
     } 

    } else { 
     // File type not allowed 
     $errorMsg = 'Type of file not allowed.'; 
     $hook->addError($sf, $errorMsg); 
     $output = false; // generate submission error 
    } 

// if no file, don't error, but return blank 
} else { 
    $hook->setValue($sf, ''); 
} 

} 

return $output; 

回答

0

我有类似的代码为我的网站,这个代码是超级老,所以不要直接判断或使用它。只是一个例子。

if(isset($_POST['upload'])){ 

for($i=0; $i<count($_FILES['upload']['name']); $i++) { 
//Get the temp file path 
$tmpFilePath = $_FILES['upload']['tmp_name'][$i]; 

//Make sure we have a filepath 
if ($tmpFilePath != ""){ 
//Setup our new file path 
$newFilePath = "../FOLDER NAME/" . $_FILES['upload']['name'][$i]; 

//Upload the file into the temp dir 
if(move_uploaded_file($tmpFilePath, $newFilePath)) { 
    copy($newFilePath, $newFilePath1); 
    $filename = basename($_FILES['upload']['name'][$i]); 
    // add $filename to list or database here 
      $result = "The files were uploaded succesfully."; 
}else{ 
      $result = "There was an error adding the files, please try again!"; 

} 
} 
}