gpt4 book ai didi

php - 如何使用codeigniter实现上传脚本

转载 作者:行者123 更新时间:2023-12-02 20:27:49 24 4
gpt4 key购买 nike

我正在尝试向 codeigniter 实现多个文件上传脚本。该脚本可以在这里找到 http://valums.com/ajax-upload/ 。我觉得它很好,所以我决定放入当前的应用程序。

问题是,我无法使用 codeigniter 访问上传的文件。如果您是 codeigniter 开发人员,请告诉我如何获取脚本发送的文件?

最佳答案

CodeIgniter 有几种方法来处理上传,但由于您的 ajax 上传脚本已经有一个处理程序,因此您应该使用它。

如何:

在您的 application/libraries 文件夹中创建一个新文件,命名为 Qqfileuploader.php 并将其粘贴到其中:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Qqfileuploader {
private $allowedExtensions = array();
private $sizeLimit = 10485760;
private $file;

function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
$allowedExtensions = array_map("strtolower", $allowedExtensions);

$this->allowedExtensions = $allowedExtensions;
$this->sizeLimit = $sizeLimit;

$this->checkServerSettings();

if (isset($_GET['qqfile'])) {
$this->file = new qqUploadedFileXhr();
} elseif (isset($_FILES['qqfile'])) {
$this->file = new qqUploadedFileForm();
} else {
$this->file = false;
}
}

private function checkServerSettings(){
$postSize = $this->toBytes(ini_get('post_max_size'));
$uploadSize = $this->toBytes(ini_get('upload_max_filesize'));

if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
$size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
}
}

private function toBytes($str){
$val = trim($str);
$last = strtolower($str[strlen($str)-1]);
switch($last) {
case 'g': $val *= 1024;
case 'm': $val *= 1024;
case 'k': $val *= 1024;
}
return $val;
}

/**
* Returns array('success'=>true) or array('error'=>'error message')
*/
function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
if (!is_writable($uploadDirectory)){
return array('error' => "Server error. Upload directory isn't writable.");
}

if (!$this->file){
return array('error' => 'No files were uploaded.');
}

$size = $this->file->getSize();

if ($size == 0) {
return array('error' => 'File is empty');
}

if ($size > $this->sizeLimit) {
return array('error' => 'File is too large');
}

$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = $pathinfo['extension'];

if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
}

if(!$replaceOldFile){
/// don't overwrite previous files that were uploaded
while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
$filename .= rand(10, 99);
}
}

if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}

}
}

现在,在您要上传到的 Controller 中,执行以下操作:

// list of valid extensions, ex. array("jpeg", "xml", "bmp")
$allowedExtensions = array();
// max file size in bytes
$sizeLimit = 10 * 1024 * 1024;

$this->load->library("Qqfileuploader",array($allowedExtensions, $sizeLimit));
$this->Qqfileuploader->handleUpload('uploads/');
// to pass data through iframe you will need to encode all html tags
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);

应该有效。

关于php - 如何使用codeigniter实现上传脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4583942/

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