2013-10-28 162 views
0

好吧,我已經搜索了一個答案,我找不到任何幫助。我嘗試使用while循環和forloop,但只有一個文件上傳。這是我的代碼。上傳多個文件Php

形式:

<form method="post" enctype="multipart/form-data" action="process.php"> 
<div id="filediv"> 
<div id="imagefiles"> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
<label>Upload File: 
<input name="userfile[]" type="file" id="userfile" multiple></label> 
<label>Alt Text: <input name="alt" type="text"></label> 
</div> 
</div> 

下面是上傳功能:

$alt=mysqli_real_escape_string($conn, $_POST['alt']); 
foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name){ 
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0)) 
{ 
$fileName = $_FILES['userfile']['name'][$key]; 
$tmpName = $_FILES['userfile']['tmp_name'][$key]; 
$fileSize = $_FILES['userfile']['size'][$key]; 
$fileType = $_FILES['userfile']['type'][$key]; 
} else{ 
    echo"error"; 
} 

$allowedExts = array("jpg", "jpeg", "gif", "png"); 
$extension = end(explode(".", $_FILES["userfiles"]["name"])); 
if((($_FILES["userfile"]["type"] == "image/gif") 
    ||($_FILES["userfile"]["type"]=="image/jpeg") 
    ||($_FILES["userfile"]["type"]=="image/png") 
    ||($_FILES["userfile"]["type"]=="image/pjpeg") 
    && in_array($extension, $allowedExts))) 
    { 
     $fp = fopen($tmpName, 'r'); 
     $content =fread($fp, filesize($tmpName)); 
     $SourceImage = imagecreatefromstring($content); 
     $SourceWidth = imagesx($SourceImage); 
     $SourceHeight=imagesy($SourceImage); 
     $DestWidth=100; 
     $DestHeight=130; 
     if ($SourceHeight> $SourceWidth) 
     {$ratio = $DestHeight/$SourceHeight; 
     $newHeight = $DestHeight; 
     $newWidth = $sourceWidth * $ratio; 
     } 
     else 
     { 
      $ratio = $DestWidth/$SourceWidth; 
      $newWidth = $DestWidth; 
      $newHeight = $SourceHeight * $ratio; 
     } 
     $DestinationImage = imagecreatetruecolor($newWidth, $newHeight); 
     imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth); 
     ob_start(); 
     imagejpeg($DestinationImage); 
     $BinaryThumbnail = ob_get_contents(); 
     ob_end_clean(); 
     $thumb = addslashes($BinaryThumbnail); 
     $content = addslashes($content); 
     fclose($fp); 
     $fp  = fopen($tmpName, 'r'); 
$content = fread($fp, filesize($tmpName)); 
$content = addslashes($content); 
fclose($fp); 

     mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed'); 
      echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>"; 
      unlink ($_FILES['username']['tmp_name']); 
    }else{ 
      echo "<script>alert('Please upload an image');location.replace('upload.php');</script>"; 
    } 

} 
} 

我意識到,我並不需要一半我的代碼。現在我有一個圖像上傳,但不能再一次。

+0

這與此相關嗎? http://stackoverflow.com/questions/1361673/get-raw-post-data – jdog

+0

我不認爲這是它。第一個文件不會上傳,但不會超過一個。我的循環,我試過它不起作用。 – Pureblood

+0

當我嘗試你的html時,$ _FILES是完全空的。我不確定PHP是否支持multiple屬性,因此我建議查看原始HTTP數據 – jdog

回答

0

我可能是錯的,我不知道我曾經編寫了一個$ _FILE陣列....但我認爲這個問題是在這裏

while ($_FILES["userfile'"]["tmp_name"][$counter]){ 

除了額外的報價我不認爲數據不存儲這樣的...你可以嘗試做的print_r($ _ FILES)

這可能是

$_FILES["userfile"][$counter]["tmp_name"] 

,但實際上,我懷疑這是否會甚至工作。只是遍歷$ _FILE陣列本身來處理文件的動態數量....這裏是我使用的功能....

public static function upload($file, $accountDirectory, $uploadDirectory) 
{ 
    $return = array(); 
    if(!file_exists($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) { 
     $return["result"] = false; 
    } 
    if($file['error'] != UPLOAD_ERR_OK) { 
     $return["result"] = false; 
      switch($file['error']){ 
      case 0: //no error; possible file attack! 
       echo "There was a problem with your upload."; 
       break; 
      case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini 
       echo "The file you are trying to upload is too big."; 
       break; 
      case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form 
       echo "The file you are trying to upload is too big."; 
       break; 
      case 3: //uploaded file was only partially uploaded 
       echo "The file you are trying upload was only partially uploaded."; 
       break; 
      case 4: //no file was uploaded 
       echo "You must select an image for upload."; 
       break; 
      default: //a default error, just in case! :) 
       echo "There was a problem with your upload."; 
       break;    
      }   
    } else { 
     $newFileName = preg_replace("/[^a-zA-Z0-9-.]/", "", $file["name"]); 
     $uploadLocation = $accountDirectory . $uploadDirectory . $newFileName; 
     while (file_exists($uploadLocation)) { 
      $uploadLocation = $accountDirectory . $uploadDirectory . time() . $newFileName; 
     } 
     if (move_uploaded_file($file["tmp_name"],$uploadLocation)==true) { 
      $return["file"] = str_replace($accountDirectory, "/",$uploadLocation); 
      $return["result"] = true; 
     } else { 
      $return["result"] = false; 
     } 
    } 
    return $return; 
} 
0

這樣做$_FILES['userfile']$_FILES['file'],你只指通過上傳的文件上傳帶有這些名稱的字段(分別爲userfilefile)。 $_FILES是一個關聯數組,所以你應該這樣做

foreach ($_FILES as $fieldName => $fileProperties) { 
    // do something 
} 

另外請注意,你有$_FILES["userfile'"]大約有十幾個線下;額外的'將打破這條線。

查看the PHP help files瞭解更多信息。

+0

謝謝。你讓我意識到我甚至不需要我的代碼的一部分。 – Pureblood

+0

很高興幫助! –

+0

我仍然無法獲取要上傳的多個文件。我添加了foreach($ _ FILES ['userfile'] ['tmp_name']作爲$ key => $ tmp_name){但它不起作用。 – Pureblood