2013-10-30 12 views
1

我終於取得了一些進展。現在,當我上傳2張圖片時,它會被放入我的數組中。我附加我的vardumb。但是該文件實際上沒有上傳?有任何想法嗎?上傳多個文件卡住了出錯

array(5) { 
    ["name"]=> 
    array(2) { 
    [0]=> 
    string(11) "1bTUWI3.jpg" 
    [1]=> 
    string(11) "1T1NBmd.jpg" 
    } 
    ["type"]=> 
    array(2) { 
    [0]=> 
    string(10) "image/jpeg" 
    [1]=> 
    string(10) "image/jpeg" 
    } 
    ["tmp_name"]=> 
    array(2) { 
    [0]=> 
    string(14) "/tmp/phpgB2bAe" 
    [1]=> 
    string(14) "/tmp/phpg8ZZk1" 
    } 
    ["error"]=> 
    array(2) { 
    [0]=> 
    int(0) 
    [1]=> 
    int(0) 
    } 
    ["size"]=> 
    array(2) { 
    [0]=> 
    int(671869) 
    [1]=> 
    int(352029) 
    } 
} 
    function uploadFile() 

} 
$files = array(); 
$fdata = $_FILES['userfile']; 
if (is_array($fdata["name"])){ 
    for ($i = 0; $i < count($fdata['name']); ++$i) { 
     $files[] = array(
     'name' => $fdata['name'][$i], 
     'tmp_name' => $fdata['tmp_name'][$i], 
     'size' => $fdata['size'][$i], 
     'filetype' => $fdata['type'][$i], 
     ); 
    } 

    echo '<pre>'; 
var_dump($fdata); 
echo '</pre>'; 
$username=$_SESSION['name']; 
$alt=mysqli_real_escape_string($conn, $_POST['alt']); 

$allowedExts = array("jpg", "jpeg", "gif", "png"); 
$extension = end(explode(".", $_FILES["userfiles"]["name"])); 
if((($fdata['type'] == "image/gif") 
    ||($fdata['type']=="image/jpeg") 
    ||($fdata['type']=="image/png") 
    ||($fdata['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>"; 


    }else{ 
      echo "<script>alert('Please upload an image');location.replace('upload.php');</script>"; 
    } 

} 
} 

我仍然卡在我的回聲錯誤。

我的表單看起來像這樣。

 <h1>Upload a file</h1> 
<form method="post" enctype="multipart/form-data" action="process.php"> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
<label>Upload File: 
<input name="userfile" type="file" id="userfile"></label> 
<br> 
<label>Alt Text: <input name="alt" type="text"></label> 
<input name="UploadFile" type="submit" /> 
</form> 

我vardump看起來像這樣

array(1) { 
    ["userfile"]=> 
    array(5) { 
    ["name"]=> 
    array(1) { 
     [0]=> 
     string(17) "Desert - Copy.jpg" 
    } 
    ["type"]=> 
    array(1) { 
     [0]=> 
     string(10) "image/jpeg" 
    } 
    ["tmp_name"]=> 
    array(1) { 
     [0]=> 
     string(14) "/tmp/phpIpCoQ3" 
    } 
    ["error"]=> 
    array(1) { 
     [0]=> 
     int(0) 
    } 
    ["size"]=> 
    array(1) { 
     [0]=> 
     int(845941) 
    } 
    } 
} 
+0

至於我看到'$ file'不會在第一後存在的if/else結構。此外,你需要''foreach''周圍的'{}'。 但這可能不是所有的錯誤,所以把它作爲評論。 – Theolodis

+0

如何讓$文件存在於if else之外? – Pureblood

+0

用於_multiple_文件上傳的$ _FILES結構不是您認爲的結構 - 因此請使用var_dump來查看它_actually_的樣子! – CBroe

回答

0

首先確認它是不是userfileusername

而且在loop像檢查error

// check the userfile array is set and it is not empty 
if(isset($_FILES["userfile"]) and !empty($_FILES["userfile"])) 
{ 
    // loop for each file to upload 
    foreach ($_FILES['userfile'] as $file) 
    { 
     // check for error and size 
     if (($file["error"] == 0) && ($file['size'] > 0)) 
     { 
      // your remaning code 

而在HTML使用file namesarray

<input type="file" name="userfile[]" /> 
<input type="file" name="userfile[]" /> 
+0

是的,這是我在表單中。 – Pureblood

+0

我這樣做,它仍然陷入回聲「錯誤」; – Pureblood

+0

請使用您的當前代碼編輯您的問題,以便顯示您所做的事情! – CBroe