如何将我的zend脚本图片文件名放入隐藏字段

时间:2012-07-21 15:32:55

标签: zend-framework upload image hiddenfield

好的,我在这里难过。我正在使用Zend脚本来帮助我上传图片。它工作得很好,但我试图将图片名称捕获到表单的隐藏字段中。我想要注意的是,名称会发生​​变化,因此我需要将其最终放入隐藏字段中。这是Zend脚本:

<?php
if (isset($_POST['upload'])) {
require_once('scripts/library.php');

    try {

        $destination = 'xxxxxxxx';
        $uploader = new Zend_File_Transfer_Adapter_Http();
        $uploader->setDestination($destination);

        $filename = $uploader->getFileName(NULL, FALSE);
        $uploader->addValidator('Size', FALSE, '90kB');
        $uploader->addValidator('ImageSize', FALSE, array('minheight' => 100, 'minwidth' => 100));

        if (!$uploader->isValid()) {
            $messages = $uploader->getMessages();
        } else {

            $no_spaces = str_replace(' ', '_', $filename, $renamed);
            $uploader->addValidator('Extension', FALSE, 'gif, png, jpg');
            $recognized = FALSE;

            if ($uploader->isValid()) {
                $recognized = TRUE;
            } else {

                $mime = $uploader->getMimeType();
                $acceptable = array('jpg' => 'image/jpeg',
                                    'png' => 'image/png',
                                    'gif' => 'image/gif');
                $key = array_search($mime, $acceptable);
                if (!$key) {
                    $messages[] = 'Unrecognized image type';
                } else {

                    $no_spaces = "$no_spaces.$key";
                    $recognized = TRUE;
                    $renamed = TRUE;
                }
            }

            $uploader->clearValidators();
            if ($recognized) {

                $existing = scandir($destination);
                if (in_array($no_spaces, $existing)) {

                    $dot = strrpos($no_spaces, '.');
                    $base = substr($no_spaces, 0, $dot);
                    $extension = substr($no_spaces, $dot);

                    $i = 1;
                    do {
                        $no_spaces = $base . '_' . $i++ . $extension;
                    } while (in_array($no_spaces, $existing));
                    $renamed = TRUE;
                }

                $uploader->addFilter('Rename', array('target' => $no_spaces));
                $success = $uploader->receive();
                if (!$success) {
                    $messages = $uploader->getMessages();
                } else {

                    $uploaded = "$filename uploaded successfully";
                    $pic = $filename;
                    if ($renamed) {
                        $uploaded .= " and renamed $no_spaces";
                    }
                    $messages[] = "$uploaded";
                }
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

这里我试图让$pic变量暂时不抛出错误(在表单页面上)

if (isset($_POST['upload'])) {  } else { $pic = "unknown";}

我试图将zend脚本中的文件名复制到$pic变量中。任何反馈都非常感激:)

0 个答案:

没有答案