Skip to content
This repository was archived by the owner on May 5, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This component simplifies file validation and uploading.

## Usage

See http://flysystem.thephpleague.com/ for information on adapters.

Assume a file is uploaded with this HTML form:

```html
Expand All @@ -17,7 +19,10 @@ When the HTML form is submitted, the server-side PHP code can validate and uploa

```php
<?php
$storage = new \Upload\Storage\FileSystem('/path/to/directory');
use \League\Flysystem\Filesystem;
use \League\Flysystem\Adapter\Local as Adapter;

$storage = new Filesystem(new Adapter(__DIR__.'/path/to/directory'));
$file = new \Upload\File('foo', $storage);

// Optionally you can rename the file on upload
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"require": {
"php": ">=5.3.0",
"ext-fileinfo": "*"
"ext-fileinfo": "*",
"league/flysystem": "0.5.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
Expand Down
23 changes: 15 additions & 8 deletions src/Upload/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class File implements \ArrayAccess, \IteratorAggregate, \Countable

/**
* Storage delegate
* @var \Upload\StorageInterface
* @var \League\Flysystem\Filesystem
*/
protected $storage;

Expand Down Expand Up @@ -109,12 +109,12 @@ class File implements \ArrayAccess, \IteratorAggregate, \Countable
/**
* Constructor
*
* @param string $key The $_FILES[] key
* @param \Upload\StorageInterface $storage The upload delegate instance
* @throws \RuntimeException If file uploads are disabled in the php.ini file
* @throws \InvalidArgumentException If $_FILES[] does not contain key
* @param string $key The $_FILES[] key
* @param \League\Flysystem\Filesystem $storage The upload delegate instance
* @throws \RuntimeException If file uploads are disabled in the php.ini file
* @throws \InvalidArgumentException If $_FILES[] does not contain key
*/
public function __construct($key, \Upload\StorageInterface $storage)
public function __construct($key, \League\Flysystem\Filesystem $storage)
{
// Check if file uploads are allowed
if (ini_get('file_uploads') == false) {
Expand Down Expand Up @@ -372,19 +372,26 @@ public function __call($name, $arguments)
/**
* Upload file (delegated to storage object)
*
* @param bool $overwrite If file should overwrite existing file.
* @return bool
* @throws \Upload\Exception If validation fails
* @throws \Upload\Exception If upload fails
*/
public function upload()
public function upload($overwrite = false)
{
if ($this->isValid() === false) {
throw new \Upload\Exception('File validation failed');
}

foreach ($this->objects as $fileInfo) {
$this->applyCallback('beforeUpload', $fileInfo);
$this->storage->upload($fileInfo);

if ($overwrite === false && $this->storage->has($fileInfo->getNameWithExtension())) {
throw new \Upload\Exception('File already exists', $fileInfo);
}

$this->storage->put($fileInfo->getNameWithExtension(), $fileInfo->getContents());

$this->applyCallback('afterUpload', $fileInfo);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Upload/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public function __construct($filePathname, $newName = null)
parent::__construct($filePathname);
}

/**
* Get the file contents
*
* @return string
*/
public function getContents()
{
return file_get_contents($this->getRealPath());
}

/**
* Get file name (without extension)
*
Expand Down
2 changes: 2 additions & 0 deletions src/Upload/FileInfoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface FileInfoInterface
{
public function getPathname();

public function getContents();

public function getName();

public function setName($name);
Expand Down
109 changes: 0 additions & 109 deletions src/Upload/Storage/FileSystem.php

This file was deleted.

10 changes: 6 additions & 4 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use League\Flysystem\Adapter\Local as Adapter;

class FileTest extends PHPUnit_Framework_TestCase
{
public function setUp()
Expand All @@ -23,9 +25,9 @@ public function setUp()

// Mock storage
$this->storage = $this->getMock(
'\Upload\Storage\FileSystem',
'\League\Flysystem\Filesystem',
array('upload'),
array($this->assetsDirectory)
array(new Adapter($this->assetsDirectory))
);
$this->storage
->expects($this->any())
Expand Down Expand Up @@ -121,7 +123,7 @@ public function testCallbacks()
$file->afterValidate($callbackAfterValidate);
$file->beforeUpload($callbackBeforeUpload);
$file->afterUpload($callbackAfterUpload);
$file->upload();
$file->upload(true);
}

/********************************************************************************
Expand Down Expand Up @@ -231,7 +233,7 @@ public function testWillUploadIfValid()
{
$file = new \Upload\File('single', $this->storage);
$this->assertTrue($file->isValid());
$this->assertTrue($file->upload());
$this->assertTrue($file->upload(true));
}

/**
Expand Down
80 changes: 0 additions & 80 deletions tests/Storage/FileSystemTest.php

This file was deleted.