在使用PHP检查验证后如何将文件保存到服务器

在使用PHP检查验证后如何将文件保存到服务器

问题描述:

If a user uploads a file and the contents are retrieved like so

$file = $_FILES['uploadedFile'];

Then, the file is sent to a function to make sure it's an accepted file type. If it is, save it on the server

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

Assuming it passes the checkFile function, how can I then save this file to my server from within the saveInputFile function? Can I set the file equal to a variable, and then save that variable or do I have to save the file directly from the POST data?

I've seen it done like this, but I already have the file passed into this function.

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!";
}

When it comes down to it, I want to save a file in the following function. Can I pass the file like a variable as I did in the saveInputFile function above, or does it not work like this?

如果用户上传文件并按原样检索内容 p>

   $ file = $ _FILES ['uploadedFile']; 
  code>  pre> 
 
 

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

  function saveInputFile($ file){
 if($ check-> checkFile($ file)== TRUE){\  n //在我的服务器上保存$ file 
} 
其他{
 echo“无法保存!”; 
} 
} 
  code>  pre> 
 
 

假设它通过checkFile函数,我怎么能从saveInputFile函数中将这个文件保存到我的服务器? 我可以将文件设置为等于变量,然后保存该变量或者我是否必须直接从POST数据保存文件? strong> p>

我看过它已完成 像这样,但我已经将文件传递给了这个函数。 p>

  if(move_uploaded_file($ _ FILES ['uploadedfile'] ['tmp_name'],$ target_path)){
 echo“The file”。  basename($ _FILES ['uploadedfile'] ['name'])。  “已上传”; 
}其他{
 echo“上传文件时出错,请重试!”; 
} 
  code>  pre> 
 
 

归结为它,我想在以下函数中保存文件。 我可以像上面的saveInputFile函数一样传递文件,或者它不能像这样工作吗? p> div>

You can make the upload file a type of it's own that has the methods it needs next to it's data. That will make the usage more flexible:

$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;
}

The new type UploadFile represents one file from the the $_FILE array, it is a class that wraps the basic data and methods a file-upload carries. Here is some example code:

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;
    }
}

Use it at your will. See as well SplFileInfo and the documentation about file uploads in the PHP manual which documents the structure of the $_FILE array and the PHP upload error codes (which are important).

Your move_uploaded_file function should take 2 parameters, the source file name and the destination file name.

In PHP the file move function is called rename.