Skip to content

Commit

Permalink
get_tmp_name
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdual committed Aug 22, 2020
1 parent 0b80b2e commit 1f1e539
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
47 changes: 28 additions & 19 deletions src/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

class Uploader
{
const ERR_EMPTY_FILE = 1;
const ERR_INVALID_EXT = 2;
const ERR_INVALID_TYPE = 3;
const ERR_LONG_SIZE = 4;
const ERR_SMALL_SIZE = 5;
const ERR_EMPTY_FILE = 1;
const ERR_INVALID_EXT = 2;
const ERR_INVALID_TYPE = 3;
const ERR_LONG_SIZE = 4;
const ERR_SMALL_SIZE = 5;
const ERR_UNKNOWN_ERROR = 6;
const ERR_NOT_AN_IMAGE = 7;
const ERR_NOT_AN_IMAGE = 7;
const ERR_MAX_DIMENSION = 8;
const ERR_MIN_DIMENSION = 9;

Expand All @@ -46,13 +46,13 @@ class Uploader
* @var array
*/
private $error_messages = array(
self::ERR_EMPTY_FILE => "No file selected.",
self::ERR_INVALID_EXT => "Invalid file extension.",
self::ERR_INVALID_TYPE => "Invalid file mime type.",
self::ERR_LONG_SIZE => "File size is too large.",
self::ERR_SMALL_SIZE => "File size is too small.",
self::ERR_EMPTY_FILE => "No file selected.",
self::ERR_INVALID_EXT => "Invalid file extension.",
self::ERR_INVALID_TYPE => "Invalid file mime type.",
self::ERR_LONG_SIZE => "File size is too large.",
self::ERR_SMALL_SIZE => "File size is too small.",
self::ERR_UNKNOWN_ERROR => "Unknown error occurred.",
self::ERR_NOT_AN_IMAGE => "The selected file must be an image.",
self::ERR_NOT_AN_IMAGE => "The selected file must be an image.",
self::ERR_MAX_DIMENSION => "The dimensions of the file is too large.",
self::ERR_MIN_DIMENSION => "The dimensions of the file is too small."
);
Expand Down Expand Up @@ -323,18 +323,27 @@ public function get_name()
}

if ($this->encrypt_name) {
$this->name = self::hashed($this->name) . $this->get_ext($this->file["name"], true);
$this->name = self::hashed($this->name) . self::get_ext($this->file["name"], true);
$this->auto_extension = false;
$this->encrypt_name = false;
}

if ($this->auto_extension) {
return pathinfo($this->name, PATHINFO_FILENAME) . $this->get_ext($this->file["name"], true);
return pathinfo($this->name, PATHINFO_FILENAME) . self::get_ext($this->file["name"], true);
} else {
return $this->name;
}
}

/**
* Get the name of the temporary file
* @return string
*/
public function get_tmp_name()
{
return isset($this->file["tmp_name"]) ? $this->file["tmp_name"] : null;
}

/**
* Check the file can be uploaded
* @return boolean
Expand All @@ -350,17 +359,17 @@ public function check()
$this->error = self::ERR_EMPTY_FILE;
} else if (strlen($this->file["name"]) == 0 || strlen($this->file["tmp_name"]) == 0 || strlen($this->file["type"]) == 0 || $this->file["size"] == 0) {
$this->error = self::ERR_EMPTY_FILE;
} else if ($this->extensions !== null && !in_array($this->get_ext($this->file["name"]), $this->extensions)) {
} else if ($this->extensions !== null && !in_array(self::get_ext($this->file["name"]), $this->extensions)) {
$this->error = self::ERR_INVALID_EXT;
} else if ($this->disallowed_extensions !== null && in_array($this->get_ext($this->file["name"]), $this->disallowed_extensions)) {
} else if ($this->disallowed_extensions !== null && in_array(self::get_ext($this->file["name"]), $this->disallowed_extensions)) {
$this->error = self::ERR_INVALID_EXT;
} else if ($this->types !== null && !in_array($this->file["type"], $this->types)) {
$this->error = self::ERR_INVALID_TYPE;
} else if ($this->disallowed_types !== null && in_array($this->file["type"], $this->disallowed_types)) {
$this->error = self::ERR_INVALID_TYPE;
} else if ($this->max_size !== null && $this->file["size"] > $this->mb_to_byte($this->max_size)) {
} else if ($this->max_size !== null && $this->file["size"] > self::mb_to_byte($this->max_size)) {
$this->error = self::ERR_LONG_SIZE;
} else if ($this->min_size !== null && $this->file["size"] < $this->mb_to_byte($this->min_size)) {
} else if ($this->min_size !== null && $this->file["size"] < self::mb_to_byte($this->min_size)) {
$this->error = self::ERR_SMALL_SIZE;
} else if ($this->file["error"] == 1 || $this->file["error"] == 2) {
$this->error = self::ERR_LONG_SIZE;
Expand Down Expand Up @@ -552,7 +561,7 @@ public static function create_temp_file($source, $mime_map = [])
$ext = array_pop($split);
}

register_shutdown_function(function() use($temp) {
register_shutdown_function(function() use ($temp) {
fclose($temp);
});

Expand Down
11 changes: 8 additions & 3 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public function testGetName1()
{
$upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]);
$upload->name("foo");
$upload->check();

$this->assertEquals($upload->get_name(), "foo.jpg");
}
Expand All @@ -18,8 +17,14 @@ public function testGetName2()
$upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]);
$upload->name("foo");
$upload->encrypt_name();
$upload->check();

$this->assertNotEquals($upload->get_name(), "foo.jpg");
}
}

public function testGetTmpName()
{
$upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]);

$this->assertNotNull($upload->get_tmp_name());
}
}

0 comments on commit 1f1e539

Please sign in to comment.