I am having trouble with this script. It’s an image upload script and I am getting two errors that say the same thing:
Warning: Missing argument 1 for DoUpload::doUpload(), called in
/var/www/vhosts/mysite.net/httpdocs/mp/upload.php on line 8 and
defined in /var/www/vhosts/mysite.net/httpdocs/mp/includes/classes.php
on line 26 The file has been uploaded! Warning: Missing argument 1 for
DoUpload::doUpload(), called in
/var/www/vhosts/mysite.net/httpdocs/mp/upload.php on line 10 and
defined in /var/www/vhosts/mysite.net/httpdocs/mp/includes/classes.php
on line 26 An error occurred when uploading the file!
But as you can see in upload.php, I am passing an argument: the $_FILES array.
What do I do? (Not looking for anyone to re-write anything, I just need a little guidance in what I am doing wrong. 🙂 )
classes.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require('config.php');
// Connect to database
// Does not handle anything else
class DatabaseCon {
public $dbh;
// Method to connect to database
function dbConnect($config) {
try {
$this->dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
class DoUpload {
private $target_path = 'i/';
public $_FILES;
public function doUpload($_FILES) {
$this->target_path .= basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $this->target_path)) {
echo "The file has been uploaded!";
}
else {
echo "An error occurred when uploading the file!";
}
}
}
upload.php
<?php
ini_set('display_errors', 1);
require_once('includes/config.php');
require_once('includes/classes.php');
$db = new DatabaseCon();
$db->dbConnect($config);
$upload = new DoUpload();
$upload->doUpload($_FILES);
$sth = $db->prepare("INSERT INTO images (filename) VALUES (?)");
$sth->bindParam(1, $_FILES['file']['tmp_name']);
$sth->execute();
$_FILES is a superglobal, meaning it will be available to any part of your script regardless of scope. You shouldn’t need to pass in any arguments to your doUpload method, and you can still use the $_FILES array inside your method definition.
Don’t know if this is what is causing the problem or not, but just a heads up.