2013-04-29 88 views
2

我在web項目上使用uploadify,我不是一個特別的php專家,所以我很難理解爲什麼以下結果導致上傳的圖像沒有有效的擴展名。我沒有.jpg擴展名而獲得'1-profile'。任何人都可以擺脫任何光線?Uploadify缺少文件擴展名

<?php 
/* 
UploadiFive 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
*/ 

// Set the uplaod directory 
$uploadDir = '/img/profiles/'; 

// Set the allowed file extensions 
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 

    rename($_FILES['Filedata']['name'], $_POST['user_id'].'-profile'); 

    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 

    $targetFile = $uploadDir .$_POST['user_id']. '-profile.'. strtolower($fileParts['extension']); 

    // Validate the filetype 
    $fileParts = pathinfo($_FILES['Filedata']['name']); 
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) { 

     // Save the file 
     move_uploaded_file($tempFile, $targetFile); 
     echo 1; 

    } else { 

     // The file type wasn't allowed 
     echo 'Invalid file type.'; 

    } 
} 
?> 
+0

你說你要'1-image'沒有擴展名尚代碼,您提供的不進行遠程創建該文件的名稱,我有點糊塗。 – 2013-04-29 19:26:12

+0

對不起 - 我的意思是沒有1檔 – 2013-04-29 19:28:14

回答

2

您是否在定義之前引用$fileParts

​​

應該

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$targetFile = $uploadDir .$_POST['user_id']. '-profile.'. strtolower($fileParts['extension']); 

// Validate the filetype 
+0

謝謝!這麼簡單,但我看不到它! :) – 2013-04-29 19:36:06

+0

PHP會對此發出警告。您可能想要在開發計算機上打開錯誤報告,以使這些問題更易於調試:http://stackoverflow.com/questions/6575482/php-how-do-i-enable-error-reporting – nullability 2013-04-29 20:03:55