2013-04-29 92 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