compose and upload a new article using a custom form - php

I'm starting to learn about php trying to achieve the goal of make my first custom form. From what I understand, it's possible to collect data from a form you can compose and customize to get information like title, subtitle, uploaded images and of course the main text of a supposed post and collect them in an html file that the form itself plus a php file can store in a folder on my server.
what i would like to understand better is if i can compose the form making it be able to include the collected information between pieces of code i don't wanna write anytime in the fields of the form. i also need to understand how to make it be able to save the final html file with a different suffix like an increasing number at the end of the name to be sure no older file created this way is going to be lost cause of overwrite.
the best would be a way to leave the last 5 documents i upload using this method with the suffix between 1 and 5 and rise the number of the older files, but that would be super fancy. doing so i wanna basically make myself a sort of backoffice for my website. I'm sincerely just trying to learn and do it in the most easygoing way so feel free to suggest me good guides if you know some, it would already be a nice help.

what i would like to understand better is if i can compose the form
making it be able to include the collected informations between pieces
of code i don't wanna write anytime in the fields of the form
this is not clear. please give an example.
i also need to understand how to make it be able to save the final
html file with a different suffix
if you really need to save forms data on disk, and avoid collision. you can generate unique id while writing the file as file name
see http://php.net/manual/en/function.uniqid.php
or using a composer package to generate uuid. see https://github.com/ramsey/uuid
while i still recommend using a proper data store, database or in memory.

Related

Whats the best place to make the upload of a file before saving data

I have a simple form with some data to be saved to database but on this form I also have a file field.
I would to know what is the best place to make the upload of the file.
I thought to do it on beforeSave() but I dont have acces to $this->request->data('file_name') on beforeSave().
BeforeSave is for pre-save logic (validations, etc). You better use afterSave() for upload the file.
You could use a plugin like this one instead of inventing your own way of handling uploads:
https://github.com/davidyell/CakePHP3-Proffer
You can use this plugin as well or take a look at it how it does it.
https://github.com/burzum/cakephp-file-storage/tree/3.0
The plugin allows you to configure when you want to store the file:
Every developer might want to store the file at a different point or
apply other operations on the file before or after it is store. Based
on different circumstances you might want to save an associated file
even before you created the record its going to get attached to, in
other scenarios like in this documentation you might want to do it
after.
The plugin is using events for manipulating files before they get saved.

Heatmap in PHP, using mysql database

I've currently got a database with just short of 2000 client locations in Australia. What I am trying to do is to display this data on a heatmap, to be embedded into an existing website.
I've done a heap of looking around, and can't seem to find exactly what I'm after.
http://www.heatmapapi.com/sample_googlev3.aspx
http://www.heatmaptool.com/documentation.php
These are along the right lines of what I want to achieve, however I cannot see these working with data from a mysql database (require the data to be hard-coded, or uploaded through CSV files).
Has anyone come across this sort of thing before, or managed to achieve it?
Both of the examples you provide would potentially work.
With the first you would need to use the data you have to dynamically generate the javascript, or at least the values that go into the javascript.
The second is probably the better option. You would provide a path to the script that would dynamically generate a CSV file.

Direction needed - how to create a customizable files?

Here is the flow I would like to achieve:
I have a set of template .php files on server.
Visitor of the website chooses some options
Options are added to template files as pieces of code
Visitor downloads customized files (.zip should contain both default template files if not customised, and customized ones)
Original files remain intact for the next visitor
The flow is similar to bootstrap customisation, only difference is that on bootstrap visitors are editing .less files, and my visitors would edit .php files
I would appreciate if you could point me to general direction how to execute steps 3,4 and 5, baring in mind that user input will be stored in objects and arrays with javascript.
My php knowledge is very rudimentary, so I don't know where would I start.
If you need any clarifications please do not hesitate to ask.
Thanks!
If the users only modify configuration options it seems the best way forward would be to use JSON as it is very easy to encode/decode with JS & PHP with $.parseJSON (jQuery) and json_decode() & json_encode() respectively.
You would provide a form and then convert these user inputs into the JSON format and store it in a .json file which can then be zipped up and sent as a download to your user.
This would also allow users to upload their zip files and have the form inputs defaulted to their original options.
So lets run through how bootstrap does it.
Page to customize the bootstrap file, you would do the same with a similar form.
Once the form is POSTed, take the array of form inputs and validate them for malicious input, correct data types such as a number etc and remove any form inputs that should not be there.
Convert the input into JSON, without validation this is easy as json_encode($_POST).
Write that to a JSON file and allow the user to download it (zip it up if you want).
In your case you have multiple PHP files, this seems a bit off and you should not be using PHP files to hold this sort of variable configuration data, JSON seems a lot better suited.
Take a look at this SO question: What is the best file format for configuration file?
Three methods are mentioned, PHP's way, JSON & XML. Take your pick, but I'm willing to bet that the easiest one for you is JSON.
If you have not used JSON before, create a new PHP script and try out the functions with multi-dimensional arrays and have a play around.
PHP Documention for JSON
If you are using JavaScript to store user selections etc., I recommend you don't actually use php for any of the functionality you described. So do it like this:
Store templates as static text file with demarcated sections to insert user options. Make the demarcated sections different from php syntax like ###Your option here###.
This is easy.
Fetch the relevant template using AJAX and do a simple string replace in JavaScript on the demarcated sections.
Put the resulting php code into a <textarea> using JavaScript for them copy and paste into Notepad or something
Original files are static and code is client-side, so this is fine
So I understand you want to ship php scripts customised for the user.
Consider having php files that cater for all possible user inputs. Then capture the user options into a configuration file where the structure is predictacable and easy to create.
For example
# config.inc.php
# Created by script that captures user options
$config['allow_shipping'] = true;
then in the php script
# file1.php
include config.inc.php
if ($config[$config['allow_shipping'] === true) {
some_php_code;
}
Your steps will be
I have a set of .php scripts files on server.
Visitor of the website chooses some options
Options are sent to a server script which creates a config file.
Visitor downloads customized files (.zip should contain both script files and config file)
Original files remain intact for the next visitor (less the config file)
Some posts to guide you
How I can create installer for website. PHP mysql
and
PHP Installer Script
I would consider making default code snippets and have every "option" link to various required snippets. That way every time an option is selected it uses the right code.
To continue with #Crafter's example, you could have the following code snippets
if (
$config[
]
===
) {
}
I have done this once before when I tried something similar in Java, and even though it seems to be more work than it should, it will end up quite useful once you get a sizeable "database" of code snippets.
Hope it was of any use?
-Peter

Using JQuery or PHP to pull latest image?

I was trying to figure out a way to have JQuery (if possible) to display the latest image from a directory, regardless of its file name. Basically, I just want a simple tag to be updated to show the image. I realize this might not be possible with Jquery, so my fallback would be PHP.
I've been trying to research how to do this, but honestly do not know where to start. Any tips to get going would be greatly appreciated,
If you want to do it with JQuery, you can! You only need to add a few lines of logic to your app.
For example, when you add images to that folder, also write a text file with the name of the image just added. Then this text file is going to always have the filename of the last image uploaded. Finally, using JavaScript(with JQuery or anything else) you do one request to the text file and then you do the second request to obtain the image with that filename.
That is just a very basic example to try to share the idea of how to do it, then depending your particular case, you can add or change a lot of details to make this works for your case.
BTW, to do it with PHP, you are going to need to execute a system command, for example 'ls -ltrh' and parse the output to obtain the last filename.
You can use PHP script to get latest modified file in a specific directory.
Code can be found here: Get last modified file in a directory
You then can format that into JSON which can be passed through a jQuery AJAX call http://api.jquery.com/jQuery.ajax/
With those 2 techniques, you can manipulate the data to display the image.

How to group files together in upload

What is the most efficient way to group a bunch of file uploads together on the following screen? I currently have http://blueimp.github.io/jQuery-File-Upload/ implemented on one of my websites, and I use a hidden field with some random generated id that gets passed into the next page. This has proven to be unreliable, and I was wondering if someone had a better way of making sure all of the XHR files responses are grouped together on the next page?
Thanks.
Not 100% sure what you mean by grouping the files, but if you want to make sure the files are legit, perhaps using a SESSION key will help.

Categories