Skip to content

Commit

Permalink
Merge pull request #1 from MohamedAmine-C/analysis-qBPgdA
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
Mohamed Amine authored Oct 14, 2016
2 parents 7944ff2 + 8f00766 commit aab5874
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 222 deletions.
6 changes: 4 additions & 2 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
}
$fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php';
$fileName = __DIR__.DIRECTORY_SEPARATOR.$fileName.$className.'.php';
if (file_exists($fileName)) {
require $fileName;

return true;
}

return false;
});
38 changes: 17 additions & 21 deletions testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@
// Single file upload test

if (isset($_POST['submit'])) {
$file = new upload\File('myfile');

$file = new upload\File('myfile');
$file->setName('MyNewFileName');
$file->size(1000000)->Extension('jpg,png,jpeg')->Exist();
$file->Compress(75);

$file->setName("MyNewFileName");
$file->size(1000000)->Extension('jpg,png,jpeg')->Exist();
$file->Compress(75);
$data = $file->Upload($_SERVER['DOCUMENT_ROOT']);

$data = $file->Upload($_SERVER["DOCUMENT_ROOT"]);

if (empty($file->Errors)) {
var_dump($data);
if (empty($file->Errors)) {
var_dump($data);
/*
$data['name'] -> uploaded file name
$data['dir'] -> uploaded file directory in server
*/
} else {
var_dump($file->Errors);
}

} else {
var_dump($file->Errors);
}
}

?>
Expand All @@ -35,26 +33,24 @@
// Multi file upload test

if (isset($_POST['submit2'])) {
$files = new upload\MultiFile('myfiles');

$files = new upload\MultiFile('myfiles');
$files->Size(99900999)->Extension('png,jpg')->Compress(70);

$files->Size(99900999)->Extension('png,jpg')->Compress(70);
$data = $files->Upload($_SERVER['DOCUMENT_ROOT']);

$data = $files->Upload($_SERVER["DOCUMENT_ROOT"]);

if (in_array(false, $data)) { // if there is some error
if (in_array(false, $data)) { // if there is some error
$Errors = $files->getErrors();
var_dump($Errors);
} else { // if every thing went right
var_dump($Errors);
} else { // if every thing went right
var_dump($data);
/*
$data[0]['name'] -> first uploaded file name
$data[1]['name'] -> second uploaded file name
$data[0]['dir'] -> first uploaded file directory in server
$data[1]['dir'] -> second uploaded file directory in server
*/
}

}
}
?>

Expand Down
61 changes: 31 additions & 30 deletions upload/File.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
<?php

namespace upload;

class File extends Validate
{

function __construct($key, $File = false)
{
if (!$File) {
$File = array(
'name' => $_FILES[$key]['name'],
'tmp_name' => $_FILES[$key]['tmp_name']
);
public function __construct($key, $File = false)
{
if (!$File) {
$File = [
'name' => $_FILES[$key]['name'],
'tmp_name' => $_FILES[$key]['tmp_name'],
];
}
$FilePath = $File['tmp_name'];
$FileName = explode('.', $File['name']);
$this->setName($FileName[0]);
$this->setExtension(strtolower(end($FileName)));
parent::__construct($FilePath);
}
$FilePath = $File['tmp_name'];
$FileName = explode(".", $File["name"]);
$this->setName($FileName[0]);
$this->setExtension(strtolower(end($FileName)));
parent::__construct($FilePath);
}

public function Upload($newPath)
{
$newPath = $_SERVER["DOCUMENT_ROOT"].'/'.$newPath;
if (is_dir($newPath) && is_writable($newPath)) {
if (empty($this->Errors)) {
$Path = $newPath.DIRECTORY_SEPARATOR.$this->getName().'.'.$this->getExtension();
move_uploaded_file($this->getTmpName(), $Path);
return array('name' => $this->getName().'.'.$this->getExtension(), 'dir' => $Path);
} else {
return false;
}
} else {
$this->Errors[] = "No access to this directory $newPath";
return false;
}
}
public function Upload($newPath)
{
$newPath = $_SERVER['DOCUMENT_ROOT'].'/'.$newPath;
if (is_dir($newPath) && is_writable($newPath)) {
if (empty($this->Errors)) {
$Path = $newPath.DIRECTORY_SEPARATOR.$this->getName().'.'.$this->getExtension();
move_uploaded_file($this->getTmpName(), $Path);

return ['name' => $this->getName().'.'.$this->getExtension(), 'dir' => $Path];
} else {
return false;
}
} else {
$this->Errors[] = "No access to this directory $newPath";

return false;
}
}
}
130 changes: 64 additions & 66 deletions upload/FileInfo.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@
<?php

namespace upload;

class FileInfo implements FileInfoInterface
{

private $Name;
private $Extension;
private $Size;
private $TmpName;

function __construct($FilePath)
{
$this->Name = $this->Name ? $this->Name : pathinfo($FilePath, PATHINFO_FILENAME);
$this->Size = filesize($FilePath);
$this->TmpName = $FilePath;
}

public function getName()
{
return $this->Name;
}

public function setName($newName)
{
$this->Name = $newName;
}

public function getSize($File = false)
{
return $this->Size;
}

public function getExtension($File = false)
{
return $this->Extension;
}

public function setExtension($newExtension)
{
$this->Extension = $newExtension;
}

public function getTmpName()
{
return $this->TmpName;
}

public function setTmpName($TmpName)
{
$this->TmpName = $TmpName;
}

public function Compress($quality)
{
$info = getimagesize($this->TmpName);

if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($this->TmpName);

elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($this->TmpName);

elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($this->TmpName);

imagejpeg($image, $this->TmpName, $quality);

return $this->TmpName;
}

private $Name;
private $Extension;
private $Size;
private $TmpName;

public function __construct($FilePath)
{
$this->Name = $this->Name ? $this->Name : pathinfo($FilePath, PATHINFO_FILENAME);
$this->Size = filesize($FilePath);
$this->TmpName = $FilePath;
}

public function getName()
{
return $this->Name;
}

public function setName($newName)
{
$this->Name = $newName;
}

public function getSize($File = false)
{
return $this->Size;
}

public function getExtension($File = false)
{
return $this->Extension;
}

public function setExtension($newExtension)
{
$this->Extension = $newExtension;
}

public function getTmpName()
{
return $this->TmpName;
}

public function setTmpName($TmpName)
{
$this->TmpName = $TmpName;
}

public function Compress($quality)
{
$info = getimagesize($this->TmpName);

if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($this->TmpName);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($this->TmpName);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($this->TmpName);
}

imagejpeg($image, $this->TmpName, $quality);

return $this->TmpName;
}
}
21 changes: 14 additions & 7 deletions upload/FileInfoInterface.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<?php

namespace upload;

interface FileInfoInterface
{
public function getName();
public function setName($newName);
public function getSize();
public function getExtension();
public function setExtension($newExtension);
public function getTmpName();
public function setTmpName($tmpName);
public function getName();

public function setName($newName);

public function getSize();

public function getExtension();

public function setExtension($newExtension);

public function getTmpName();

public function setTmpName($tmpName);
}
Loading

0 comments on commit aab5874

Please sign in to comment.