1

好吧,我被难住了。我正在使用帮助我上传图片的 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";}

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

4

0 回答 0