php save json to file
How to safely write JSON data to file using PHP
I’ve got HTML form for editing images. All data is stored in JSON. When I change current image, I want to save changes, through PHP script, to a text file. If I return to previous image, this configuration will be send again from this file to the form.
How to write/read this kind of data safely. Where and how effectively check data to prevent some JS/PHP code injections?
I have attached some concept code below:
JavaScript (using jQuery):
PHP example (read-write.php):
3 Answers 3
The problem with your code is that it won’t work, security issues aside. You must either serialize the data, or encode it to json BEFORE storing it in a file, ie. like this:
Serialising works like this:
As long as you make sure that the path you read/write to is correct, there are no code injection problems with this code. The only potential problem is if you can choose what file to use (rather than hardcode «config.txt» into the code). Then you’d have to validate to make sure the file is in a given directory etc.
First of all: JSON is not JavaScript and vice versa. And JSON is even not a proper subset of JavaScript.
Besides that, since you neither interpret some user input as PHP nor some output as JavaScript, there is no need to worry. But don’t forget to specify your output properly:
I would always check the data returned to see if it is in a format I expect. Say you are saving an image. Check it using MIME checks etc. to make sure that it is an image. If you just save data as is on the server you could open the door for some potential security issues.
If you mean that you just save data about which images was viewed it could still pose a problem depending on how and where that data is accessed and used. So if you except an integer and nothing more, make sure that the data you receive and save is an integer and nothing more.
saving json data to json file using ajax PHP
My json file looks like this:
count_click.json
now I open this file using
update.php
I get error in the browser console:
But the file is there. When I go to this http://127.0.0.x:3xx9/update.php in the browser, I see the contents of the php fine perfectly fine.
2 Answers 2
You could edit your PHP:
Maybe you find some other methods to improve security (for example only allow post from own domain. ).
file_get_contents(«php://input»); Why? You are already sending a Post with data, no need to complicate things with a stream.
Also file_put_contents needs the path to the actual file on disk, not a URL!
Simply echo something out to see if you are actually getting anything.
If that doesn’t work, try accessing the endpoint with your browser (not the file as that does not need PHP for the browser to read it).
Point your browser to http://127.0.0.x:3xx9/update.php and on your server, simply
echo «Request received!»;
If you see that in your browser, your endpoint is working and you can continue troubleshooting. If you don’t, then this is beyond JS and PHP and probably has to do with your server’s settings. If you are using RStudio’s Shiny Server, then that does not work for PHP
In any case, your endpoint should always return something when called. Not just save the file. It is just good practice.
how to convert jsonarray to jsonobject in php and save as json file
i want it to save as jsonobject
4 Answers 4
Is this what you are looking for.
Fetch the mysql result as an object. Dont bother converting it.
i found the answer
<"title":"tatoo","author":"lakmal","article"A tattoo is a form of body modification, made by inserting indelible ink into the dermis layer of the skin to change the pigment.">,>»title»:»dog»,»author»:»lakmal»,»article»The domestic dog (Canis lupus familiaris)[2][3] is a subspecies of the gray wolf (Canis lupus), a member of the Canidae family of the mammalian order Carnivora. The term «domestic dog» is generally used for both domesticated and feral varieties. The «>,>»title»:»cat»,»author»:»chamikara»,»article»The domestic cat[1][2] (Felis catus[2] or Felis silvestris catus[4]) is a small, usually furry, domesticated, and carnivorous mammal. It is often called the housecat when kept as an indoor pet,[6] or simply the cat when there is no need to distinguis»>,>»title»:»Automobile»,»author»:»lakmal»,»article»An automobile, autocar, motor car or car is a wheeled motor vehicle used for transporting passengers, which also carries its own engine or motor. Most definitions of the term specify that automobiles are designed to run primarily on roads, to have se»>,>>
Not the answer you’re looking for? Browse other questions tagged php javascript json or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How do I extract data from JSON with PHP?
This is intended to be a general reference question and answer covering many of the never-ending «How do I access data in my JSON?» questions. It is here to handle the broad basics of decoding JSON in PHP and accessing the results.
How do I decode this in PHP and access the resulting data?
7 Answers 7
Intro
Therein you might find:
These are the things that can be encoded in JSON. Or more accurately, these are PHP’s versions of the things that can be encoded in JSON.
Objects will be instances of stdClass, a built-in class which is just a generic thing that’s not important here.
Accessing object properties
Accessing array elements
Accessing nested items
Passing true as the second argument to json_decode()
Accessing associative array items
The value of key ‘foo’ is ‘foo value’
The value of key ‘bar’ is ‘bar value’
The value of key ‘baz’ is ‘baz value’
Don’t know how the data is structured
Read the documentation for whatever it is you’re getting the JSON from.
Hit the decoded data with a print_r() :
and check the output:
It’ll tell you where you have objects, where you have arrays, along with the names and values of their members.
Break the problem down into pieces that are easier to wrap your head around.
json_decode() returns null
This happens because either:
If you need to change the max depth you’re probably solving the wrong problem. Find out why you’re getting such deeply nested data (e.g. the service you’re querying that’s generating the JSON has a bug) and get that to not happen.
Object property name contains a special character
If you have an integer as property see: How to access object properties with names like integers? as reference.
Someone put JSON in your JSON
Data doesn’t fit in memory
If your JSON is too large for json_decode() to handle at once things start to get tricky. See: