我在web项目上使用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.';
}
}
?>
答案 0 :(得分: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