2015-09-16 97 views
0

我寫了下面的代碼,應該驗證用戶上傳的圖像。但是,當代碼運行時,它會返回一個空白頁面。在這裏我的代碼:php - 文件上傳返回空白頁

HTML<form>標題):

<form class="registerbodysections" enctype="multipart/form-data" method="post" action="php/registrationForm.php"> 

PHP

<?php 

$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$email = $_POST['email']; 
$confirmemail = $_POST['confirmemail']; 
$birthday = $_POST['birthday']; 
$birthmonth = $POST['birthmonth']; 
$birthyear = $_POST['birthyear']; 
$location = $_POST['location']; 
$picture = $_FILES['picture']; 
$picturelocation = "../img/uploads/'$email'/picture/'$picture[\'type\']"; 

echo var_dump($picture); //for testing purposes, but does not get executed 

function imageFormatCheck ($fileMime) { 
    $datatypes = array(
    'image/jpg', 
    'image/gif', 
    'image/png', 
    'image/jpeg' 
    ); 

    foreach($datatypes as $datatype) { 
     if ($fileMime !== $datatype) { 
      return false; 
     } 
    } 
    return true; 
} 

function checkFileType() { 
    if(imageFormatCheck($fileMime)) { 
     header('Location: http://google.com'); 
     exit(); 
    } else { 
     header('Location: http://facebook.com'); 
     exit(); 
    } 
} 

function moveFile() { 
    if(!move_uploaded_file($tmpName, $picturelocation)) { 
     header('Location: http://google.com'); 
     exit(); 
    } else { 
     header('Location: http://facebook.com'); 
     exit(); 
    } 
} 


if(count($picture)) { 

    $tmpName = $picture['tmp_name']; //when I var_dump $picture I receive a single array, not a double array as often seen 
    $fInfo = $finfo_open(FILEINFO_MIME_TYPE); 
    $fileMime = finfo_file($fInfo, $tmpName); 
    $finfo_close($fInfo); 

    checkFileType(); 
    moveFile(); 

} 

$userDataArray = array(
    $firstname, 
    $lastname, 
    $birthday, 
    $birthmonth, 
    $birthyear, 
    $location, 
    $picturelocation 
); 

?> 

我添加用於測試目的的標頭。但是,這些都沒有得到執行。相反,如上所述,每次都會顯示一個空白頁面。 HTML <form>元素是正確的,這就是爲什麼我沒有在這裏包括它們。任何想法,問題可能是?提前致謝!

注意:目錄$picturelocation實際上並不存在。 move_uploaded_file()是否可以動態創建,如果它不存在?在文檔中找不到任何內容。

+0

$ userDataArray的輸出是什麼?我特別感興趣的是$ picturelocation變量的輸出。 – satroy

+0

嘗試添加'ini_set(「display_errors」,1); error_reporting(E_ALL);'超出頁面上的所有內容。您可能有錯誤。 – Rasclatt

+0

@Rasclatt謝謝你的回覆。只是這樣做,但頁面仍然保持空白。 – MoneyIsAMotivation

回答

1

文件信息可以從超級全局$ _FILES獲得。因此,您可以從以下列表中獲取名稱,類型,大小和文件的信息。

$_FILES['userfile']['name'] 
$_FILES['userfile']['type'] 
$_FILES['userfile']['size'] 
$_FILES['userfile']['tmp_name']