Fileupload class
Posted: Fri Jun 18, 2004 4:40 pm
I am starting to write my own upload class but am having a problem. This is nowhere near finished but, I get this error when starting to test it
Notice: Array to string conversion in sw_class_upload.php on line 14
sw_class_upload.phpThe formCalling the scriptAny help with whats wrong appreciated. I know there are lots of upload utilities out there but I would prefer to write my own as a learning experience
Notice: Array to string conversion in sw_class_upload.php on line 14
sw_class_upload.php
Code: Select all
<?php
class FileUpload
{
var $index; //
var $temp_file_name; //Temporary file name
var $file_name; //Preview file upload directory
var $upload_dir; //Directory to upload to
//Constructor method - pass the $_FILES index and upload directory name
function FileUpload($arr_index, $upload_dir)
{
$this->index = $arr_index;
$this->temp_file_name = $_FILES[$this->index]['tmp_name'];
$this->file_name = strtolower($_FILES[$this->index]['name']);
$this->upload_dir = $upload_dir;
}
//Do the actual upload
function move_file()
{
if ( move_uploaded_file($_FILES[$this->index]['tmp_name'], $this->upload_dir . $this->file_name) )
{
return true;
} else {
return false;
}
}
//Rename the uploaded file using the ID from the DB insert
//Using a hardcoded name for testing still
function FileRename($file)
{
rename($file, 'abc.jpg');
}
}
?>Code: Select all
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SW_FileUpload Class Implementation Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
require 'sw_class_upload.php';
?>
<form action="sw_test.php" method="post" enctype="multipart/form-data">
<!-- MAX_FILE_SIZE set to 5 MB -->
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input type="file" name="userfile[]" size="30" /><br />
<input type="file" name="userfile[]" size="30" /><br />
<input type="submit" value="Send files" />
</form>
</body>
</html>Code: Select all
<?php
error_reporting(E_ALL);
require 'sw_class_upload.php';
$fu = new FileUpload('userfile','upload');
print_r($_FILES);
?>