Page 1 of 1

How to using "upload class" in PHPBB 3.2.2

Posted: Sun Jul 15, 2018 9:37 am
by CongVT
Hi everyone !
I'm a new in this forum. I am learning to write an extension !
I'm having problems using an upload class. I tried a lot of the code is still running but no file is up. Does anyone here have any sample code on this issue or can help me see the code below

Code: Select all

private function doc_upload($action = '', $form_name)
	{
		$old_doc = $this->request->variable('old_doc ', '');
		$upload_dir = $this->root_path . 'ext/vtcong/libraries/docs/';
		
		$this->files_upload->reset_vars();
		$this->files_upload->set_allowed_extensions(array('doc', 'docx', 'xls', 'xlsx'));
		
		// Upload file
		$this->set_file($this->files_upload->handle_upload('files.types.form', $form_name));
		
		echo $form_name;
		echo 'Tên file:'.$this->file->get('realname');
		
		$this->file->clean_filename('unique_ext');
		
		echo $this->file->get('realname');
		
		$name = $this->file->get('realname');
		if ($action == 'edit_article' && empty($name))
		{
			return $old_doc;
		}
		
		if (!$this->file->move_file($upload_dir))
		{
			$this->file->set_error('FILE_MOVE_UNSUCCESSFUL');
		}
		
			if (count($this->file->error))
		{
			throw new \phpbb\exception\runtime_exception($this->file->error[0]);
		}
		// remove the old doc if set
		@unlink($this->root_path . 'ext/vtcong/libraries/docs'. $old_doc);
		
		return $this->file->get('realname');
	}

Re: How to using "upload class" in PHPBB 3.2.2

Posted: Sun Jul 15, 2018 6:31 pm
by GanstaZ
You can read about it here => File uploads.

Re: How to using "upload class" in PHPBB 3.2.2

Posted: Mon Jul 16, 2018 4:31 am
by CongVT
GanstaZ wrote:
Sun Jul 15, 2018 6:31 pm
You can read about it here => File uploads.
Thank you !
I also followed the basic instructions! The code is still running and not error. However, no ffile is transferred to the host. I still do not understand where I was wrong because it does not report any errors!
Can someone show me an example code for this problem! The instructions at the link you brought are really too sketchy!

Re: How to using "upload class" in PHPBB 3.2.2

Posted: Mon Jul 16, 2018 12:28 pm
by pierredu
My glossary extension (still in development) uses a minimal uploading feature.
Have a look at the file core/glossedit.php. Github repo: https://github.com/pierrdu/lmdi_gloss.

Re: How to using "upload class" in PHPBB 3.2.2

Posted: Mon Jul 16, 2018 6:49 pm
by canonknipser
Just had a quick glance at you code - maybe you are struggling with your $old_doc-handling? Your comment is // remove the old doc if set, but you don't check if it is set, so even if it is unset your @unlink is executed. Maybe you delete the complete content of the folder?

Maybe?

Re: How to using "upload class" in PHPBB 3.2.2

Posted: Wed Jul 18, 2018 9:52 am
by CongVT

Code: Select all

private function doc_upload($action = '')
	{
		$old_doc = $this->request->variable('old_doc ', '');
		$upload_dir = $this->root_path . 'ldocs';
		
		$upload = $this->files_factory->get('upload')
					   ->set_allowed_extensions(array('doc', 'docx', 'xls', 'xlsx'));
		
		// Upload file
		$file = $upload->handle_upload('files.types.form', 'doc_file');
		$name = $file->get('realname');

		if ($action == 'edit_article' && empty($name))
		{
			return $old_doc;
		}else 
		{
			@unlink($this->root_path . 'ldocs/'. $old_doc);
		}
		$file->move_file($upload_dir);
		return $file->get('uploadname');
	}
it return "0".