Example #1
0
 public function test_file()
 {
     $file1 = self::getDir() . '/dir1/foo.txt';
     $file2 = self::getDir() . '/dir2/bar.txt';
     $file3 = self::getDir() . '/dir2/baz.txt';
     $file3_name = 'baz.txt';
     $file4 = self::getDir() . '/dir4/yolo.txt';
     $this->assertFalse(file_exists($file1));
     file_create($file1);
     file_write($file1, '');
     $this->assertTrue(file_exists($file1));
     $this->assertEquals('', file_read($file1));
     file_write($file1, 'foo');
     $this->assertEquals('foo', file_read($file1));
     file_append($file1, '_bar');
     $this->assertEquals('foo_bar', file_read($file1));
     file_prepend($file1, '#');
     $this->assertEquals('#foo_bar', file_read($file1));
     file_append($file1, '_bar');
     file_prepend($file1, '#');
     $this->assertEquals('##foo_bar_bar', file_read($file1));
     file_append($file1 . '_append', '_bar');
     $this->assertEquals(file_read($file1 . '_append'), '_bar');
     file_prepend($file1 . '_prepend', '#');
     $this->assertEquals(file_read($file1 . '_prepend'), '#');
     $this->assertFalse(file_exists($file2));
     file_copy($file1, $file2);
     $this->assertTrue(file_exists($file2));
     $this->assertEquals(file_read($file1), file_read($file2));
     $this->assertFalse(file_exists($file3));
     file_rename($file2, $file3_name);
     $this->assertFalse(file_exists($file2));
     $this->assertTrue(file_exists($file3));
     $this->assertEquals(file_read($file1), file_read($file3));
     $this->assertFalse(file_exists($file4));
     file_move($file3, $file4);
     $this->assertFalse(file_exists($file3));
     $this->assertTrue(file_exists($file4));
     $this->assertEquals(file_read($file1), file_read($file4));
     file_delete($file4);
     file_delete($file4);
     $this->assertFalse(file_exists($file4));
     $this->assertEquals(self::getDir() . '/dir1', file_get_directory($file1));
     $this->assertEquals('txt', file_get_extension($file1));
     $this->assertEquals('foo.txt', file_get_name($file1));
 }
Example #2
0
function toStorage($file, $settings = array())
{
    @($naming = $settings['naming'] ? $settings['naming'] : "%s");
    @($ext = $settings['ext'] ? $settings['ext'] : "");
    @($delete = $settings['delete'] ? $settings['delete'] : false);
    @($subdir = $settings['subdir'] ? $settings['subdir'] : false);
    @($dir = $settings['dir'] ? $settings['dir'] : false);
    $add = 0;
    clearstatcache();
    $dir = rtrim($dir, '/');
    if (host($file)) {
        $tempDirectory = rtrim(`mktemp -d`);
        $content = curl($file);
        if (!is_string($content)) {
            _warn(__FUNCTION__, 'INVALID FILE (URL)!');
            return null;
        }
        $path = parse_url($file, PHP_URL_PATH);
        $name = basename($path);
        if (!$name) {
            $name = mt_rand() . '.tmp';
        }
        $file = "{$tempDirectory}/{$name}";
        file_put_contents($file, $content);
    }
    // $dir - это каталог! $file - это файл!
    if (!is_file($file) or !is_dir($dir)) {
        _warn(__FUNCTION__, 'INVALID FILE OR DIRECTORY!');
        return null;
    }
    $hash = function ($_) {
        return hash('sha256', $_, false);
    };
    //
    $name = file_get_name($file);
    $ext = $ext ? $ext : file_get_ext($file);
    if ($ext) {
        $ext = ".{$ext}";
    }
    //
    if (!$name) {
        $name = mt_rand();
    }
    $name = str_replace(' ', '-', $name);
    if (is_callable($naming)) {
        $name = $naming($name);
    } elseif (is_string($naming)) {
        $name = sprintf($naming, $name);
    }
    $name = preg_replace('~\\W+~', ' ', $name);
    $name = preg_replace('~\\s+~', '-', $name);
    // $name = Normalizer::go($name, 'tr,latinRu,en,hyphen');
    if (!$name) {
        $name = mt_rand();
    }
    //
    $fileHash = $hash(file_get_contents($file));
    if (!$subdir) {
        $subdir = rand_from_string($fileHash) % 1000;
    }
    if (!is_dir($_ = $dir . '/' . $subdir)) {
        mkdir($_, 0755);
    }
    $_ = "/{$subdir}/{$name}{$ext}";
    while (true) {
        if (is_file($dir . $_) and $hash(file_get_contents($dir . $_)) === $fileHash) {
            if ($delete) {
                unlink($file);
            }
            if (isset($tempDirectory)) {
                `rm -rf '{$tempDirectory}'`;
            }
            return $_;
        }
        if (!is_file($dir . $_)) {
            file_put_contents($dir . $_, file_get_contents($file));
            if ($delete) {
                unlink($file);
            }
            if (isset($tempDirectory)) {
                `rm -rf '{$tempDirectory}'`;
            }
            return $_;
        }
        $_ = "/{$subdir}/{$name}-{$add}{$ext}";
        $add += 1;
    }
}
Example #3
0
File: core.php Project: ejz/core
function toStorage($file, $settings = array())
{
    $dir = isset($settings['dir']) ? $settings['dir'] : false;
    clearstatcache();
    $delete = isset($settings['delete']) ? $settings['delete'] : false;
    // $dir - это каталог! $file - это файл!
    if (!is_file($file) or !is_dir($dir)) {
        _log('INVALID FILE OR DIRECTORY!', E_USER_WARNING);
        return null;
    }
    $dir = rtrim($dir, '/');
    $defaultName = file_get_name($file);
    $defaultExt = file_get_ext($file);
    $name = isset($settings['name']) ? $settings['name'] : $defaultName;
    $ext = isset($settings['ext']) ? $settings['ext'] : $defaultExt;
    //
    $name = preg_replace('~[^a-zA-Z0-9\\.-]~', ' ', $name);
    $name = preg_replace('~\\s+~', '-', $name);
    if (!$name) {
        $name = mt_rand();
    }
    $target = $dir . '/' . $name . ($ext ? ".{$ext}" : "");
    $i = 0;
    $initialTarget = $target;
    while (file_exists($target)) {
        $target = preg_replace($i ? '~-\\d+(\\.\\w+)$~' : '~(\\.\\w+)$~', '-' . ++$i . '$1', $target);
    }
    if ($delete) {
        rename($file, $target);
    } else {
        copy($file, $target);
    }
    if ($initialTarget != $target and md5_file($initialTarget) === md5_file($target)) {
        unlink($target);
        $target = $initialTarget;
    }
    return $target;
}
Example #4
0
 public function testToStorage()
 {
     $tmp = rtrim(`mktemp --suffix=.png`);
     $dir = rtrim(`mktemp -d`);
     file_put_contents($tmp, $rand = mt_rand());
     $target = toStorage($tmp, array('dir' => $dir));
     $this->assertTrue(file_get_name($tmp) === file_get_name($target));
     $this->assertTrue(file_get_ext($tmp) === file_get_ext($target));
     $this->assertEquals($rand, file_get_contents($target));
     $this->assertTrue(is_file($tmp));
     //
     file_put_contents($tmp, mt_rand());
     $target_1 = toStorage($tmp, array('dir' => $dir));
     $this->assertTrue(file_get_name($target) . "-1" === file_get_name($target_1));
     //
     file_put_contents($tmp, $rand);
     $target_2 = toStorage($tmp, array('dir' => $dir));
     $this->assertTrue($target_2 === $target);
     //
     file_put_contents($tmp, $rand = mt_rand());
     $target = toStorage($tmp, array('dir' => $dir, 'ext' => 'bla', 'delete' => true));
     $this->assertTrue(file_get_name($tmp) === file_get_name($target));
     $this->assertTrue('bla' === file_get_ext($target));
     $this->assertEquals($rand, file_get_contents($target));
     $this->assertTrue(!is_file($tmp));
 }