Search code examples
phphtmlcodeignitercodeigniter-3dropify

editing file not taking default value=image_name


View

<form class="m-form" action="<?php echo AURL;?>products/update_product/<?php echo $products['product_id'];?>" method="post" enctype="multipart/form-data">

<input type="file" name="product_image_name" class="form-control m-input dropify" placeholder="" data-default-file="<?php echo Website_Assets.'images/'.$products['product_image_name'];?>" value="<?php $products['product_image_name'];?>" data-max-file-size="2M" required>

</form>

In above code product_image_name not taking any value but showing the image picking the path and when I change image it post the image_name

Controller

public function update_product($product_id)
    {
        echo "<pre>";
        print_r ($_FILES['product_image_name']);
        echo "</pre>";
        exit();
    }

changing the image works ok but if i donot change image its not picking the default value of the image


Solution


  • Short answer: An input of type file can not have a default value.

    Instead use an <img />-tag to show default images. For example:

    <img src=" <?php echo Website_Assets.'images/'.$products['product_image_name'];?>" />
    

    Normal answer: Assuming you are trying to upload an image, save it to the entity that is behind the form and later edit it; try to think around using just the one field. Instead perhaps a sequence like this:

    1. Upload a file using the upload field
    2. Save the uploaded file on the machine that runs PHP (server-side)
    3. Store the path to the uploaded file in the entity
    4. In the edit view of the entity show both:
      • the image that was uploaded (or a placeholder)
      • an upload field to upload a new image

    Good luck!

    Tip: You might also want to have a look at open source libraries like dropzonejs. Existing libraries often give examples and excellent documentation. This example visually combines the upload field with the display field.