我可以在 codeigniter 中使用此代码上传文件吗?
Can I upload file with this code in codeigniter?
$judul = $_POST['judul'];
$idKategori = $_POST['kategori'];
$idPropinsi = $_POST['propinsi'];
$img = $_FILES['img']['name'];
$img_tmp = $_FILES['img']['tmp_name'];
$idUser = $_POST['user'];
$isi = $_POST['isi'];
$date = $_POST['date'];
if(empty($img)) {
$query = mysql_query("INSERT INTO `artikel`(`idArtikel`, `idKategori`, `idPropinsi`, `judul`, `idUser`, `isi`, `date`) VALUES ('','$idKategori','$idPropinsi','$judul','$idUser','$isi','$date')");
}else{
if(move_uploaded_file($img_tmp,"../../../img/".$img)) {
$query = mysql_query("INSERT INTO `artikel`(`idArtikel`, `idKategori`, `idPropinsi`, `judul`, `img`, `idUser`, `isi`, `date`) VALUES ('','$idKategori','$idPropinsi','$judul','$img','$idUser','$isi','$date')");
}else{
echo "Failed to upload image";
}
}
if($query) {
header("location:../../index.php?page=artikel");
}else{
echo "failed to update this post";
}
但是结果是
move_uploaded_file(http://localhost/mvc/kuliner/assets/img/Green Nature Wallpapers 04.jpg): failed to open stream: HTTP wrapper does not support writeable connections
没有!
基本上重写此代码以使用 Codeigniter 比使用 CI 文档中的示例从头开始编写要多得多。 Link 在上面的评论中提供 (http://www.codeigniter.com/userguide2/libraries/file_uploading.html)。
有几个原因,因为文件上传、重定向、数据库处理、用户输出等在 CI 中使用 CI 的方法和编程的 MVC 结构不同。
这就是我在 CI 中上传的方式(使用 Dropzone)。
创建Dropzone
控制器
class Dropzone extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url','html','form'));
}
public function upload() {
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$temp = $_FILES["file"]["name"];
$path_parts = pathinfo($temp);
$t = preg_replace('/\s+/', '', microtime());
$fileName = $path_parts['filename']. $t . '.' . $path_parts['extension'];
$targetPath = UPLOADPATH;
$targetFile = $targetPath . $fileName ;
echo $fileName;
move_uploaded_file($tempFile, $targetFile);
// if you want to save in db,where here
// with out model just for example
// $this->load->database(); // load database
// $this->db->insert('file_table',array('file_name' => $fileName));
}
}
}
// Usage : <form action="<?php echo site_url('/dropzone/upload');" class="dropzone" >
/* End of file dropzone.js */
/* Location: ./application/controllers/dropzone.php */
其次添加如下内容HTML,(主要是为了提交文件名的一个表单
<div id="upload" class="form-group">
<label>Drop file Here</label>
<input type="hidden" id="file" name="file"/>
<div
class="dropzone"
id="uploadFile"><!--uploadFile is the dropzone name-->
</div>
</div>
然后一点Js来施展魔法
Dropzone.options.uploadFile = {
paramName: "file", // The name that will be used to transfer the file
url:"<?php echo site_url('/dropzone/upload');?>",
maxFiles:1,
acceptedFiles:'image/*,application/pdf,.docx,.doc,.xls,.xlsx,.csv',
dictMaxFilesExceeded:"You can only upload one file per Response",
init: function() {
this.on("sending", function(file) {
// $('button#submit').attr('disabled','');// Requires Jquery
});
this.on("complete", function(file) {
//$('button#submit').removeAttr('disabled'); // Requires JQuery
});
this.on("success", function(file,xhr) {
//$('input[type="hidden"]#file').val(xhr); // Requires Jquery
});
}
};
如果需要,请记住添加 dropzone.js 和 jquery.js。
您可以使用PHP 功能上传文件。但首先,您应该自己制作目录或文件夹。
这是我的代码,我没有添加验证部分。我在根目录中创建了 uploads
文件夹。
$file = $_FILES['verfication_image'];
$file_name_with_extenstion = $file['name'];
$file_name = pathinfo($file_name_with_extenstion, PATHINFO_FILENAME);
$extension = pathinfo($file_name_with_extenstion, PATHINFO_EXTENSION);
$file_tmp_location =$_FILES['verfication_image']['tmp_name'];
$upload_name = $file_name.time().".$extension";
if(move_uploaded_file($file_tmp_location,"uploads/".$upload_name)){
echo "The file has been uploaded.";
}else{
echo "There was an error.";
}
我建议使用 Codeigniter 版本上传文件,因为添加验证很简单。我已经添加了带有验证的 Codeigniter 代码,
$file = $_FILES['verfication_image'];
$file_name_with_extenstion = $file['name'];
$file_name = pathinfo($file_name_with_extenstion, PATHINFO_FILENAME);
$extension = pathinfo($file_name_with_extenstion, PATHINFO_EXTENSION);
$upload_name = $file_name.time().".$extension";
$config['upload_path'] = 'uploads/';
$config['file_name'] = $upload_name;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);//Load the libary with the configuration
if(!$this->upload->do_upload('verfication_image')){
echo($this->upload->display_errors());//validation error will be printed
}else{
echo "The file has been uploaded.";
}
更多详情请关注官方指引File Uploading class
$judul = $_POST['judul'];
$idKategori = $_POST['kategori'];
$idPropinsi = $_POST['propinsi'];
$img = $_FILES['img']['name'];
$img_tmp = $_FILES['img']['tmp_name'];
$idUser = $_POST['user'];
$isi = $_POST['isi'];
$date = $_POST['date'];
if(empty($img)) {
$query = mysql_query("INSERT INTO `artikel`(`idArtikel`, `idKategori`, `idPropinsi`, `judul`, `idUser`, `isi`, `date`) VALUES ('','$idKategori','$idPropinsi','$judul','$idUser','$isi','$date')");
}else{
if(move_uploaded_file($img_tmp,"../../../img/".$img)) {
$query = mysql_query("INSERT INTO `artikel`(`idArtikel`, `idKategori`, `idPropinsi`, `judul`, `img`, `idUser`, `isi`, `date`) VALUES ('','$idKategori','$idPropinsi','$judul','$img','$idUser','$isi','$date')");
}else{
echo "Failed to upload image";
}
}
if($query) {
header("location:../../index.php?page=artikel");
}else{
echo "failed to update this post";
}
但是结果是
move_uploaded_file(http://localhost/mvc/kuliner/assets/img/Green Nature Wallpapers 04.jpg): failed to open stream: HTTP wrapper does not support writeable connections
没有!
基本上重写此代码以使用 Codeigniter 比使用 CI 文档中的示例从头开始编写要多得多。 Link 在上面的评论中提供 (http://www.codeigniter.com/userguide2/libraries/file_uploading.html)。
有几个原因,因为文件上传、重定向、数据库处理、用户输出等在 CI 中使用 CI 的方法和编程的 MVC 结构不同。
这就是我在 CI 中上传的方式(使用 Dropzone)。
创建Dropzone
控制器
class Dropzone extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url','html','form'));
}
public function upload() {
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$temp = $_FILES["file"]["name"];
$path_parts = pathinfo($temp);
$t = preg_replace('/\s+/', '', microtime());
$fileName = $path_parts['filename']. $t . '.' . $path_parts['extension'];
$targetPath = UPLOADPATH;
$targetFile = $targetPath . $fileName ;
echo $fileName;
move_uploaded_file($tempFile, $targetFile);
// if you want to save in db,where here
// with out model just for example
// $this->load->database(); // load database
// $this->db->insert('file_table',array('file_name' => $fileName));
}
}
}
// Usage : <form action="<?php echo site_url('/dropzone/upload');" class="dropzone" >
/* End of file dropzone.js */
/* Location: ./application/controllers/dropzone.php */
其次添加如下内容HTML,(主要是为了提交文件名的一个表单
<div id="upload" class="form-group">
<label>Drop file Here</label>
<input type="hidden" id="file" name="file"/>
<div
class="dropzone"
id="uploadFile"><!--uploadFile is the dropzone name-->
</div>
</div>
然后一点Js来施展魔法
Dropzone.options.uploadFile = {
paramName: "file", // The name that will be used to transfer the file
url:"<?php echo site_url('/dropzone/upload');?>",
maxFiles:1,
acceptedFiles:'image/*,application/pdf,.docx,.doc,.xls,.xlsx,.csv',
dictMaxFilesExceeded:"You can only upload one file per Response",
init: function() {
this.on("sending", function(file) {
// $('button#submit').attr('disabled','');// Requires Jquery
});
this.on("complete", function(file) {
//$('button#submit').removeAttr('disabled'); // Requires JQuery
});
this.on("success", function(file,xhr) {
//$('input[type="hidden"]#file').val(xhr); // Requires Jquery
});
}
};
如果需要,请记住添加 dropzone.js 和 jquery.js。
您可以使用PHP 功能上传文件。但首先,您应该自己制作目录或文件夹。
这是我的代码,我没有添加验证部分。我在根目录中创建了 uploads
文件夹。
$file = $_FILES['verfication_image'];
$file_name_with_extenstion = $file['name'];
$file_name = pathinfo($file_name_with_extenstion, PATHINFO_FILENAME);
$extension = pathinfo($file_name_with_extenstion, PATHINFO_EXTENSION);
$file_tmp_location =$_FILES['verfication_image']['tmp_name'];
$upload_name = $file_name.time().".$extension";
if(move_uploaded_file($file_tmp_location,"uploads/".$upload_name)){
echo "The file has been uploaded.";
}else{
echo "There was an error.";
}
我建议使用 Codeigniter 版本上传文件,因为添加验证很简单。我已经添加了带有验证的 Codeigniter 代码,
$file = $_FILES['verfication_image'];
$file_name_with_extenstion = $file['name'];
$file_name = pathinfo($file_name_with_extenstion, PATHINFO_FILENAME);
$extension = pathinfo($file_name_with_extenstion, PATHINFO_EXTENSION);
$upload_name = $file_name.time().".$extension";
$config['upload_path'] = 'uploads/';
$config['file_name'] = $upload_name;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);//Load the libary with the configuration
if(!$this->upload->do_upload('verfication_image')){
echo($this->upload->display_errors());//validation error will be printed
}else{
echo "The file has been uploaded.";
}
更多详情请关注官方指引File Uploading class