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