Skip to content

Commit eb7a7d2

Browse files
committed
Make file upload work
1 parent f76daf5 commit eb7a7d2

6 files changed

Lines changed: 326 additions & 165 deletions

File tree

src/Ci4FileBridge.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/Ci4RequestBridge.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use Laminas\Diactoros\ServerRequest;
55
use SDPMlab\Ci4Roadrunner\Ci4UriBridge;
6-
use SDPMlab\Ci4Roadrunner\Ci4FileBridge;
6+
use SDPMlab\Ci4Roadrunner\UploadedFileBridge;
77

88
class Ci4RequestBridge
99
{
@@ -24,8 +24,7 @@ public function __construct(ServerRequest $rRequest)
2424

2525
private function setFile(){
2626
if(count($this->_rRequest->getUploadedFiles()) > 0){
27-
$fileBridge = new Ci4FileBridge($this->_rRequest->getUploadedFiles());
28-
$fileBridge->setFile();
27+
UploadedFileBridge::getPsr7UploadedFiles($this->_rRequest->getUploadedFiles(),true);
2928
}
3029
}
3130

src/UploadedFile.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
namespace SDPMlab\Ci4Roadrunner;
3+
4+
use Psr\Http\Message\UploadedFileInterface;
5+
use Psr\Http\Message\StreamInterface;
6+
use Laminas\Diactoros\Stream;
7+
use CodeIgniter\HTTP\Exceptions\HTTPException;
8+
9+
class UploadedFile implements UploadedFileInterface
10+
{
11+
12+
private $path;
13+
private $clientFilename;
14+
private $clientMediaType;
15+
private $size;
16+
17+
/**
18+
* PHP uploaderror code
19+
*
20+
* @var integer
21+
*/
22+
private $error;
23+
24+
/**
25+
* Whether the file has been moved already or not.
26+
*
27+
* @var boolean
28+
*/
29+
protected $hasMoved = false;
30+
31+
32+
/**
33+
* Accepts the file information as would be filled in from the $_FILES array.
34+
*
35+
* @param string $path The temporary location of the uploaded file.
36+
* @param string $filename The client-provided filename.
37+
* @param string $mimeType The type of file as provided by PHP
38+
* @param integer $size The size of the file, in bytes
39+
* @param integer $error The error constant of the upload (one of PHP's UPLOADERRXXX constants)
40+
*/
41+
public function __construct(string $path,string $filename = null, string $mimeType = null, int $size = null, int $error = null)
42+
{
43+
$this->path = $path;
44+
$this->clientFilename = $filename;
45+
$this->clientMediaType = $mimeType;
46+
$this->size = $size;
47+
$this->error = $error;
48+
49+
//parent::__construct($path, false);
50+
}
51+
52+
53+
/**
54+
* Retrieve a stream representing the uploaded file.
55+
*
56+
* @return StreamInterface Stream representation of the uploaded file.
57+
* @throws \RuntimeException in cases when no stream is available or can be
58+
* created.
59+
*/
60+
public function getStream() : StreamInterface
61+
{
62+
if ($this->error !== UPLOAD_ERR_OK) {
63+
throw HTTPException::forInvalidFile();
64+
}
65+
66+
if ($this->hasMoved)
67+
{
68+
throw HTTPException::forAlreadyMoved();
69+
}
70+
71+
if ($this->stream instanceof StreamInterface) {
72+
return $this->stream;
73+
}
74+
75+
$this->stream = new Stream($this->path);
76+
return $this->stream;
77+
}
78+
79+
/**
80+
* Move the uploaded file to a new location.
81+
*
82+
* @see http://php.net/is_uploaded_file
83+
* @see http://php.net/move_uploaded_file
84+
* @param string $targetPath Path to which to move the uploaded file.
85+
* @throws \InvalidArgumentException if the $targetPath specified is invalid.
86+
* @throws \RuntimeException on any error during the move operation, or on
87+
* the second or subsequent call to the method.
88+
*/
89+
public function moveTo($targetPath)
90+
{
91+
$path = $this->setPath($targetPath); //set the target path
92+
93+
if ($this->hasMoved) throw HTTPException::forAlreadyMoved();
94+
95+
if ($this->error !== UPLOAD_ERR_OK) throw HTTPException::forInvalidFile();
96+
97+
try
98+
{
99+
move_uploaded_file($this->path, $targetPath);
100+
}
101+
catch (\Exception $e)
102+
{
103+
$error = error_get_last();
104+
$message = isset($error['message']) ? strip_tags($error['message']) : '';
105+
throw HTTPException::forMoveFailed(basename($this->path), $targetPath, $message);
106+
}
107+
108+
@chmod($targetPath, 0777 & ~umask());
109+
110+
$this->hasMoved = true;
111+
112+
return true;
113+
}
114+
115+
/**
116+
* create file target path if
117+
* the set path does not exist
118+
*
119+
* @param string $path
120+
*
121+
* @return string The path set or created.
122+
*/
123+
protected function setPath(string $targetPath): string
124+
{
125+
$path = dirname($targetPath);
126+
if (! is_dir($path))
127+
{
128+
mkdir($path, 0777, true);
129+
//create the index.html file
130+
if (! is_file($path . 'index.html'))
131+
{
132+
$file = fopen($path . 'index.html', 'x+');
133+
fclose($file);
134+
}
135+
}
136+
return $path;
137+
}
138+
139+
/**
140+
* Retrieve the file size.
141+
*
142+
* @return int|null The file size in bytes or null if unknown.
143+
*/
144+
public function getSize() : ?int
145+
{
146+
return $this->size;
147+
}
148+
149+
/**
150+
* Retrieve the error associated with the uploaded file.
151+
*
152+
* @return int One of PHP's UPLOAD_ERR_XXX constants.
153+
*/
154+
public function getError() : int
155+
{
156+
return $this->error;
157+
}
158+
159+
/**
160+
* Retrieve the filename sent by the client.
161+
*
162+
* @return string|null The filename sent by the client or null if none
163+
* was provided.
164+
*/
165+
public function getClientFilename() : ?string
166+
{
167+
return $this->clientFilename;
168+
}
169+
170+
/**
171+
* Retrieve the media type sent by the client.
172+
*
173+
* @return string|null The media type sent by the client or null if none
174+
* was provided.
175+
*/
176+
public function getClientMediaType()
177+
{
178+
return $this->clientMediaType;
179+
}
180+
181+
}
182+
183+
?>

0 commit comments

Comments
 (0)