When a person selects an image they will send the selected one, but if not select choose a pattern. How can I do this?
I was making a code to upload more how can I add this functionality?
$up = new Upload("file");
if(!is_dir($path)):
mkdir($path, 0755, true);
endif;
$up->setDir( $path )->setExtension( array( 'jpeg','jpg','png', 'gif' ) )->setSize( 2 );
@$up->upload();
<?php
class Upload
{
private $file = array();
public $dir;
public $extension = array();
public $size;
public $name = array();
public function __construct( $file )
{
$this->file = $_FILES[ $file ];
}
public function upload()
{
$this->checkExtension()->size()->rename();
if( is_array( $this->file[ "name" ] ) && !empty( $this->file[ "name" ] ) )
{
foreach( $this->file[ "error" ] as $key => $error )
{
if( $error == UPLOAD_ERR_OK || !empty( $this->file[ "name" ][ $key ] ) )
{
move_uploaded_file( $this->file[ "tmp_name" ][ $key ], $this->getDir() . $this->file[ "name" ][ $key ] );
$this->name[] = $this->file[ "name" ][ $key ];
}
}
}
elseif( !empty( $this->file[ "name" ] ) )
{
move_uploaded_file( $this->file[ "tmp_name" ], $this->getDir() . $this->file[ "name" ] );
$this->name[] = $this->file[ "name" ];
}
return $this->name;
}
public function checkExtension()
{
if( is_array( $this->extension ) )
{
$extensions = implode( "|", $this->extension );
if( is_array( $this->file[ "name" ] ) )
{
foreach( $this->file[ "name" ] as $key => $val )
{
if( !preg_match( "/.+\.({$extensions})/", $val ) )
{
$this->file[ "name" ][ $key ] = "";
}
}
}
else
{
if( !preg_match( "/.+\.({$extensions})/", $this->file[ "name" ] ) )
{
unset( $this->file[ "name" ] );
}
}
}
return $this;
}
public function size()
{
$size = $this->convertMbToBt();
if( is_array( $this->file[ "size" ] ) )
{
foreach( $this->file[ "size" ] as $key => $sizes )
{
if( $sizes > $size )
{
$this->file[ "name" ][ $key ] = "";
$this->file[ "size" ][ $key ] = "";
}
}
}
else
{
if( $this->file[ "size" ] > $size )
{
unset( $this->file[ "size" ] );
}
}
return $this;
}
private function convertMbToBt()
{
$size = $this->getSize() * ( 1024 * 1024 );
return $size;
}
protected function rename()
{
if( is_array( $this->file[ "name" ] ) )
{
foreach( $this->file[ "name" ] as $key => $val )
{
if( !empty( $this->file[ "name" ][ $key ] ) )
{
$exts = preg_split( "[\.]", $this->file[ "name" ][ $key ] );
$n = count( $exts ) - 1;
$exts = $exts[ $n ];
$this->file[ "name" ] = time() . uniqid() . md5($this->file['name']) . "." . $exts;
}
else
{
$this->file[ "name" ][ $key ] = "";
}
}
}
else
{
$exts = preg_split( "[\.]", $this->file[ "name" ] );
$n = count( $exts ) - 1;
$exts = $exts[ $n ];
$this->file[ "name" ] = time() . uniqid() . md5($this->file['name']) . "." . $exts;
}
return $this;
}
public function getDir()
{
return $this->dir;
}
public function setDir( $dir )
{
$this->dir = $dir;
return $this;
}
public function getExtension()
{
return $this->extension;
}
public function setExtension( $extension )
{
$this->extension = $extension;
return $this;
}
public function getSize()
{
return $this->size;
}
public function setSize( $size )
{
$this->size = $size;
return $this;
}
public function setName( $nomeFim )
{
$this->size = $size;
return $this;
}
}
?>