Upload file / rename
Ik heb een website waarop 4 banners worden getoond. Via een uploadfunctie wil ik de banners kunnen wijzigen. Dit kan door middel van het uploaden van een nieuwe banner. Het is de bedoeling dat de oude banner dan 'overschreven' wordt. Echter krijg ik de naam van het geüploade bestand niet aangepast.
Dit is de class die ik gebruik:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class uploadClass {
var $uploadDir;
var $customName;
var $errors;
var $ext;
var $checkExt;
function uploadClass ( $dir )
{
$this->errors = array ();
$this->customName = FALSE;
$this->checkExt = FALSE;
if ( is_writable ( $dir ) && file_exists ( $dir ) )
$this->uploadDir = $dir;
else
$this->errorLog ( $dir . ' is niet schrijfbaar, of bestaat niet', TRUE );
}
function uploadFile ( $name ) {
if ( IsSet ( $_FILES [ $name ] ) && is_uploaded_file ( $_FILES [ $name ][ 'tmp_name' ] ) )
{
if ( $this->extCheck ( $_FILES [ $name ][ 'name' ] ) )
{
if ( $this->customName )
{
if ( copy ( $_FILES [ $name ][ 'tmp_name' ], $this->uploadDdir . $this->custom_name ) )
return TRUE;
else
return FALSE;
}
else
{
if ( copy ( $_FILES [ $name ][ 'tmp_name' ], $this->uploadDir . $_FILES [ $name ][ 'name' ] ) )
return TRUE;
else
return FALSE;
}
}
}
else
$this->errorLog ( $name . 'bestaat niet als upload bestand', TRUE );
return FALSE;
}
function possibleExtensions ( $extensions ) {
$this->checkExt = TRUE;
$temp = explode ( ',', $extensions );
if ( Empty ( $this->ext[0] ) )
$this->ext = $temp;
else
array_merge ( $this->ext, $tmp );
}
function extCheck ( $filename ) {
if ($this->checkExt)
{
$tmp = explode ( ".", $filename );
if ( in_array ( end($tmp), $this->ext))
return TRUE;
else
return FALSE;
}
else
return TRUE;
}
function setName ( $name )
{
$this->custom_name = $name;
}
function errorLog ( $error, $die = FALSE )
{
$this->errors[] = $error;
if ( $die )
die ( print_r ( $this->errors ) );
}
}
var $uploadDir;
var $customName;
var $errors;
var $ext;
var $checkExt;
function uploadClass ( $dir )
{
$this->errors = array ();
$this->customName = FALSE;
$this->checkExt = FALSE;
if ( is_writable ( $dir ) && file_exists ( $dir ) )
$this->uploadDir = $dir;
else
$this->errorLog ( $dir . ' is niet schrijfbaar, of bestaat niet', TRUE );
}
function uploadFile ( $name ) {
if ( IsSet ( $_FILES [ $name ] ) && is_uploaded_file ( $_FILES [ $name ][ 'tmp_name' ] ) )
{
if ( $this->extCheck ( $_FILES [ $name ][ 'name' ] ) )
{
if ( $this->customName )
{
if ( copy ( $_FILES [ $name ][ 'tmp_name' ], $this->uploadDdir . $this->custom_name ) )
return TRUE;
else
return FALSE;
}
else
{
if ( copy ( $_FILES [ $name ][ 'tmp_name' ], $this->uploadDir . $_FILES [ $name ][ 'name' ] ) )
return TRUE;
else
return FALSE;
}
}
}
else
$this->errorLog ( $name . 'bestaat niet als upload bestand', TRUE );
return FALSE;
}
function possibleExtensions ( $extensions ) {
$this->checkExt = TRUE;
$temp = explode ( ',', $extensions );
if ( Empty ( $this->ext[0] ) )
$this->ext = $temp;
else
array_merge ( $this->ext, $tmp );
}
function extCheck ( $filename ) {
if ($this->checkExt)
{
$tmp = explode ( ".", $filename );
if ( in_array ( end($tmp), $this->ext))
return TRUE;
else
return FALSE;
}
else
return TRUE;
}
function setName ( $name )
{
$this->custom_name = $name;
}
function errorLog ( $error, $die = FALSE )
{
$this->errors[] = $error;
if ( $die )
die ( print_r ( $this->errors ) );
}
}
En op deze manier roep ik de upload op:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
$upload = new uploadClass( $_SERVER ['DOCUMENT_ROOT'] . '/coh/upload/banner/' );
$upload->possibleExtensions ( 'jpg' );
$upload->setName ('banner'.$_POST['id'].'.jpg');
if ( $upload->uploadFile ( 'bestand' ) ) {
echo 'Upload OK!<br />';
}
else {
echo 'Upload error!<br />';
}
$upload->possibleExtensions ( 'jpg' );
$upload->setName ('banner'.$_POST['id'].'.jpg');
if ( $upload->uploadFile ( 'bestand' ) ) {
echo 'Upload OK!<br />';
}
else {
echo 'Upload error!<br />';
}
Het bestand wordt succesvol geüpload, echter krijgt deze de originele naam mee. Ik wil de naam dus veranderen banner?.jpg
Iemand enig idee wat ik hier fout doe?
Alvast bedankt!
Er zijn nog geen reacties op dit bericht.