Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在zend框架中创建配置文件图片上传器_Php_Zend Framework - Fatal编程技术网

Php 在zend框架中创建配置文件图片上传器

Php 在zend框架中创建配置文件图片上传器,php,zend-framework,Php,Zend Framework,我最近开始研究zend框架。我想上传个人资料图片,并对其重命名和重新调整大小。我使用下面的代码。有了这个我可以上传,但我不能重命名,我没有办法重新大小上传的文件 如果($this->getRequest()->isPost()) { if(!$objProfilePictureForm->isValid($_POST)) { //return $this->render('add');

我最近开始研究zend框架。我想上传个人资料图片,并对其重命名和重新调整大小。我使用下面的代码。有了这个我可以上传,但我不能重命名,我没有办法重新大小上传的文件

如果($this->getRequest()->isPost()) {

            if(!$objProfilePictureForm->isValid($_POST))
            {
                //return $this->render('add');

            }

            if(!$objProfilePictureForm->profile_pic->receive())
            {
                $this->view->message = '<div class="popup-warning">Errors Receiving File.</div>';


            }

            if($objProfilePictureForm->profile_pic->isUploaded())
            {
                $values = $objProfilePictureForm->getValues();
                $source = $objProfilePictureForm->profile_pic->getFileName();


                //to re-name the image, all you need to do is save it with a new name, instead of the name they uploaded it with. Normally, I use the primary key of the database row where I'm storing the name of the image. For example, if it's an image of Person 1, I call it 1.jpg. The important thing is that you make sure the image name will be unique in whatever directory you save it to.

                $new_image_name = 'new';

                //save image to database and filesystem here
                $image_saved = move_uploaded_file($source, '../uploads/thumb'.$new_image_name);
                if($image_saved)
                {
                    $this->view->image = '<img src="../uploads/'.$new_image_name.'" />';
                    $objProfilePictureForm->reset();//only do this if it saved ok and you want to re-display the fresh empty form
                }
            }
        }
if(!$objProfilePictureForm->isValid($\u POST))
{
//返回$this->render('add');
}
如果(!$objProfilePictureForm->profile\u pic->receive())
{
$this->view->message='接收文件时出错';
}
如果($objProfilePictureForm->profile\u pic->isupload())
{
$values=$objProfilePictureForm->getValues();
$source=$objProfilePictureForm->profile_pic->getFileName();
//要重新命名图像,只需使用新名称保存图像,而不是使用他们上载图像时使用的名称。通常,我使用存储图像名称的数据库行的主键。例如,如果它是Person 1的图像,我将其命名为1.jpg。重要的是要确保图像名称在任何情况下都是唯一的r您保存它的目录。
$new_image_name='new';
//在此处将图像保存到数据库和文件系统
$image\u saved=move\u uploads\u file($source,../uploads/thumb'.$new\u image\u name);
如果($image\u已保存)
{
$this->view->image='';
$objProfilePictureForm->reset();//仅当它保存了ok并且您想重新显示新的空表单时才执行此操作
}
}
}

要在上载时重命名文件,必须将“重命名过滤器”添加到文件表单元素中。该类称为
Zend\u Filter\u File\u Rename

// Create the form
$form = new Zend_Form();

// Create an configure the file-element
$file = new Zend_Form_Element_File('file');
$file->setDestination('my/prefered/path/to/the/file') // This is the path where you want to store the uploaded files.
$file->addFilter('Rename', array('target' => 'my_new_filename.jpg')); // This is for the filename
$form->addElement($file);

// Submit-Button
$form->addElement(new Zend_Form_Element_Submit('save');

// Process postdata
if($this->_request->isPost())
{
    // Get the file and store it within the specified destination with the specified name.
    $file->receive();
}
要动态生成文件名,您可以用时间戳或其他东西来命名它。在调用
$file->receive()
之前,您还可以在后期数据处理中应用重命名过滤器。如果在表中插入一行,并希望使用刚刚插入的行的id命名文件,则这可能非常有用


由于您想存储个人资料图片,您可以从数据库中获取用户id,并用该id命名图片。

谢谢!请告诉我,我们如何使文件名动态化,以及如何将文件移动到其他位置