gpt4 book ai didi

php - 即使通过了论证,也缺少论证?

转载 作者:行者123 更新时间:2023-11-29 05:34:38 28 4
gpt4 key购买 nike

我在使用这个脚本时遇到了问题。这是一个图片上传脚本,我收到两个错误,说的是同一件事:

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!

但是正如您在 upload.php 中所见,我传递了一个参数:$_FILES 数组。

我该怎么办? (不找任何人重写任何东西,我只需要一点指导我做错了什么。:))

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 并不总是定义,IIRC。如果您不上传任何内容或只是点击页面上的提交按钮而不传递任何文件或上传任何内容,它将为空,因此会导致错误。

此外,$_FILES 是一个超全局 PHP 变量,我不会在您自己的函数(参数名称)中重复使用那个确切的名称。不确定 PHP 将如何处理。

PHP 似乎也反对它:

Note: Variable variables Superglobals cannot be used as variable variables inside functions or class methods.

我想将它用作方法参数意味着它是一个可变变量。

关于php - 即使通过了论证,也缺少论证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11821050/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com