如何使用 PHP 和 Zend Framework 进行文件上传?

发布于 2024-08-13 15:29:30 字数 3284 浏览 7 评论 0原文

我正在使用 Zend Framework 1.9.6。我想我已经把事情弄清楚了,除了最后。这是我到目前为止所拥有的:

表单:

<?php

class Default_Form_UploadFile extends Zend_Form
{
    public function init()
    {
        $this->setAttrib('enctype', 'multipart/form-data');
        $this->setMethod('post');

        $description = new Zend_Form_Element_Text('description');
        $description->setLabel('Description')
            ->setRequired(true)
            ->addValidator('NotEmpty');
        $this->addElement($description);

        $file = new Zend_Form_Element_File('file');
        $file->setLabel('File to upload:')
            ->setRequired(true)
            ->addValidator('NotEmpty')
            ->addValidator('Count', false, 1);
        $this->addElement($file);

        $this->addElement('submit', 'submit', array(
            'label'    => 'Upload',
            'ignore'   => true
        ));
    }
}

控制器:

public function uploadfileAction()
{
    $form = new Default_Form_UploadFile();
    $form->setAction($this->view->url());

    $request = $this->getRequest();

    if (!$request->isPost()) {
        $this->view->form = $form;
        return;
    }

    if (!$form->isValid($request->getPost())) {
        $this->view->form = $form;
        return;
    }

    try {
        $form->file->receive();
        //upload complete!
        //...what now?
        $location = $form->file->getFileName();
        var_dump($form->file->getFileInfo());
    } catch (Exception $exception) {
        //error uploading file
        $this->view->form = $form;
    }
}

现在我该如何处理该文件?默认已经上传到我的/tmp目录了。显然那不是我想保存它的地方。我希望我的应用程序的用户能够下载它。所以,我想这意味着我需要将上传的文件移动到应用程序的公共目录并将文件名存储在数据库中,以便我可以将其显示为网址。

或者首先将其设置为上传目录(尽管我之前尝试这样做时遇到了错误)。

您以前使用过上传的文件吗?我下一步应该采取什么步骤?

解决方案:

我决定将上传的文件放入 data/uploads (这是一个指向我的应用程序外部目录的符号链接,以便我的应用程序的所有版本都可以访问它)。

# /public/index.php
# Define path to uploads directory
defined('APPLICATION_UPLOADS_DIR')
    || define('APPLICATION_UPLOADS_DIR', realpath(dirname(__FILE__) . '/../data/uploads'));

# /application/forms/UploadFile.php
# Set the file destination on the element in the form
$file = new Zend_Form_Element_File('file');
$file->setDestination(APPLICATION_UPLOADS_DIR);

# /application/controllers/MyController.php
# After the form has been validated...
# Rename the file to something unique so it cannot be overwritten with a file of the same name
$originalFilename = pathinfo($form->file->getFileName());
$newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension'];
$form->file->addFilter('Rename', $newFilename);

try {
    $form->file->receive();
    //upload complete!

    # Save a display filename (the original) and the actual filename, so it can be retrieved later
    $file = new Default_Model_File();
    $file->setDisplayFilename($originalFilename['basename'])
        ->setActualFilename($newFilename)
        ->setMimeType($form->file->getMimeType())
        ->setDescription($form->description->getValue());
    $file->save();
} catch (Exception $e) {
    //error
}

I am using Zend Framework 1.9.6. I think I've got it pretty much figured out except for the end. This is what I have so far:

Form:

<?php

class Default_Form_UploadFile extends Zend_Form
{
    public function init()
    {
        $this->setAttrib('enctype', 'multipart/form-data');
        $this->setMethod('post');

        $description = new Zend_Form_Element_Text('description');
        $description->setLabel('Description')
            ->setRequired(true)
            ->addValidator('NotEmpty');
        $this->addElement($description);

        $file = new Zend_Form_Element_File('file');
        $file->setLabel('File to upload:')
            ->setRequired(true)
            ->addValidator('NotEmpty')
            ->addValidator('Count', false, 1);
        $this->addElement($file);

        $this->addElement('submit', 'submit', array(
            'label'    => 'Upload',
            'ignore'   => true
        ));
    }
}

Controller:

public function uploadfileAction()
{
    $form = new Default_Form_UploadFile();
    $form->setAction($this->view->url());

    $request = $this->getRequest();

    if (!$request->isPost()) {
        $this->view->form = $form;
        return;
    }

    if (!$form->isValid($request->getPost())) {
        $this->view->form = $form;
        return;
    }

    try {
        $form->file->receive();
        //upload complete!
        //...what now?
        $location = $form->file->getFileName();
        var_dump($form->file->getFileInfo());
    } catch (Exception $exception) {
        //error uploading file
        $this->view->form = $form;
    }
}

Now what do I do with the file? It has been uploaded to my /tmp directory by default. Obviously that's not where I want to keep it. I want users of my application to be able to download it. So, I'm thinking that means I need to move the uploaded file to the public directory of my application and store the file name in the database so I can display it as a url.

Or set this as the upload directory in the first place (though I was running into errors while trying to do that earlier).

Have you worked with uploaded files before? What is the next step I should take?

Solution:

I decided to put the uploaded files into data/uploads (which is a sym link to a directory outside of my application, in order to make it accessible to all versions of my application).

# /public/index.php
# Define path to uploads directory
defined('APPLICATION_UPLOADS_DIR')
    || define('APPLICATION_UPLOADS_DIR', realpath(dirname(__FILE__) . '/../data/uploads'));

# /application/forms/UploadFile.php
# Set the file destination on the element in the form
$file = new Zend_Form_Element_File('file');
$file->setDestination(APPLICATION_UPLOADS_DIR);

# /application/controllers/MyController.php
# After the form has been validated...
# Rename the file to something unique so it cannot be overwritten with a file of the same name
$originalFilename = pathinfo($form->file->getFileName());
$newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension'];
$form->file->addFilter('Rename', $newFilename);

try {
    $form->file->receive();
    //upload complete!

    # Save a display filename (the original) and the actual filename, so it can be retrieved later
    $file = new Default_Model_File();
    $file->setDisplayFilename($originalFilename['basename'])
        ->setActualFilename($newFilename)
        ->setMimeType($form->file->getMimeType())
        ->setDescription($form->description->getValue());
    $file->save();
} catch (Exception $e) {
    //error
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凯凯我们等你回来 2024-08-20 15:29:30

默认情况下,文件上传到系统临时目录,这意味着您可以:

  • 使用 move_uploaded_file 将文件移动到其他位置,
  • 或配置 Zend Framework 应将文件移动到的目录;您的表单元素应该有一个可用于此目的的 setDestination 方法。

对于第二点,手册中有一个示例 :

$element = new Zend_Form_Element_File('foo');
$element->setLabel('Upload an image:')
        ->setDestination('/var/www/upload')
        ->setValueDisabled(true);

(但是请阅读该页面:还有其他有用的信息)

By default, files are uploaded to the system temporary directory, which means you'll to either :

  • use move_uploaded_file to move the files somewhere else,
  • or configure the directory to which Zend Framework should move the files ; your form element should have a setDestination method that can be used for that.

For the second point, there is an example in the manual :

$element = new Zend_Form_Element_File('foo');
$element->setLabel('Upload an image:')
        ->setDestination('/var/www/upload')
        ->setValueDisabled(true);

(But read that page : there are other usefull informations)

苯莒 2024-08-20 15:29:30

如果您要将文件移动到公共目录,任何人都可以向其他人发送该文件的链接,并且您无法控制谁有权访问该文件。

相反,您可以将文件作为 longblob 存储在数据库中,然后使用 Zend Framework 让用户通过控制器/操作访问该文件。这将使您可以围绕文件的访问封装自己的身份验证和用户权限逻辑。

您需要从 /tmp 目录获取文件,以便将其保存到数据库:

// I think you get the file name and path like this:
$data = $form->getValues(); // this makes it so you don't have to call receive()
$fileName = $data->file->tmp_name; // includes path
$file = file_get_contents($fileName);

// now save it to the database. you can get the mime type and other
// data about the file from $data->file. Debug or dump $data to see
// what else is in there

控制器中用于查看的操作将具有您的授权逻辑,然后从数据库加载行:

// is user allowed to continue?
if (!AuthenticationUtil::isAllowed()) {
   $this->_redirect("/error");
}

// load from db
$fileRow = FileUtil::getFileFromDb($id); // don't know what your db implementation is

$this->view->fileName = $fileRow->name;
$this->view->fileNameSuffix = $fileRow->suffix;
$this->view->fileMimeType = $fileRow->mime_type;
$this->view->file = $fileRow->file;

然后在视图中:

<?php
header("Content-Disposition: attachment; filename=".$this->fileName.".".$this->fileNameSuffix);
header('Content-type: ".$this->fileMimeType."');
echo $this->file;
?>

If you were to move the file to a public directory, anyone would be able to send a link to that file to anyone else and you have no control over who has access to the file.

Instead, you could store the file in the DB as a longblob and then use the Zend Framework to provide users access the file through a controller/action. This would let you wrap your own authentication and user permission logic around access to the files.

You'll need to get the file from the /tmp directory in order to save it to the db:

// I think you get the file name and path like this:
$data = $form->getValues(); // this makes it so you don't have to call receive()
$fileName = $data->file->tmp_name; // includes path
$file = file_get_contents($fileName);

// now save it to the database. you can get the mime type and other
// data about the file from $data->file. Debug or dump $data to see
// what else is in there

Your action in the controller for viewing would have your authorization logic and then load the row from the db:

// is user allowed to continue?
if (!AuthenticationUtil::isAllowed()) {
   $this->_redirect("/error");
}

// load from db
$fileRow = FileUtil::getFileFromDb($id); // don't know what your db implementation is

$this->view->fileName = $fileRow->name;
$this->view->fileNameSuffix = $fileRow->suffix;
$this->view->fileMimeType = $fileRow->mime_type;
$this->view->file = $fileRow->file;

Then in the view:

<?php
header("Content-Disposition: attachment; filename=".$this->fileName.".".$this->fileNameSuffix);
header('Content-type: ".$this->fileMimeType."');
echo $this->file;
?>
与往事干杯 2024-08-20 15:29:30
 $this->setAction('/example/upload')->setEnctype('multipart/form-data');
 $photo = new Zend_Form_Element_File('photo');
 $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../public/tmp/upload"); 
 $this->addElement($photo);
 $this->setAction('/example/upload')->setEnctype('multipart/form-data');
 $photo = new Zend_Form_Element_File('photo');
 $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../public/tmp/upload"); 
 $this->addElement($photo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文