$request->file() help

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

$request->file() help

Post by JH14 » Thu Aug 09, 2018 12:24 pm

Hi all,

Firstly I apologise if this is in the wrong place, couldn't quite work out where to put it as the issue is related to this version but also some PHP code.

I'm having some trouble with the $request->file feature. After getting the illegal use of $_FILE I found the $request

I am uploading a file but it doesn't seem to be uploading to the directory.

Am I using the $request correctly?

Here is the script related to the error I'm receiving:

Code: Select all

		$gamefeaturedimage2 = $request->file('featuredimage');
		$gamefeaturedimage = $gamefeaturedimage2['name'];
		
		function uploadImage($featuredimage){
			$target_dir = "images/";
			$target_file = $target_dir . $featuredimage;
			$uploadOk = 1;
			$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
			if ($gamefeaturedimage2['size'] > 30000000) {
				$message = "Only images under 3MB are allowed!";
				writeEMes($message);
				$uploadOk = 0;
			}
			if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
				$message = "Only .jpg and .png image formats are allowed!";
				writeEMes($message);
				$uploadOk = 0;
			}
			if ($uploadOk == 0) {
			}
			else{
				if (move_uploaded_file($gamefeaturedimage2['tmp_name'], $target_file)){
				}
				else {
					$message = "Failed to add image!";
					writeEMes($message);
				}
			}
		}
		
		$temp = explode(".", $gamefeaturedimage);
		$id1 = uniqid();
		$id2 = uniqidReal();
		$hid = $id1."".$id2;		
		$featuredimage = $hid.".".end($temp);
		uploadImage($featuredimage);

If I'm not using it correctly could somebody please advise me on how to do it correctly or point me in the direction of a post or something that does (I personally couldn't find a post related to this).

Kind Regards,

JH14
Last edited by kinerity on Thu Aug 09, 2018 1:29 pm, edited 1 time in total.
Reason: Added [code] tags. Moved to phpBB Custom Coding.

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1888
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: $request->file() help

Post by kinerity » Thu Aug 09, 2018 1:32 pm

You need to call the request class.
Kailey Truscott - Community Team

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Aug 09, 2018 1:37 pm

kinerity wrote:
Thu Aug 09, 2018 1:32 pm
You need to call the request class.
The script is not in the forum directory so does this need to change to:

$request = new \forum\phpbb\request\request();

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1888
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: $request->file() help

Post by kinerity » Thu Aug 09, 2018 1:42 pm

I'm not entirely sure. Try it and post back - I'd be interested. It looks correct.
Kailey Truscott - Community Team

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Aug 09, 2018 1:44 pm

I've tried
$request = new \phpbb\request\request();
and
$request = new forum\phpbb\request\request();
and
$request = new \forum\phpbb\request\request();

but none of them seem to work, i'm not getting any errors in the error_log

It does, however, fail to load the rest of the page

User avatar
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 24715
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: $request->file() help

Post by Paul » Thu Aug 09, 2018 2:11 pm

The first post usage is right. It is probarbly caused somewhere else. Can you post your whole script, instead of a snippet of it?
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Aug 09, 2018 2:15 pm

Paul wrote:
Thu Aug 09, 2018 2:11 pm
The first post usage is right. It is probarbly caused somewhere else. Can you post your whole script, instead of a snippet of it?

Code: Select all


<?php
	$request = new \phpbb\request\request();
	$gameaccid = $user->data['user_id'];
	$userpaidtf = $user->data['user_indielix_paid'];
	
	include("dbconnectfile.php");
	$stmt = $conn->prepare("SELECT COUNT(AccountID) FROM indielix_gameaccount WHERE AccountID='$gameaccid'");
	$stmt->bind_param('i');
	$stmt->bind_result($totalusersgames);
	$stmt->execute();
	$row = $stmt->fetch();
	$conn->close();
	

	$submittf = (isset($_POST['addgamesubmit'])) ? true : false;
	if ($submittf == true) {
		include("dbconnectfile.php");
		$gamename = $conn->real_escape_string(request_var('inputGameName', '0'));
		$gamewebsite = $conn->real_escape_string(request_var('inputGameWebsite', '0'));
		$gameshortdescription = $conn->real_escape_string(request_var('gameshortdescription', '0'));
		$gamedescription = $conn->real_escape_string(request_var('gamedescription', '0'));
		$gamedescription = $conn->real_escape_string(request_var('gamedescription', '0'));
		$gametags = strtolower($conn->real_escape_string(request_var('gametags', '0')));
		$gamestartdate = $conn->real_escape_string(request_var('inputGameStartDate', '0'));
		$gamecategory = $conn->real_escape_string(request_var('gamecategory', '0'));
		$gameprogress = $conn->real_escape_string(request_var('releasestate', '0'));
		$gameios = $conn->real_escape_string(request_var('iosSupport', '0'));
		$gameandroid = $conn->real_escape_string(request_var('androidSupport', '0'));
		$gamemicrosoft = $conn->real_escape_string(request_var('microsoftSupport', '0'));
		$gameiosurl = $conn->real_escape_string(request_var('inputGameIos', '0'));
		$gameandroidurl = $conn->real_escape_string(request_var('inputGameAndroid', '0'));
		$gamemicrosofturl = $conn->real_escape_string(request_var('inputGameMicrosoft', '0'));
		$gamefeaturedimage2 = $request->file('featuredimage');
		$gamefeaturedimage = $gamefeaturedimage2['name'];
		function uniqidReal($length = 13) {
			if (function_exists("random_bytes")) {
				$bytes = random_bytes(ceil($length / 2));
			}
			elseif (function_exists("openssl_random_pseudo_bytes")) {
				$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
			}
			else {
				throw new Exception("no cryptographically secure random function available");
			}
			return substr(bin2hex($bytes), 0, $length);
		}
		
		function uploadImage($featuredimage){
			$target_dir = "images/";
			$target_file = $target_dir . $featuredimage;
			$uploadOk = 1;
			$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
			if ($gamefeaturedimage2['size'] > 30000000) {
				$message = "Only images under 3MB are allowed!";
				writeEMes($message);
				$uploadOk = 0;
			}
			if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
				$message = "Only .jpg and .png image formats are allowed!";
				writeEMes($message);
				$uploadOk = 0;
			}
			if ($uploadOk == 0) {
			}
			else{
				if (move_uploaded_file($gamefeaturedimage2['tmp_name'], $target_file)){
				}
				else {
					$message = "Failed to add image! $target_file";
					writeEMes($message);
				}
			}
		}
		
		$temp = explode(".", $gamefeaturedimage);
		$id1 = uniqid();
		$id2 = uniqidReal();
		$hid = $id1."".$id2;		
		$featuredimage = $hid.".".end($temp);
		uploadImage($featuredimage);
		
		$imagepath = "images/$featuredimage";
		$info = getimagesize($imagepath);
		$aspectratio = $info[1] / $info[0];
		
		if($aspectratio == 0.5625){
			$gameidentification = uniqid()."".uniqidReal();
			include("dbconnectfile.php");
			$addgame = "INSERT INTO indielix_games (GameID, GameName, GameDescription, GameShortDescription, GameWebsite, GameTags, GameOS1, GameOS2, GameOS3, GameIos, GameAndroid, GameWinPhone, GameFeaturedImage, GameStartDate, GameState, GameProgress) VALUES ('$gameidentification', '$gamename', '$gamedescription', '$gameshortdescription', '$gamewebsite', '$gametags', '$gameios', '$gameandroid', '$gamemicrosoft', '$gameiosurl', '$gameandroidurl', '$gamemicrosofturl', '$featuredimage', '$gamestartdate', '99', '$gameprogress')";

			if($conn->query($addgame)){
				include("dbconnectfile.php");
				$addgamecat = "INSERT INTO indielix_gamecategory (GameID, CategoryID) VALUES ('$gameidentification', '$gamecategory')";

				if($conn->query($addgamecat)){
					include("dbconnectfile.php");
					$addgameacc = "INSERT INTO indielix_gameaccount (GameID, AccountID) VALUES ('$gameidentification', '$gameaccid')";

					if($conn->query($addgameacc)){
						$message = "Game has been added! Please wait between 24 and 48 hours for it to be approved by the team!";
						writeSMes($message);
					}
					else{
						$message = "Error adding game to system!";
						writeEMes($message);
					}
					$conn->close();
				}
				else{
					$message = "Error adding game to system!";
					writeEMes($message);
				}
				$conn->close();
			}
			else{
				$message = "Error adding game to system!";
				writeEMes($message);
			}
			$conn->close();
		}
		else{
			unlink($imagepath);
			$message = "Featured image must have the 16:9 aspect ratio! $aspectratio $featuredimage";
			writeEMes($message);
		}
		
	}
	
	if($userpaidtf == 0){
		if($totalusersgames >= 1){
			include_once('message.php');
			$message = "Only one game per user";
			writeEMes($message);
		}
	}
	else{
		include('addgameformpart.php');
	}
?>




User avatar
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 24715
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: $request->file() help

Post by Paul » Thu Aug 09, 2018 2:45 pm

That is still not the full script, as you never include any of the phpBB related files. You will need to properly set the phpBB required variables/constants and after that include at least common.php.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Aug 09, 2018 2:51 pm

Paul wrote:
Thu Aug 09, 2018 2:45 pm
That is still not the full script, as you never include any of the phpBB related files. You will need to properly set the phpBB required variables/constants and after that include at least common.php.
In the header.php that is included I have this at the top:

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = 'forum/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup(); 
?>

The script in the previous post is included as it is in a separate location and so is the form

User avatar
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 24715
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: $request->file() help

Post by Paul » Thu Aug 09, 2018 2:55 pm

In that case you won't need

Code: Select all

$request = new \phpbb\request\request();
At all.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Aug 09, 2018 4:38 pm

Paul wrote:
Thu Aug 09, 2018 2:55 pm
In that case you won't need

Code: Select all

$request = new \phpbb\request\request();
At all.
So with that removed, I still get the error message showing that it failed to save the file.

But when I echo out the contents of $gamefeaturedimage2 I get this array:
( [name] => 86179733.jpg [type] => image/jpeg [tmp_name] => /tmp/phpBXeCAj [error] => 0 [size] => 44183 )

That all looks fine

Any suggestions?

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51347
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: $request->file() help

Post by Brf » Thu Aug 09, 2018 5:49 pm

Is your images/ folder writeable?

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Aug 09, 2018 6:28 pm

Yes.

Folder has 777 permissions

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Sat Aug 11, 2018 2:52 pm

Any more suggestions?

JH14
Registered User
Posts: 17
Joined: Thu Aug 09, 2018 12:14 pm

Re: $request->file() help

Post by JH14 » Thu Sep 20, 2018 1:52 am

Hi all,

I have tried using the upload class instead of the previous script I had used.

No images are being uploaded still, but I'm not sure that I'm using the script correctly. Could somebody point out to me if I need to do any other additions to the script?

Code: Select all

$picupload = (isset($_POST['picupload'])) ? true : false;
	if ($picupload == true) {
		$action = request_var('action', '');
		switch ($action)
		{
			case 'upload':
				//Our main code goes in here
				if (!class_exists('fileupload'))
				{
					include($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
				}
				//Set upload directory
				$upload_dir = 'images/';
				//Upload file
				$upload = new fileupload();
				$upload->set_allowed_extensions(array('png', 'jpg', 'jpeg'));
				$file = $upload->form_upload('picupload');
				if (empty($file->filename))
				{
					trigger_error( 'File upload failed.' . adm_back_link($this->u_action), E_USER_WARNING);
				}
				$file->move_file($upload_dir, true);
				$gameimagenamefile = $file->filename;
				include("dbconnectfile.php");
				$addgame = "INSERT INTO indielix_gameimages (GameID, GameImage) VALUES ('$gameidnumber', '$gameimagenamefile')";

				if($conn->query($addgame)){
					$message = "Image added to game! $gameimagenamefile";
					writeSMes($message);
				}
				else{
					$message = "Error adding image to game!";
					writeEMes($message);
				}
				$conn->close();
			break;
		}		
	}

Here is the form:

Code: Select all

<div class="col-sm-2">
	<form action="" method="post" enctype="multipart/form-data" id="picupload" name="picupload">
		<p>Select image to upload:</p>
		<input type="file" name="gameimgfileToUpload" id="gameimgfileToUpload"><br></br>
		<input type="submit" class="btn btn-primary" value="Upload Image" id="picupload" name="picupload">
	</form>
</div>
Regards,
JH14

Post Reply

Return to “phpBB Custom Coding”

Who is online

Users browsing this forum: No registered users and 1 guest