CodeIgniter Forums
file uploading - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: file uploading (/showthread.php?tid=90579)



file uploading - JasonJJ - 04-05-2024

Hello,

I am new to coding world and using CI4.4.6
I followed the manual about file uploading and I successfully made the link to force file downloaded via web browser.

One problem I encountered is the how can I link to the mp3 file.
I made <audio><source src=""></audio> but it didn't play the mp3 file. 

I researched and found some solution;
1. I have to move 'WRITEPATH . 'uploads/' directory to '/public' because /public directory is able to be accessed via web.

Here is a question. 
If my understanding is right, how can I change the directory from '/writable' to '/public' ?

Here is my code in Controller.

PHP Code:
$userfile $this->request->getFile('userfile');
WRITEPATH 'uploads/' $userfile->store(); 


even though tried below the result is same strangely. File is still stored in writable directory !!!!

PHP Code:
$userfile $this->request->getFile('userfile');
$filepath '/public/aaa' $userfile->store(); 



RE: file uploading - kenjis - 04-05-2024

Read https://codeigniter.com/user_guide/libraries/uploaded_files.html#store-files

The following code is a bit difficult to understand, but $img->store() moves the file.

PHP Code:
        if (! $img->hasMoved()) {
            $filepath WRITEPATH 'uploads/' $img->store();

            $data = ['uploaded_fileinfo' => new File($filepath)];

            return view('upload_success'$data);
        



RE: file uploading - JasonJJ - 04-07-2024

According to my understanding, $file->store() is CI4's function which only works for WRITEPATH. With store(), I can only modify the folder under the WRITEPATH.

So I use $file->move() to save uploaded file and set the directory '/public' manually. After saving uploaded files in '/public', I can link mp3 file in audio tag.