Set image uploader field using php - javascript

I am trying to save user data while on later i give the option to user to see his unsaved work.I got all the data but image upload file is missing.I i am trying to save file field like this in DB :
$files = $_FILES;
$__fields['file'] = json_encode($files);
Then i save this field to DB.I tried to set the file field like this but no luck.
$files = json_decode($row['file']);
jQuery('#myimg'). val('<?php echo $files->name;?>');
what i am doing wrong ? I want that when customer see his unsaved data the file field is filled like he leaved it.Only file field is creating issue for me.Thanks

When a user unsaved a file means he doesn't uploaded to your server. the file is being in his local computer.
And Its not possible to take that it from php side. unless you upload the file.
Do one thing when the user select file then upload it to your server and keep it in a temporary place and then do your job.
hope this helps

As per the documentation, $_FILES is an associative array of items uploaded to the current script via the HTTP POST method. See manual
What you need to do;
Grab the contents of $_FILES on the initial HTTP POST.
Validate the image
Save the image to your server
Store the location of the saved image in the database, to be read later
OR
Use a 3rd party API
http://api.imgur.com/
See http://www.w3schools.com/php/php_file_upload.asp
(sorry for a w3schools link)

Related

Why is my $_FILES empty given the following code?

In my frontend PHP page I have a simple upload file input object:
<input type="file" v-on:change="fileAdded">
My fileAdded function within my JS file simply takes the event object (e) and grabs the file in order to obtain other information regarding the file itself:
if (e.target.files !== undefined) {
v.data_file = e.target.files[0]
}
I then put the data file and a request code into a fetch statement to my backend:
fetch("pages/gp/gpBackend.php", {
method: "POST",
body: v.form_data_obj
})
Within my backend php page I use the $_FILES array to grab information like the filename and extension of the file to then pass along to a python script that I have which will unpack the data in the file itself.
During this phase of operation of my webpage, everything works fine, I send the response back to my frontend, handle the data accordingly and move on.
The rest of my frontend requires the user to input certain data before submitting a second post back to my backend with a different request code.
In that instance I send another FormObject with all the needed data to my backend in the same manner I did with my file upload POST. However this time my $_FILES is now empty and I cannot access the file name or extension of the file I uploaded earlier.
I cannot seem to find out why this is the case? I have a near identical set up on another frontend/backend php page I have for the same site that through both POSTS maintains the files in $_FILES to be used however often it is needed. But for this page, without me being able to tell the difference, when I POST another request to my backend the second time $_FILES is empty.
I can provide any additional details that would be needed to answer this question so please let me know. I do know it is not a file size issue as not only do I have the .ini configured for an admin (me) set file size limit, but the file I'm currently using as an example during development is significantly smaller than the max file size on my web server.
Thank you for any help anyone can provide!
PHP $_FILE must serve with a with enctype as this way.
<form action="upload.php" method="post" enctype="multipart/form-data">
Hope this help: https://www.w3schools.com/php/php_file_upload.asp

How to handle auto-uploaded file in cancelled form submission

This is my scenario:
I have a form with some information like full name, birthday... and one input is filesupload with an auto upload option
If I use auto aupload, files will be uploaded to server before the form is submitted. If user cancels form submission, The db record is not created hence I do not need the file uploaded anymore and this lead to trash files on my server.
Is there any way I can handle this so i do not have too many trash files in the upload folder on the server?
Form your question i think what you want to do is to be able to delete a file if the the form data is not submitted and the file has auto-uploaded right?...
These are two ways to achieve this:
1. Do not auto-upload in the first place
There is no real reason why you should upload the file itself to server FILE_UPLOAD_FOLDER. Instead, convert the file into a base64 string which you can save in your db instead of using the file-path as link. When you want to render you can convert the string back to a file
2. Create a method that listens to the cancel button click.
I would assume that you have a variable that holds FILE_UPLOAD_PATH, hence just create a javascript function to delete the file and put it in the onClick attribute of the cancel button.

How to Upload,display and save image in a database table using jQuery javascript

How to upload , display and save image in a database. jQuery JavaScript using API calls .
I have four fields in db
File I'd
Filename
Filesize
Filepath
I don't see any mention of server side languages such as PHP listed here, but you need some sort of server side implementation to upload an image and deal with the database.
Ill help you go in the right direction so you know what to look up and look for.
Uploading an image
To upload an image you would conventionally use a form element with an input of type file. The form action would be your php upload script. The upload script can be complex, but they tend to be the same once you get the hang of it. Take a look at this page: https://www.w3schools.com/php/php_file_upload.asp. It has a full php upload script.
Store in database
To 'store' the image in the database you just need to save the path to the image. This means you will need to setup your SQL database, and a table. Then use PHP's SQL database functions to INSERT the link into the table, along with your other data.
Display the image
To display the image you will need to use a php script to gain access to the database, and then use a specific ID to get the image path out of the table and output it to the page.
Conclusion
Uploading files is an age old problem, and is tough until you've done it a few times. In the comments i saw mention of dropzone.js, which is really great for handling the uploading of multiple images but it does not actually do the uploading, you still need to write some php. Dropzone is purely a front end tool.
Hope that helps you move in the right direction!

i want my content to be saved in the txt file in my system and then it get copied to the another folder

i am working on javascipt ,i want the data which is entered by the user in textarea get save in some txt file in my system in some specific folder. can anyone please suggest the solution of this problem in javascript code or jquery .
JavaScript cannot access file system of user. So, you cannot store the data entered by user in his system.

Image insertion in mysql database without uploading(images stored in folder in sequence)

How to insert an image in mysql database without uploading image(uploading-using html tag type=file).The images are stored in a folder in sequence.
I am using html,javascript and php.
please post answer if you know.
you might want to then just make a reference in the db a file path for example. if you have solved how to upload then just add that in. remember, you might also want to add some form of numbering to the file so you are certain the file will be correct. that way the file system wont be destroyed the first time some one uploads a image that is the same as the last one
You cannot save any image to a DB if you do not send information about that file...
If you are in a Web Browser, you could ask for the BASE64 of the image, and save that string to your DB.
Here is a question that will help you understand what I am trying to explain you.
You ask the user to enter the BASE64 that can be get from any online webpage that does this. This page does what I mean.
I hope this answered your question !

Categories