0

如果用户上传文件并像这样检索内容

$file = $_FILES['uploadedFile'];

然后,将文件发送到函数以确保它是可接受的文件类型。如果是,请保存在服务器上

function saveInputFile($file){
   if($check->checkFile($file)== TRUE){
      //save $file on my server
   }
   else{
      echo "can't be saved!";
   }
}

假设它通过了 checkFile 函数,那么我怎样才能从 saveInputFile 函数中将此文件保存到我的服务器?我可以将文件设置为等于变量,然后保存该变量还是必须直接从 POST 数据中保存文件?

我已经看到它是这样完成的,但是我已经将文件传递给了这个函数。

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
   echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
 } else{
    echo "There was an error uploading the file, please try again!";
}

归根结底,我想在以下函数中保存一个文件。我可以像在上面的 saveInputFile 函数中那样传递文件,还是不能像这样工作?

4

2 回答 2

1

您可以使上传文件成为它自己的类型,它在数据旁边具有所需的方法。这将使使用更加灵活:

$upload = new UploadFile($_FILES['uploadedFile']);
$message = saveInputFile($upload);
echo $message;

function saveInputFile(UploadFile $upload)
{
    if ($check->checkFile($upload->getBasename()) == TRUE) {
        $message = $upload->moveTo($target_path)
            ? sprintf('The file %s has been uploaded', $upload->getBasename())
            : 'There was an error uploading the file, please try again!'
            ;
    } else {
        $message = "can't be saved!";
    }

    return $message;
}

新类型UploadFile代表$_FILE数组中的一个文件,它是一个包装文件上传携带的基本数据和方法的类。这是一些示例代码:

class UploadFile
{
    protected $file;

    private $filename;

    public function __construct(array $file)
    {
        $this->file = $file;
    }

    public function hasError()
    {
        return $this->getProperty('error') !== UPLOAD_ERR_OK;
    }

    public function getError()
    {
        return $this->getProperty('error');
    }

    public function getBasename()
    {
        return basename($this->getProperty('name'));
    }

    public function getFilename()
    {
        return $this->filename;
    }

    /**
     * @param $newName
     * @return NULL|SplFileInfo
     * @throws BadMethodCallException
     */
    public function moveTo($newName)
    {
        $newName  = (string)$newName;
        $filename = $this->getFilename();

        if ($filename !== NULL) {
            throw new BadMethodCallException(sprintf('Upload file (%s) has been already moved (%s).', $this->getBasename(), $filename));
        }

        $tmpName = $this->getProperty('tmp_name');

        if (move_uploaded_file($tmpName, $newName)) {
            $this->filename = realpath($newName);
        }

        return $this->getFileInfo();
    }

    /**
     * @return SplFileInfo|NULL
     */
    public function getFileInfo()
    {
        $filename = $this->getFilename();

        if ($filename !== NULL) {
            return new SplFileInfo($filename);
        }
    }

    protected function getProperty($name, $default = NULL)
    {
        if (isset($this->file[$name])) {
            return $this->file[$name];
        }

        return $default;
    }
}

随心所欲地使用它。另请参阅PHP 手册中有关文件上传的SplFileInfo文档,其中记录了数组的结构和PHP 上传错误代码(这很重要)$_FILE

于 2012-09-17T21:27:26.067 回答
1

您的move_uploaded_file函数应采用 2 个参数,即源文件名destination file名称。

在 PHP 中,文件移动函数称为rename

于 2012-09-17T20:53:17.870 回答