editing file not taking default value=image_name - php

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:
Upload a file using the upload field
Save the uploaded file on the machine that runs PHP (server-side)
Store the path to the uploaded file in the entity
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.

Related

Saving image to PHP session

What I'm attempting to accomplish
Hi! I've scoured the web and can't quite find what I'm looking for. We are working on an internal email signature creation tool. I would like to add an image to a session so I can insert that image into the tool at the end. I do not want to store it in a temp folder or save it to a database. I know this is not best practice to put images in a session. The tool copies all rendered HTML to your clipboard to be immediately inserted into Gmail/Outlook.
My code on Page 1
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
</form>
My code on Page 2
<?php
$_SESSION["fileToUpload"] = $_POST['fileToUpload'];
?>
The result
"image.png" text is inserted into the signature tool. Instead of the file info/name inserted into the tool, I want the actual image itself. Is this possible? Thanks!
So you're trying to do something like markdown? That's not going to work...
You could try this:
https://github.com/ziggi/zimg-host
You can convert an image to base64 encode and then play with it.
Example:
$img = file_get_contents('<path>/image.jpg');
$data = base64_encode($img);
As result, you will have a bit string that you can save the value in the session.
But keep in mind that this is not a best practice.
Also, you can use this string in the source of an image.
Example:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAyACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" />

How I can show which image has been uploaded inside input type file with php?

I have database structure as such for image that it stores it name and moves upload to a uploads directory via move_uploaded_file(). I have a page where people can edit what they have uploaded to that specific post. Commonly it is done via giving an value for input but what ever I put inside input is not being displayed. Basically I wants to show which image is uploaded with that specific post.
Html (doesn't works )
<input class="" value="<?php echo $row['post_image'] ?>" type="file" id="addPost_post_image" name="addPost_post_image">
echo $row['post_image'] shows which image name is stored. and is giving right info.
Image is placed under a folder uploads.
Try putting
<img src="uploads/<?php echo $row['post_image'] ?>">
next to input field.
I assume you have an image name saved in the database row post_image. If not then get name from basename($_FILES["addPost_post_image"]["name"]);.

How to put an image file to input[file] from database using php

How should I put an image into <input type="file"> where the image is from my database(actually, the image name only written into database, and the image store in my folder), just like a value="" in the other input type, but <input type="file" value="<?php echo $imagefileerwerewr; ?>"> doesn't work to retain the image file.
There is not a way that you can just put you image inside an file input and you can update image. I would suggest you that you use both img tag and input tag to archieve this. You can use img tag to show the current image and also you can use input to let the users to update another image if they want.
I would suggest you to use dropzone it has some great features and would be easier for you to manage all this

Implementing Profile Photo Upload for Student Database

I am currently develop a student database system for my faculty. I am using the PHP together with MySQL. I am thinking to create an option for student to upload their profile photo but I could not find any proper instruction or tutorial of doing that.
Here's the code processing the file uploading:
<?php
/* Script name: uploadFile.php
* Description: Uploads a file via HTTP with a POST form.
*/
if(!isset($_POST[‘Upload’]))
{
include(“form_upload.inc”);
}
else
{
if($_FILES[‘pix’][‘tmp_name’] == “none”)
{
echo “<p style=’font-weight: bold’>
File did not successfully upload. Check the
file size. File must be less than 500K.</p>”;
include(“form_upload.inc”);
exit();
}
if(!ereg(“image”,$_FILES[‘pix’][‘type’]))
{
echo “<p style=’font-weight: bold’>
File is not a picture. Please try another
file.</p>”;
include(“form_upload.inc”);
exit();
}
else
{
$destination=’c:\data’.”\\”.$_FILES[‘pix’][‘name’];
$temp_file = $_FILES[‘pix’][‘tmp_name’];
move_uploaded_file($temp_file,$destination);
echo “<p style=’font-weight: bold’>
The file has successfully uploaded:
{$_FILES[‘pix’][‘name’]}
({$_FILES[‘pix’][‘size’]})</p>”;
}
}
?>
Code for the file upload form:
<!-- Program Name: form_upload.inc
Description: Displays a form to upload a file -->
<html>
<head><title>File Upload</title></head>
<body>
<ol><li>Enter the file name of the product picture you
want to upload or use the browse button
to navigate to the picture file.</li>
<li>When the path to the picture file shows in the
text field, click the Upload Picture
button.</li>
</ol>
<div align=”center”><hr />
<form enctype=”multipart/form-data”
action=”uploadFile.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE”
value=”500000” />
<input type=”file” name=”pix” size=”60” />
<p><input type=”submit” name=”Upload”
value=”Upload Picture” />
</form>
</div></body></html>
I got the same outcome which I cant find the file that being uploaded and it is not being uploaded to the location as it should be.
You should change the destination:
$destination=’c:\data’.”\”.$_FILES[‘pix’][‘name’];
Or, if this is not working, try to move the uploaded file somewhere near the script path, like:
$destination= dirname(__FILE__).DIRECTORY_SEPARATOR.$_FILES[‘pix’][‘name’];
If the second one works then is means, you have given a wrong directory for the upload.
I think you should first change your image path.
and than your datatype set to image in mysql manually,
it will be done.
Do you get any error messsage? Do the PHP user have write permissions to the upload directory?
Also, here's two advices possibly not related to your question:
Don't use eregi in any new code. It is deprecated, which means it will be removed in a future version of PHP. Instead, use the preg_ functions, or just strpos in this case.
Why would $_FILES[‘pix’][‘tmp_name’] ever be the string "none"?

file upload fail in php

When I put this:
<input name="image" type="file" value="Browse" />
I get a weird text in hebrew..saying "choose file" and next to it is written "file wasnt chosen", how do I make the text in english..how do I override the default?
Also how do I put a predetermined picture in my database.. do I go to a field and click As defined than put :
"Images/Untitled-1.png"
The image folder than the image name?
Check your localization settings....no overriding here, it's the OS that does this for you.

Categories