Page 1 of 1

Fileupload class

Posted: Fri Jun 18, 2004 4:40 pm
by therat
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.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');
    }

}
?>
The form

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>
Calling the script

Code: Select all

<?php
error_reporting(E_ALL);

require 'sw_class_upload.php';

    $fu = new FileUpload('userfile','upload');



print_r($_FILES);
?>
Any 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

Posted: Fri Jun 18, 2004 4:53 pm
by feyd
$_FILES['userfile'] is an array.

your error lies here:

Code: Select all

<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&#1111;]" size="30" /><br /> 
  <input type="file" name="userfile&#1111;]" size="30" /><br /> 
  <input type="submit" value="Send files" /> 
</form>

Posted: Sat Jun 19, 2004 7:24 am
by therat
feyd wrote:$_FILES['userfile'] is an array.

your error lies here:

Code: Select all

<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&#1111;]" size="30" /><br /> 
  <input type="file" name="userfile&#1111;]" size="30" /><br /> 
  <input type="submit" value="Send files" /> 
</form>
I read somewhere else that when uploadin multiple files that you had to use userfile[]. If not then what should this be. Also in order to upload multiple files I assume I need a loop somewhere. Where would be best to put this, in the class file or the actual submit page.

Posted: Sat Jun 19, 2004 11:27 am
by feyd
You could name them differently, since your class only wants the name, basically. You could pass the associative array containing the information, instead of the name (probably a better idea)