Pergunta

I'm using uploadify on web project, and I'm not particularly a php whizz so am struggling to see why the following results in an uploaded image without a valid extension. I get '1-profile' without the .jpg extension. Can anybody shed any light?

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

    }
}
?>
Foi útil?

Solução

Are you referencing $fileParts before it is defined?

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

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

should be

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

// Validate the filetype
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top