【问题标题】:Uploadify missing out file extensionUploadify缺少文件扩展名
【发布时间】:2013-04-30 03:28:18
【问题描述】:

我在 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.';

    }
}
?>

【问题讨论】:

  • 你说你得到1-image 没有扩展但你提供的代码不会远程创建该文件名,我有点困惑。
  • 抱歉 - 我的意思不是 1-profile

标签: php uploadify


【解决方案1】:

您是否在定义之前引用了$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

【讨论】:

  • 谢谢!很简单,但我看不到! :)
  • PHP 会对此发出警告。您可能希望在您的开发机器上打开错误报告以使这些问题更易于调试:stackoverflow.com/questions/6575482/…
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 2016-03-01
相关资源
最近更新 更多