Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../vendor/guzzleht.../psr7/src
File: UploadedFile.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Psr7;
[2] Fix | Delete
[3] Fix | Delete
use InvalidArgumentException;
[4] Fix | Delete
use Psr\Http\Message\StreamInterface;
[5] Fix | Delete
use Psr\Http\Message\UploadedFileInterface;
[6] Fix | Delete
use RuntimeException;
[7] Fix | Delete
[8] Fix | Delete
class UploadedFile implements UploadedFileInterface
[9] Fix | Delete
{
[10] Fix | Delete
/**
[11] Fix | Delete
* @var int[]
[12] Fix | Delete
*/
[13] Fix | Delete
private static $errors = [
[14] Fix | Delete
UPLOAD_ERR_OK,
[15] Fix | Delete
UPLOAD_ERR_INI_SIZE,
[16] Fix | Delete
UPLOAD_ERR_FORM_SIZE,
[17] Fix | Delete
UPLOAD_ERR_PARTIAL,
[18] Fix | Delete
UPLOAD_ERR_NO_FILE,
[19] Fix | Delete
UPLOAD_ERR_NO_TMP_DIR,
[20] Fix | Delete
UPLOAD_ERR_CANT_WRITE,
[21] Fix | Delete
UPLOAD_ERR_EXTENSION,
[22] Fix | Delete
];
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* @var string
[26] Fix | Delete
*/
[27] Fix | Delete
private $clientFilename;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* @var string
[31] Fix | Delete
*/
[32] Fix | Delete
private $clientMediaType;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* @var int
[36] Fix | Delete
*/
[37] Fix | Delete
private $error;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* @var string|null
[41] Fix | Delete
*/
[42] Fix | Delete
private $file;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* @var bool
[46] Fix | Delete
*/
[47] Fix | Delete
private $moved = false;
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* @var int
[51] Fix | Delete
*/
[52] Fix | Delete
private $size;
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* @var StreamInterface|null
[56] Fix | Delete
*/
[57] Fix | Delete
private $stream;
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* @param StreamInterface|string|resource $streamOrFile
[61] Fix | Delete
* @param int $size
[62] Fix | Delete
* @param int $errorStatus
[63] Fix | Delete
* @param string|null $clientFilename
[64] Fix | Delete
* @param string|null $clientMediaType
[65] Fix | Delete
*/
[66] Fix | Delete
public function __construct(
[67] Fix | Delete
$streamOrFile,
[68] Fix | Delete
$size,
[69] Fix | Delete
$errorStatus,
[70] Fix | Delete
$clientFilename = null,
[71] Fix | Delete
$clientMediaType = null
[72] Fix | Delete
) {
[73] Fix | Delete
$this->setError($errorStatus);
[74] Fix | Delete
$this->setSize($size);
[75] Fix | Delete
$this->setClientFilename($clientFilename);
[76] Fix | Delete
$this->setClientMediaType($clientMediaType);
[77] Fix | Delete
[78] Fix | Delete
if ($this->isOk()) {
[79] Fix | Delete
$this->setStreamOrFile($streamOrFile);
[80] Fix | Delete
}
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Depending on the value set file or stream variable
[85] Fix | Delete
*
[86] Fix | Delete
* @param mixed $streamOrFile
[87] Fix | Delete
*
[88] Fix | Delete
* @throws InvalidArgumentException
[89] Fix | Delete
*/
[90] Fix | Delete
private function setStreamOrFile($streamOrFile)
[91] Fix | Delete
{
[92] Fix | Delete
if (is_string($streamOrFile)) {
[93] Fix | Delete
$this->file = $streamOrFile;
[94] Fix | Delete
} elseif (is_resource($streamOrFile)) {
[95] Fix | Delete
$this->stream = new Stream($streamOrFile);
[96] Fix | Delete
} elseif ($streamOrFile instanceof StreamInterface) {
[97] Fix | Delete
$this->stream = $streamOrFile;
[98] Fix | Delete
} else {
[99] Fix | Delete
throw new InvalidArgumentException(
[100] Fix | Delete
'Invalid stream or file provided for UploadedFile'
[101] Fix | Delete
);
[102] Fix | Delete
}
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* @param int $error
[107] Fix | Delete
*
[108] Fix | Delete
* @throws InvalidArgumentException
[109] Fix | Delete
*/
[110] Fix | Delete
private function setError($error)
[111] Fix | Delete
{
[112] Fix | Delete
if (false === is_int($error)) {
[113] Fix | Delete
throw new InvalidArgumentException(
[114] Fix | Delete
'Upload file error status must be an integer'
[115] Fix | Delete
);
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
if (false === in_array($error, UploadedFile::$errors)) {
[119] Fix | Delete
throw new InvalidArgumentException(
[120] Fix | Delete
'Invalid error status for UploadedFile'
[121] Fix | Delete
);
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
$this->error = $error;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* @param int $size
[129] Fix | Delete
*
[130] Fix | Delete
* @throws InvalidArgumentException
[131] Fix | Delete
*/
[132] Fix | Delete
private function setSize($size)
[133] Fix | Delete
{
[134] Fix | Delete
if (false === is_int($size)) {
[135] Fix | Delete
throw new InvalidArgumentException(
[136] Fix | Delete
'Upload file size must be an integer'
[137] Fix | Delete
);
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
$this->size = $size;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* @param mixed $param
[145] Fix | Delete
*
[146] Fix | Delete
* @return bool
[147] Fix | Delete
*/
[148] Fix | Delete
private function isStringOrNull($param)
[149] Fix | Delete
{
[150] Fix | Delete
return in_array(gettype($param), ['string', 'NULL']);
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* @param mixed $param
[155] Fix | Delete
*
[156] Fix | Delete
* @return bool
[157] Fix | Delete
*/
[158] Fix | Delete
private function isStringNotEmpty($param)
[159] Fix | Delete
{
[160] Fix | Delete
return is_string($param) && false === empty($param);
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* @param string|null $clientFilename
[165] Fix | Delete
*
[166] Fix | Delete
* @throws InvalidArgumentException
[167] Fix | Delete
*/
[168] Fix | Delete
private function setClientFilename($clientFilename)
[169] Fix | Delete
{
[170] Fix | Delete
if (false === $this->isStringOrNull($clientFilename)) {
[171] Fix | Delete
throw new InvalidArgumentException(
[172] Fix | Delete
'Upload file client filename must be a string or null'
[173] Fix | Delete
);
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
$this->clientFilename = $clientFilename;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* @param string|null $clientMediaType
[181] Fix | Delete
*
[182] Fix | Delete
* @throws InvalidArgumentException
[183] Fix | Delete
*/
[184] Fix | Delete
private function setClientMediaType($clientMediaType)
[185] Fix | Delete
{
[186] Fix | Delete
if (false === $this->isStringOrNull($clientMediaType)) {
[187] Fix | Delete
throw new InvalidArgumentException(
[188] Fix | Delete
'Upload file client media type must be a string or null'
[189] Fix | Delete
);
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
$this->clientMediaType = $clientMediaType;
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* Return true if there is no upload error
[197] Fix | Delete
*
[198] Fix | Delete
* @return bool
[199] Fix | Delete
*/
[200] Fix | Delete
private function isOk()
[201] Fix | Delete
{
[202] Fix | Delete
return $this->error === UPLOAD_ERR_OK;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
/**
[206] Fix | Delete
* @return bool
[207] Fix | Delete
*/
[208] Fix | Delete
public function isMoved()
[209] Fix | Delete
{
[210] Fix | Delete
return $this->moved;
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* @throws RuntimeException if is moved or not ok
[215] Fix | Delete
*/
[216] Fix | Delete
private function validateActive()
[217] Fix | Delete
{
[218] Fix | Delete
if (false === $this->isOk()) {
[219] Fix | Delete
throw new RuntimeException('Cannot retrieve stream due to upload error');
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
if ($this->isMoved()) {
[223] Fix | Delete
throw new RuntimeException('Cannot retrieve stream after it has already been moved');
[224] Fix | Delete
}
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* {@inheritdoc}
[229] Fix | Delete
*
[230] Fix | Delete
* @throws RuntimeException if the upload was not successful.
[231] Fix | Delete
*/
[232] Fix | Delete
public function getStream()
[233] Fix | Delete
{
[234] Fix | Delete
$this->validateActive();
[235] Fix | Delete
[236] Fix | Delete
if ($this->stream instanceof StreamInterface) {
[237] Fix | Delete
return $this->stream;
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
return new LazyOpenStream($this->file, 'r+');
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
/**
[244] Fix | Delete
* {@inheritdoc}
[245] Fix | Delete
*
[246] Fix | Delete
* @see http://php.net/is_uploaded_file
[247] Fix | Delete
* @see http://php.net/move_uploaded_file
[248] Fix | Delete
*
[249] Fix | Delete
* @param string $targetPath Path to which to move the uploaded file.
[250] Fix | Delete
*
[251] Fix | Delete
* @throws RuntimeException if the upload was not successful.
[252] Fix | Delete
* @throws InvalidArgumentException if the $path specified is invalid.
[253] Fix | Delete
* @throws RuntimeException on any error during the move operation, or on
[254] Fix | Delete
* the second or subsequent call to the method.
[255] Fix | Delete
*/
[256] Fix | Delete
public function moveTo($targetPath)
[257] Fix | Delete
{
[258] Fix | Delete
$this->validateActive();
[259] Fix | Delete
[260] Fix | Delete
if (false === $this->isStringNotEmpty($targetPath)) {
[261] Fix | Delete
throw new InvalidArgumentException(
[262] Fix | Delete
'Invalid path provided for move operation; must be a non-empty string'
[263] Fix | Delete
);
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
if ($this->file) {
[267] Fix | Delete
$this->moved = php_sapi_name() == 'cli'
[268] Fix | Delete
? rename($this->file, $targetPath)
[269] Fix | Delete
: move_uploaded_file($this->file, $targetPath);
[270] Fix | Delete
} else {
[271] Fix | Delete
Utils::copyToStream(
[272] Fix | Delete
$this->getStream(),
[273] Fix | Delete
new LazyOpenStream($targetPath, 'w')
[274] Fix | Delete
);
[275] Fix | Delete
[276] Fix | Delete
$this->moved = true;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
if (false === $this->moved) {
[280] Fix | Delete
throw new RuntimeException(
[281] Fix | Delete
sprintf('Uploaded file could not be moved to %s', $targetPath)
[282] Fix | Delete
);
[283] Fix | Delete
}
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
/**
[287] Fix | Delete
* {@inheritdoc}
[288] Fix | Delete
*
[289] Fix | Delete
* @return int|null The file size in bytes or null if unknown.
[290] Fix | Delete
*/
[291] Fix | Delete
public function getSize()
[292] Fix | Delete
{
[293] Fix | Delete
return $this->size;
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
/**
[297] Fix | Delete
* {@inheritdoc}
[298] Fix | Delete
*
[299] Fix | Delete
* @see http://php.net/manual/en/features.file-upload.errors.php
[300] Fix | Delete
*
[301] Fix | Delete
* @return int One of PHP's UPLOAD_ERR_XXX constants.
[302] Fix | Delete
*/
[303] Fix | Delete
public function getError()
[304] Fix | Delete
{
[305] Fix | Delete
return $this->error;
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
/**
[309] Fix | Delete
* {@inheritdoc}
[310] Fix | Delete
*
[311] Fix | Delete
* @return string|null The filename sent by the client or null if none
[312] Fix | Delete
* was provided.
[313] Fix | Delete
*/
[314] Fix | Delete
public function getClientFilename()
[315] Fix | Delete
{
[316] Fix | Delete
return $this->clientFilename;
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
/**
[320] Fix | Delete
* {@inheritdoc}
[321] Fix | Delete
*/
[322] Fix | Delete
public function getClientMediaType()
[323] Fix | Delete
{
[324] Fix | Delete
return $this->clientMediaType;
[325] Fix | Delete
}
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function