Echo uploaded filename from database in no file chosen part - php

I have form which includes a file upload.After the form is submitted the selected filename is saved in the database.And i have a edit form of saved data.In that form in the file upload button,at the position where shows "no file chosen" i need to echo the saved file form the database.
<div class="col-md-3">
<div class="form-group">
<i class="fa fa-fw fa-cloud-upload"></i>
<label for="">File upload</label>
<?php echo $announcements_details['file'] ; ?>
<input type="file" id="captionfile" name="captionfile"></div>\
</div>
I need echo the $announcements_details['file'] in the no file choose chosen part. Can anyone help me?

It is impossible to do this, due to security reasons. you would be able to select and upload any file from the client to your website.
However, you can display a message below it saying a file has been uploaded. Or you could override the input style using css and display the text that way.

Yes, as Jerodev said it is not possible to takeout the same file in place of the choose file button. But there is a work around for this.
If the uploaded file is an image file, then you could pull up the filename from the database, and display a small thumbnail for the image below the choose upload button.
And if it is any other file, (pdf/txt..etc.), then you could display a link to the file below the choose upload button, so that the users can download the file and see what they have uploaded.
Hope this helps.
Thanks.

Related

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:
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.

PHP move_uploaded_file equivalent in JQuery

I have this code that helps me to open a file input when I click on an image:
HTML/PHP:
<label for="img-input">
<img src=<?php echo "".$file[$n].""; ?> class="canvas-1">
<div class="alert-success pad-top-bottom text-center"><strong><?php echo basename($file[$n]); ?></strong></div>
</label>
<input type="file" id="img-input">
where "$file[$n]" is the address of the file on the server folder. There is a bunch of image that are rendered on the screen and when I click on one them, the file input opens. That is ok.
What I would like to do is that when I select a image from the file input by clicking on a specific image, The selected image should replace the image I clicked before.
I read many uploading solutions with JQuery but none provided the solution I wanted. Do someone know a way to handle that?
Thanks in advance.
You should use only JS for changing preview of picture in page.
Like this
If you want it to also change on server side then you should use AJAX solution. This solution are more complex, but you can find a lot of tutorials in internet.

Upload Image without Page Refresh & then Display Image

I have a button that allows a user to select an image - shown below:
<div id="fileUpload">
<form id="joinPhotoUploadForm" method="POST" enctype="multipart/form-data">
<input type="file" id="file"/>
</form>
<div id="fakefile">
<img src="../../images/button-grey-enhanced.png" id="usePhotoSubmit" alt="BROWSE for Photo">
<span id="usePhoto">BROWSE</span>
</div>
</div>
I then need to upload the image to the server and display the image on the same page without a page refresh. I've tried the following:
$('input#file').change(function() {
$('form#joinPhotoUploadForm').submit();
});
Any advise on how I can get the image upload and displayed on the same page without a page refresh?
thx
You could use the FileReader and File API available in modern browsers to read the file client side before uploading it and display a preview then allow the user upload after they've verified the preview. You can also implement drag and drop with an image from their desktop to the browser instead of a traditional file select input.
Here is a tutorial for it: http://www.html5rocks.com/en/tutorials/file/dndfiles/
In older browsers you can just fall back to a traditional file input with a page reload.
you can try the solution provided in this tutorial
http://www.youtube.com/course?list=EC7C25B2F18F68F3EF&feature=plcp
Have you considered a fancy uploader like Uploadify or Pupload?

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"?

Php form retaining input about file upload if captcha fails

I would to know how to retain information entered for photo upload in php form if either captcha or invalid entry occurs. All other fields retain their input, with the exception of upload path, i.e. person enters "C:\Users\inc\Desktop\3.png" and path is lost if either occurs during form submission (...php5#error)
<div class="photo">
<li class='field_block' id='field_35_div'>
<div class='col_label'>
<label class='form_field'>Photo 1</label>
<label class='form_required' > </label>
</div>
<div class='col_field'>
<input type="file" name="field_35" id="field_35" value="" class='text_box1' onchange="fmgHandler.check_upload(this);">
<div id='field_35_tip' class='instruction'></div>
</div>
I tried adding echo, but this does not work.
File fields cannot be pre-populated. And you probably don't want the user to have to upload the file every time again and again anyway, as that may potentially take a long time.
Save the file somewhere on your server and remember either in the session or in a hidden input field which file it was. Display something like "file uploaded" next to the file selector. If the user choses to upload another file, overwrite the previously uploaded file, otherwise use the previously uploaded file.

Categories