简体   繁体   中英

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


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM