【问题标题】:Pass image data from form upload to a function to save将图像数据从表单上传传递到函数以保存
【发布时间】:2011-02-18 10:23:56
【问题描述】:

我有一个包含多个文件附件的上传表单 name="attachment[]"

我需要将上传的文件传递给类中的一个函数,该函数处理文件的保存(以及其他操作)。

功能:

public function saveFile($userId, $ticketId, $fileType, $fileName, $fileContent) 
{
    // Create directory
    $this->createDirectory($ticketId);

    $file = fopen(FILE_SAVE_PATH . "/" . $ticketId . "/" . $fileName, 'w');
    fwrite($file, $fileContent);
    fclose($file);
}

向函数发送数据的循环

foreach($_FILES['attachment']['name'] as $index => $value) 
{
      $fw->models->file->saveFile(
                            $si->input->session('bug/userData/id'), 
                            $ticketId, 
                            '', 
                            $_FILES['attachment']['name'][$index],
                            base64_encode($_FILES['attachment']['tmp_name'][$index]));
}

如您所见,我已经尝试过 base64_encode,图像已“保存”,但当我尝试打开时它已损坏。

谢谢。

【问题讨论】:

  • corrupt = base64 编码临时文件名并期望它是图像

标签: php file-upload


【解决方案1】:

看看这个http://www.tizag.com/phpT/fileupload.php 似乎你错过了一些保存上传的细节。 base64_encode($_FILES['attachment']['tmp_name'][$index]));不是文件

在对数组执行任何操作之前尝试 print_r($_FILES)。

【讨论】:

    【解决方案2】:
    public function saveUploadedFile($userId, $ticketId, $fileType, $fileName, $tempFile) 
    {
        //create directory
        $this->createDirectory($ticketId);
    
        $savePath = FILE_SAVE_PATH . "/" . $ticketId . "/" . $fileName;
        return (move_uploaded_file($tempFile, $savePath)) ? TRUE : FALSE;   
    }
    

    最终通过将临时路径传递给函数来修复它,谢谢

    $_FILES['attachment']['tmp_name'][$index]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-10
      • 2015-04-15
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多