使用Codeigniter从文件夹中检索图像

My teacher,he was uploading and retrieving images from folder with ID recognition. It works with database.

  1. So, when we upload an image, it will be stored in a folder with it's name changing automatically with ID.
  2. Example: The upload form must be on "yourwebsite.com/yourpages/3144242" so the image will be stored as "3144242" and blablabla the same for other pages with ID

This is how he upload and retrieve it

 <?php
    $ff = './assets/images/kegiatan/'.$_SESSION['idkeg'].'.jpg';
      if( file_exists( $ff ) ) {
          <img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
 <?php
       }
 ?>

Retrieve images part:

      if( file_exists( $ff ) ) {
          <img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
 <?php
       }
 ?>

However, I change it a bit so I don't have to use a database and $_SESSION things. With this:

<?php
   $ff = './assets/images/kegiatan/'.$this->input->post('img_name').'.jpg';
 ?>
  <form id="form" class="kart" action="<?= base_url()?>gallery/upload2" method="post" enctype="multipart/form-data">
    <div class="form-group">
      <input type="text" class="form-control" name="img_name" required="required">
    </div>
    <div class="form-group">
       <input type="file" class="form-control" name="foto">
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-common" value="Update"><i class="fa fa-upload fa-2x"> Upload</i></button>
    </div>
 </form>

It works uploading images into folder "kegiatan" (means activity) with simply write the names of images in a form. The img_name that you have input, now become the name of that image.

Now I have a little problem to retrieve that images. How?

mistake in your form I realized is asking user to enter image name and get image from folder by using that input value.

First of all, I haven't tried upload an image with codeigniter but I will answer reference to php itself.

When we get an image as input from user by form, then we use "move_uploaded_file" function in php and we send temp_file, path with file name arguments to this function to save the file user selected in the form we prepared.

move_uploaded_file(string $filename , string $target);
// $filename: I use $_FILES['getFileUpload']['tmp_name'] for this argument
// $target: I define path with file name, eg: uploads/files/new_file_25.jpg for jpg image

So, you souldn't ask user to enter file name, because you will deifne it. Or, you must use "img_name" that you asked user to enter as file name which is most probably won't satisfy your ID prediction.

I hope this will help you.