So I am creating a web app, it has this upload form where you can upload android APKs, this part of upload form is specifically for uploading a revised version of the old APK(In others words new version of the application).
So I am doing all this in PHP, using CodeIgniter 2.x
Since user is uploading a new version of old Application, I want the website to make sure the application being uploaded for this old application by user has same package name (Android Package file name) e.g com.example.something
When the file upload is initiated, the file is parsed and the package name is put into the database, and a random generated hash of this file is stored in the session data as 'new_app_hash', the random hash of old file is in hidden input field called 'app_id'.
Now coming to the code:
My controller has this in place to do the callback thing for app_id, and compare its information with 'new_app_hash' which is in the session.
So, this callback is mainly for getting row information where the hash is 'app_id' and 'new_app_hash' in my database table, then compare the package names.
$app_id = $this->input->post('app_id',TRUE);
if($app_id!='0'){ //only if app_id is not 0, else I dont want this to happen
$this->form_validation->set_rules('app_id','New Package','required|callback_packagerevname');
}
Model function used in callback:
public function check_checkpackagerevname($app_id,$sessionhash){
if($app_id!='0'){
$old_package = $this->getPackageInfo($app_id);
$new_package = $this->getPackageInfo($sessionhash);
if($old_package['package_name']==$new_package['package_name']){
///ok fine they are same, return true.
return TRUE;
}else{
return FALSE;
}
}else{
return TRUE;
}
}
GetPackageInfo():
public function getPackageInfo($hash){
$this->db->select('*');
$this->db->from('appstore_packages');
$this->db->where('hash',$hash);
$query = $this->db->get();
return $query->row_array();
}
My callback is as follows:
function callback_packagerevname($app_id){
$sessionAppHash = $this->session->userdata('new_hash_app');
$checking = $this->storeM->check_checkpackagerevname($app_id,$sessionAppHash);
if($checking==TRUE){
return TRUE;
}else{
$this->form_validation->set_message('packagerevname','The uploaded APK must have same package name as the parent app that you are trying to upload.');
return FALSE; ////// new apk doesnt have same package name.
}
}
I have tried checking if the form_validation rule is working, when i add some other sort of rule like greater_than[0] it does show me errors, but the callback doesn't work when form is submitted, though the callback function seems to work fine, when i visit it directly from the URL, with the 'new_app_hash' set in the session and putting app_id in URL, it does return true or false accordingly.
What could be wrong?
P.S If the question is a little confusing please comment, I am working on the project for like past 12 hours continuously, so I am a little you know. :P
Edit: Ok it seems 12 hours really had it on me.
Thanks to praveen, I had named the callback method wrongly,
instead of :
function callback_packagerevname($app_id){
}
It obviously should have been:
function packagerevname($app_id){
}
You dont need callback_functionname as callback event just remove callback_ and will work fine...
function packagerevname()
{
$app_id = $this->input->post('app_id');
//so on....
}
you can use set flash data
/* $this->form_validation->set_message('checkpackagerevname',
'The uploaded APK must have same package name as the parent app that you are trying to upload.'); */
$this->session->set_flashdata('checkpackagerevname',
'The uploaded APK must have same package name as the parent app that you are trying to upload.');
and can print
echo $this->session->flashdata('checkpackagerevname');
for more visit
https://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Related
I'm trying to understand uploading files in laravel for a project I'm working on.
Sadly I failed when trying to achieve what I had in mind.
When a form is submitted you get to a site where all your inputs are displayed so you can check them again. So the form data has already been validated to this point.
On this site is also an input button to upload a file.
This means that both will go through the same controller method to be displayed after the validation is successful.
Problem here is that, even though the validation of the form works perfectly, I don't know how to validate the file.
Here you can see the store-method:
public function store(ValidateFormData $request, ValidateUploadedFile $requestFile, $param = '')
{
$validated = $request->validated();
$formData = $request->all();
if ($requestFile->hasFile('file')) {
$file = $requestFile->file('file');
// call to $requestFile/validate the uploaded file
}
return view('/validation', compact('formData'));
}
EDIT
I just need the file to be a pdf/postscript file and successfully uploaded.
This is the rule I have in ValidateUploadedFile:
public function rules()
{
return [
'datei' => 'required|file|mimes:pdf,ps',
];
}
I really hope someone can help me.
Please do not link any of the Laravel documentation, believe me, I've seen way to many sites about this topic but still don't quite get it.
Am creating a REST api with laravel that allows a user to select an image file from their android device, then upload it to the server. The mage is converted to base64 before it's sent to the server alongside other parameters. I want to convert this base64 to a file and store it on the server then generate a link that can be used to access it. here is what i have tried so far and it doesnt work: I have already created a symlink to storage
public function create(Request $request)
{
$location = new Location();
$location->name = $request->location_name;
$location->latitude = $request->latitude;
$location->longitude = $request->longitude;
$location->saveOrFail();
$provider = new Provider();
$provider->name = $request->provider_name;
$provider->location_id = $location->id;
$provider->category_id = $request->category_id;
$provider->description = $request->description;
$provider->image = request()->file(base64_decode($request->encoded_image))->store('public/uploads');
$provider->saveOrFail();
return json_encode(array('status'=>'success', 'message'=>'Provider created successfully'));
}
As already commented by Amando, you can use the Intervention/Image package, having used it for many years I can say it will do what you want and very well.
What I would also add though, is you may also want to consider, whether you indeed need to store it as a file at all.
Depending on what it will be used for, and the size etc, you could consider storing it in the DB itself, along with any other information. This removes the dependency on a file server, and will make your application much more flexible with regards to infrastructure requirements.
At the end of the day, files are just data, if you will always get the file when you get the other data, reduce the steps and keep related data together.
Either way, hope you get it sorted :)
I've been trying to solve this problem for days without success.
I am using blueimp Jquery File Upload and everything works fine but I need to save my pictures in different folders depending on a parameter send by url like /index.php?customer=160
<input type="hidden" name="customer" value="<?php print $_GET["id_cliente"];?>">
I created the hidden field in the form and got it in the uploadhanndler.php.
/files/'.$_POST['customer'].'
, here everything goes ok, the file is saved on the folder I wanted, but the next time I open the window /index.php?customer=160, the files listed are the files/ folder not the files/160/ folder or the number I want to list.
I realized I could use the PHP user directories, and the files are beeing saved in a folder like this 6afa0f7338b14aeed39eb7656f364b4e that comes from the session_id(), I tried then to change the session_id() with the number of the folder I want this way at the begining of the /index.php?customer=160
session_start();
session_id($_GET['customer']);
but the files are still beeing saved in folder 6afa0f7338b14aeed39eb7656f364b4e, and when I print the session_id() is 160.
PHP user directories is a good method to achieve what I want? what I am doing wrong?
Thanks in advance and excuse my poor english.
The code below will show you how to save a session ID called customer_id and then use it as a folder name. Please let me know if you need any more help.
// start (or restart) the session
session_start();
// set the customer id session var
$_SESSION['customer_id'] = // set this variable something that you've retrieved from the DB
// create a folder if it doesn't already exist
$dir = '/httpdocs/mySite/customers/' . $_SESSION['customer_id'];
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
even it has been a while since a last answer has been given to this question, I'd like to give a small update on that issue. Since version 5.17 of the jquery-file-upload library ther is a support for user-directories.
Within the UploadHandler.php you will find the option 'user_dirs' => false. Just set it to 'true' and you will have user-directories based on session-id.
If you want to have your own user-directories not based on session-ids, but e.g. based on user-id (your own defined Session key) you can proceed in the following way:
within the index.php file (as stated in the Demo of the jquery-file-upload library) you place following
right after "require('UploadHandler.php');"
class CustomUploadHandler extends UploadHandler {
protected function get_user_id() {
#session_start();
return $_SESSION[*<your own user id session key>*];
}
}
followed by:
$upload_handler = new CustomUploadHandler(array(
'user_dirs' => true
));
...and thats it...now you have your own user-directories for your uploaded files
I'm currently having users upload a MS Word Document where I am checking a version within the XML. The controller currently checks isValid() and then hits a library that does the parsing and extraction (since word is an archive). Now since it's technically "valid" already, I need to check the validity again through the library. What is the best course of action in Zend Framework for this?
Cheers from Kohana Land ;)
I think I understand what you are looking for.
You are currently calling is valid against the form, your file passes the form validation (correct size, extension ...) now you need to unpack the file and validate the contents.
I'm going to assume you already have the code to validate the contents and just want to understand how that might be used in the controller.'
public function anyAction() {
$form = new Form();
//test for $_POST
if ($this->getRequest()->isPost(){
//Test form for validity
if ($form->isValid($this->getRequest()->getPost()){
//will receive file upload (unless disabled in element) and filter form values,
//based on filters attached to the elements.
$data = $form->getValues();
//placeholder for whatever code is required to validate contents of file
$validateFile = new MyFileValidator();
//test for valid file contents
if ($validateFile->isValid($data['file']){
//Do some Stuff
}
//if file contents is not valid, display form and populate values with unfiltered values
$form->populate($this->getRequest()->getPost());
}
//if form is not valid, it should stay populated and display element errors
}
//if not post send form to view
$this->view->form = $form;
}
This example should provide the basic controller workflow for this type of problem. I hope it addresses your question.
So how can I pass a variable into a script that is not connected with symfony?
For example, I have variable $a, which I render in template a.html.php
How can I use this $a in some example.php script?
More close: I wanna make image uploading with TinyMCE image manager; but each user should have their own directory (which corresponds to user id). So I should pass variable user_id to config.php file of image manager.
So this directory depends on the user's session! How can I make different dirs to upload images for each user? Or can you advise me how to deal with this problem?
Maybe to use another text editor? Which one can I connect with symfony or to use different directories for different users?
You can store information in the session if you want to share it with other scripts. Make a var_dump($_SESSION) in example.php to see what you already have.
$formPath = 'nameBundle:name:name.html.twig';
$session = $controler->getRequest()->getSession();
$session->set('formPath', $formPath);
$session->set('neededVariables', $neededVariables);
return $controler->redirect($controler->generateUrl('show_result'));
I used this to handle data and read data function
public function showResultAction() {
$session = $this->getRequest()->getSession();
$formPath = $session->get('formPath');
$neededVariables = $session->get('neededVariables');
if ($formPath or $neededVariables)
return $this->render($formPath,$neededVariables); else
throw $this->createNotFoundException('The product does not exist');
}