我制作了一个可以上传某些文件类型的页面。它在我的本地开发中运行良好,但在生产中却不行。它不会通过 do_upload() 函数。除此之外没有任何显示。
$path = 'uploads';
echo 'path: '. $path .' <br>';
//-- config for uplaod --
$configUpdload['upload_path'] = $path ;
$configUpdload['allowed_types'] = 'gif|jpg|png|xls|xlsx|pdf';
$configUpdload['max_size'] = '4000'; //size in kilobytes
$configUpdload['encrypt_name'] = TRUE;
//-- load upload library --
$this->load->library('upload');
$this->upload->initialize($configUpdload);
if ($_FILES['fileref']['name']!="")
{
ini_set('display_errors', true);
error_reporting(E_ALL);
echo 'uploadAttachment<br>';
$uploaded = $this->upload->do_upload("fileref");
echo 'uploaded: '. $uploaded . '<br>';
if ($uploaded == true)
{
echo 'uploaded data<br>';
$inputUp = $this->upload->data();
echo 'data: '. print_r($inputUp,true) . '<br>';
$uploadInput['orig_name'] = $inputUp['orig_name'];
$uploadInput['enc_name'] = $inputUp['file_name'];
$uploadInput['extension'] = $inputUp['file_ext'];
echo 'uploaded true<br>';
}else{
die($this->upload->display_errors());
}
}
我尝试将 upload_path 更改为不存在的文件夹,它显示“上传路径似乎无效”。意味着 do_upload() 工作正常吗?
上传文件夹权限为 777。
我也尝试使用绝对路径,但仍然没有显示任何通过 do_upload() 的内容。
或者和服务器有什么关系?
有谁知道发生了什么?非常感谢帮助。
谢谢。
[编辑1]
服务器的错误日志和正常日志中没有任何内容。我还设置了 $config['log_threshold'] = 4; 没有错误日志。
[编辑2]
生产服务器是 linux 下的第 3 方网络托管。我尝试了 php_info() 并跟踪与上传有关的任何事情。
file_uploads On On
max_file_uploads 20 20
pload_max_filesize 30M 30M
upload_tmp_dir /tmp/ /tmp/
suhosin.upload.disallow_binary 0 0
suhosin.upload.disallow_elf 1 1
suhosin.upload.max_uploads 25 25
suhosin.upload.remove_binary 0 0
suhosin.upload.verification_script no value
我还尝试减小文件大小 $configUpdload['max_size'] = '10'; 但 do_upload() 之后仍然没有消息。
[Edit3] 我的文件夹结构:
public_html/gtapp/
public_html/gtapp/csystem/ <- is system folder
public_html/gtapp/v1/ <- is application folder
public_html/gtapp/media/
public_html/gtapp/uploads/
public_html/gtapp/license.txt
在 config.php 中:
$config['base_url'] = 'http://www.example.com/gtapp/
在 index.php 中:
$application_folder = 'v1';
当我APPPATH . 'uploads';
对上面的代码进行显示时:
uploadAttachment
path: v1/uploads
uploaded:
The upload path does not appear to be valid.
我试过$path = $_SERVER['DOCUMENT_ROOT'] . '/gtapp/uploads';
它只是显示:
uploadAttachment
path: /home/user/domains/example.com/public_html/gtapp/uploads
[编辑4]
我制作了原生 PHP 上传代码的粗略版本,它可以工作。我不认为这是服务器问题。下面是代码:
$name = 'fileref';
$upload_path = './uploads/';
$allowed_types = array("jpg", "jpeg", "gif", "png", "xls", "xlsx", "pdf");
$extension = end(explode(".", $_FILES[$name]["name"]));
$max_size = '4000';
if ($_FILES['fileref']['name']!="")
{
if ((($_FILES[$name]["type"] == "image/gif")
|| ($_FILES[$name]["type"] == "image/jpeg")
|| ($_FILES[$name]["type"] == "image/pjpeg")
|| ($_FILES[$name]["type"] == "image/png")
|| ($_FILES[$name]["type"] == "image/x-png")
|| ($_FILES[$name]["type"] == "application/excel")
|| ($_FILES[$name]["type"] == "application/vnd.ms-excel")
|| ($_FILES[$name]["type"] == "application/msexcel")
|| ($_FILES[$name]["type"] == "application/pdf")
|| ($_FILES[$name]["type"] == "application/x-download"))
&& (round($_FILES[$name]["size"]/1024,2) < $max_size )
&& in_array($extension, $allowed_types))
{
if ($_FILES[$name]["error"] > 0)
{
echo $_FILES[$name]["error"]);
}
else
{
$uploadInput['orig_name'] = $_FILES[$name]["name"];
$uploadInput['enc_name'] = sha1($_FILES[$name]["name"]);
$uploadInput['extension'] = $_FILES[$name]["type"];
$moved = move_uploaded_file($_FILES[$name]["tmp_name"], $upload_path . $uploadInput['enc_name'] . '.' . $extension);
if($moved)
echo 'success';
else
echo 'fail';
}
}
}