dongqiangse6623 2012-09-17 20:49
浏览 13
已采纳

在使用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?

  • 写回答

2条回答 默认 最新

  • douyan1453 2012-09-17 21:27
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式