簡體   English   中英

PHP處理多個文件上傳輕松

[英]PHP Handle Multiple File Uploads EASY

解決了! 使用來自php.net post方法的此示例來處理多個上載,並獲得了使用andre的答案所獲得的知識,這就是為什么我選擇它作為答案的原因!

foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
  if ($x=="0"){
    $data=explode(".",$_FILES["userfile"]["name"][$key]);
    $named=$id[id].".".$data[1];
    $x=$x+1;
    }else{
    $data=explode(".",$_FILES["userfile"]["name"][$key]);
    $named=$id[id]."-".$x.".".$data[1];
    $x=$x+1;
    }
    $uploaddir = '/home/content/92/8498392/html/items/'.$_POST[catid];
    $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
    move_uploaded_file($tmp_name, "$uploaddir/$named");
}

}

結果是第一個圖像以45.jpg上傳,第二個圖像以45-1.jpg上傳,依此類推! 謝謝你們的幫助:D

問題是您如何遍歷$ _FILES數組。 它不能像您想象的那樣處理多個文件,因此請使用以下功能並將其合並到腳本中。 以下腳本的功勞歸於PHP網站上的家伙。 鏈接在代碼下方。

function multiple(array $_files, $top = TRUE)
{
    $files = array();
    foreach($_files as $name=>$file){
        if($top) $sub_name = $file['name'];
        else    $sub_name = $name;

        if(is_array($sub_name)){
            foreach(array_keys($sub_name) as $key){
                $files[$name][$key] = array(
                    'name'     => $file['name'][$key],
                    'type'     => $file['type'][$key],
                    'tmp_name' => $file['tmp_name'][$key],
                    'error'    => $file['error'][$key],
                    'size'     => $file['size'][$key],
                );
                $files[$name] = multiple($files[$name], FALSE);
            }
        }else{
            $files[$name] = $file;
        }
    }
    return $files;
}

print_r($_FILES);
/*
Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => 400.png
                )
            [type] => Array
                (
                    [0] => image/png
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/php5Wx0aJ
                )
            [error] => Array
                (
                    [0] => 0
                )
            [size] => Array
                (
                    [0] => 15726
                )
        )
)
*/
$files = multiple($_FILES);
print_r($files);
/*
Array
(
    [image] => Array
        (
            [0] => Array
                (
                    [name] => 400.png
                    [type] => image/png
                    [tmp_name] => /tmp/php5Wx0aJ
                    [error] => 0
                    [size] => 15726
                )
        )
)
*/
?>

我是從PHP網站上獲取的

<?php
$id = 877;
$x = 0;
foreach ($_FILES as $file) {
  $data = explode(".", $file['userfile']['name']);
  $fileExtension = $data[count($data)-1];
  if ($x == 0) {
    $filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension; 
  }
  else {
    $filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
  }
  move_uploaded_file($file['userfile']['tmp_name'], $filePath);
  $x++;
}
?>

試試看

只需使用我提供的功能並合並如下:

<?php
$id = 877;
$x = 0;
$_FILES = multiple($_FILES); // taken from above snippet from php.net
foreach ($_FILES as $fileKey => $file) {
  $data = explode(".", $file['name']);
  $fileExtension = $data[count($data)-1];
  if ($x == 0) {
    $filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension; 
  }
  else {
    $filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
  }
  move_uploaded_file($file['tmp_name'], $filePath);
  $x++;
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM