How to using "upload class" in PHPBB 3.2.2

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
CongVT
Registered User
Posts: 3
Joined: Sun Jul 15, 2018 8:37 am

How to using "upload class" in PHPBB 3.2.2

Post by CongVT » Sun Jul 15, 2018 9:37 am

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');
	}

User avatar
GanstaZ
Jr. Extension Validator
Posts: 641
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

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

Post by GanstaZ » Sun Jul 15, 2018 6:31 pm

You can read about it here => File uploads.
"When answer lies in the question,.. question becomes redundant!"

CongVT
Registered User
Posts: 3
Joined: Sun Jul 15, 2018 8:37 am

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

Post by CongVT » Mon Jul 16, 2018 4:31 am

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!

User avatar
pierredu
Registered User
Posts: 886
Joined: Thu Nov 01, 2012 8:04 am
Location: Paris (France)

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

Post by pierredu » Mon Jul 16, 2018 12:28 pm

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.

User avatar
canonknipser
Registered User
Posts: 1708
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

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

Post by canonknipser » Mon Jul 16, 2018 6:49 pm

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?
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB

CongVT
Registered User
Posts: 3
Joined: Sun Jul 15, 2018 8:37 am

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

Post by CongVT » Wed Jul 18, 2018 9:52 am

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".

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 13 guests