Codeigniter 4:使用move_uploaded_file上传文件 [英] Codeigniter 4: Uploading files with move_uploaded_file
问题描述
我刚刚开始将CodeIgniter 3项目移至CodeIgniter4。
除文件上传外,其他所有操作都正常。
我想将用户上传的文件保留在/ writable / uploads中。下面是我用来将上传的文件移动到所需位置的代码。
$ target_dir ='/ writable / uploads / recordings / ';
$ target_file = $ target_dir。 basename($ _ FILES [ gfile] [ name]);
$ FileType = pathinfo($ target_file,PATHINFO_EXTENSION);
if($ FileType!= mp3){
$ vmuploadOk = 1;
}
else
$ vmuploadOk = 1;
if($ vmuploadOk == 1){
$ greetfile = $ id。 G 。 basename($ _ FILES [ gfile] [ name]);
$ target_filenew = $ target_dir。 $ greetfile;
move_uploaded_file($ _ FILES [ gfile] [ tmp_name],$ target_filenew);
}
我认为这是因为CI4将可写文件夹保留在公用文件夹之外。
这对我有用,我希望它也对您有用。在codeigniter 4中,请使用它上传文件并将其移至控制器中。
if($ imagefile = $ this-> request-> getFiles())
{
if($ img = $ imagefile ['gfile'])
{
if($ img-> isValid()&&!$ img-> hasMoved())
{
$ newName = $ img-> getRandomName(); ///如果您要将文件名更改为加密名称
$ img-> move(WRITEPATH.’uploads’,$ newName);
//您可以在此处继续编写代码以将名称保存到数据库
// db_connect()或模型格式
}
}
}
或
<上课前= lang-php prettyprint-override>
if($ img = $ this-> request-> getFile('gfile'))
{
如果($ img-> isValid()&&!$ img-> hasMoved())
{
$ newName = $ img-> getRandomName();
$ img-> move(’./ public / uploads / images / users',$ newName);
//您可以在此处继续编写代码以将名称保存到数据库
// db_connect()或模型格式
}
}
然后在您的HTML中
使用此
< input type = file name = gfile>
我希望这会帮助您引起我的注意。
I just started moving CodeIgniter 3 project to CodeIgniter 4.
Everything works fine except file upload.
I would like to keep the user uploaded files in /writable/uploads. Below is the code I use to move the uploaded file to desired location.
$target_dir = '/writable/uploads/recordings/';
$target_file = $target_dir . basename($_FILES["gfile"]["name"]);
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($FileType != "mp3") {
$vmuploadOk = 1;
}
else
$vmuploadOk = 1;
if ($vmuploadOk == 1) {
$greetfile = $id . "g" . basename($_FILES["gfile"]["name"]);
$target_filenew = $target_dir . $greetfile;
move_uploaded_file($_FILES["gfile"]["tmp_name"], $target_filenew);
}
I assume that it is because CI4 keeps writable folder outside public folder.
This worked for me and I hope it will also work for you. In codeigniter 4 please use this to upload your files and move it in your controller.
if($imagefile = $this->request->getFiles())
{
if($img = $imagefile['gfile'])
{
if ($img->isValid() && ! $img->hasMoved())
{
$newName = $img->getRandomName(); //This is if you want to change the file name to encrypted name
$img->move(WRITEPATH.'uploads', $newName);
// You can continue here to write a code to save the name to database
// db_connect() or model format
}
}
}
Or
if($img = $this->request->getFile('gfile'))
{
if ($img->isValid() && ! $img->hasMoved())
{
$newName = $img->getRandomName();
$img->move('./public/uploads/images/users', $newName);
// You can continue here to write a code to save the name to database
// db_connect() or model format
}
}
Then in your html Use this
<input type="file" name="gfile">
I hope this will help you else call my attention.
这篇关于Codeigniter 4:使用move_uploaded_file上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!