2

我在网络项目上使用uploadify,而且我不是一个特别的php高手,所以我很难理解为什么以下结果会导致上传的图像没有有效的扩展名。我得到没有 .jpg 扩展名的“1-profile”。任何人都可以解释一下吗?

    <?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.';

    }
}
?>
4

1 回答 1

2

您是否$fileParts在定义之前引用它?

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

// Validate the filetype
$fileParts = pathinfo($_FILES['Filedata']['name']);

应该

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

// Validate the filetype
于 2013-04-29T19:28:18.543 回答