2015-11-11 19 views
1
<?php 

require_once "core/init.php"; 

if(isset($_FILES['file'])){ 
    $file = $_FILES['file']; 

    // File properties 
    $file_name = $file['name']; 
    $file_tmp = $file['tmp_name']; 
    $file_size = $file['size']; 
    $file_error = $file['error']; 

    // Work out the file extension 
    $file_ext = explode('.', $file_name); 
    $file_ext = strtolower(end($file_ext)); 

    $allowed = array('jpg', 'png'); 

    if(in_array($file_ext, $allowed)) 
    { 
     echo 'Your file will be processed shortly, thank you.'; 
    } 

    if($file_error == true) 
    { 
     echo 'Failed to upload file'; 
    } 

    if($file_size <= 2097152) { 

     $file_name_new = uniqid('', true) . '.' . $file_ext; 
     $file_destination = 'profilepictures/' . $file_name_new; 

     if(move_uploaded_file($file_tmp, $file_destination)); 
      echo $destination; 
    } 
} 
?> 

簡單的問題,目前只是沮喪。我試圖在失敗的上傳中將其設置爲echo Failed to upload file,但是這是在成功上傳和上傳失敗時完成的。我收到了兩條消息,一條是成功的,一條是上傳失敗的,但是當上傳不好時,我只能得到失敗的消息。如果出現錯誤,則在上傳時回顯文件錯誤

+0

嘗試傾銷'$ file_error'上成功上傳 - 看看它說什麼? – Ben

回答

1

$file_error將返回一個值(因此將是true),即使上傳成功。 See PHP page

更改您的支票:

if($file_error !== UPLOAD_ERR_OK){ //... 

或者

if($file_error !== 0){ //... 
+0

同樣的事情正在發生,因爲我評論了我有的其他答案 – David

1

嘗試通過if($file_error != 0)

這裏更換if($file_error == true)的解釋是:http://php.net/manual/en/features.file-upload.errors.php

+0

我已經實際完成了這個方法,它不會用'!'回顯我的消息如果我刪除它,它會'回顯'我的消息,但也顯示上傳成功的上傳消息。 – David

+0

成功的消息在哪裏? –

+0

'if(in_array($ file_ext,$ allowed)) { echo'您的文件將很快處理,謝謝。'; }' – David

相關問題