CodeIgniter upload files after preview - php

The flow of my multi-part form is basically Answer form -> Preview form -> Upload to database
Right now I upload my files in the Preview step since the POST data is fresh from the actual form. My problem is to access $_FILES once in the Upload to database function because var_dump($_FILES) returns array(0) {} once inside upload_to_database().
I thought of actually making hidden input[type=file] in the Preview step just so that my upload_to_database() will receive $_FILES, but this is not possible due to security reasons.
How do I solve this?
Edit
Sorry my post above was vague.
I actually want to upload once in the upload_to_database() function. I think it's bad practice to upload the user's files in the Preview stage unlike in Upload to database where they are sure of their answers.
The upload is currently in the Preview because right now it only works inside it. The $_FILES is empty once we go to upload_to_database().
How can I get a full and presistent $_FILES from Preview to upload_to_database()?

You could upload your files in the first step (when going to the preview form; like you are doing now), move them to a specific preview directory and move them to their final destination when you upload to the database.
I suppose you use something like sessions to keep track of the posted data so you could add the preview file path the same way.
Then you set up a simple cron job to delete all files older than xx hours in the preview directory every xx hours.
Edit: An alternative would be to do the preview completely client-side in javascript, like for example the preview of the questions and answers here on SO.

Isn't it possible to do something like this:
PREVIEW:
upload_to_database($_FILES['userfile']['tmp_name']);
DB:
function upload_to_database($file)
{
// work with file here
}
I mean to supply $_FILES as an argument to your upload_to_database function

Related

FIleType not being re-used when i submit my symfony form

I have a form, based on a non-entity, it's build with an XSD schema, my problem here is that when i submit my form, all is ok except the file ..
There is the treatement when I handle the submission of my form :
$value[$sectionName]['photo'] ='/Upload/'.$field->getClientOriginalName();
$field->move($dir, $field->getClientOriginalName());
With the documentation : https://symfony.com/doc/current/controller/upload_file.html
I supose that, to upload my file I just have to use the move() function.
The strangest thing is that the file is uploaded where I want, in my '/Upload' directory.
But when my page reload, in the form the FileType is blank with no file inside.
Thanks for the help by advance. And sorry for my bad english, hard morning !
This is the expected behaviour, so that if you submit the form again, the file won't be re-uploaded unnecessarily.
It sounds to me like your upload code is working fine but you have a small misunderstanding of how the HTML File input behaves. The input field temporarily holds the path to the file on your local machine - not the uploaded file.
If you want to see the uploaded file (path), you need to do some custom coding in your form to make that happen.

Could my file structure be causing the issues with my photo upload system?

I'm trying to create a photo upload system following Adam Khourys online tutorials.
http://www.developphp.com/view.php?tid=1395 - Upload multiple files video
http://www.developphp.com/view.php?tid=1304 - User profile upload
Most of the code is like for like, but with my own variable names used instead. One major difference is instead of creating 3 different file upload fields as in the multiple upload video I created my form to allow multiple selections from just one upload field.
Below is a brief overview of the files, their location and what the page does.
create_post.php located in root directory - the page that allows for image upload
image_uploader.php located in root > includes directory - the page that contains the upload form, made as a seperate file to be included into any page that may require photo uploads.
photo_system.php located in root > parsers directory - the page that handles image upload.
When I go to the create a post page, click the link to upload image(s) all works well, I can see the form, I can even select the files I would like to try and upload but when I click the upload button nothing seems to happen, when photo_system.php should load.
I've added a line of code to photo_system.php right at the top that should bring up an alert box once the page has loaded, in an attempt to debug my issues, but I never see this unless I go directly to image_uploader.php
This has lead me to believe that maybe my relative links to files were the issue so I've tried appending $_SERVER['DOCUMENT_ROOT'] to the start of the links to ensure it can alway locate them from the root directory, currently I have it set up with a hard coded $root variable and still I can't seem to get the system to work.
Is it likey that the file structure and the way the pages interact with each other is my problem? Could anyone take a look at my code for these three files and see if they can spot anything? I've been stumped by this for a week or so now so I think its time to ask for some outside help.
I have my 3 files saved in a txt format so the code can be viewed
http://www.vwrx-project.co.uk/temp_source_code/create_post.txt
http://www.vwrx-project.co.uk/temp_source_code/image_uploader.txt
http://www.vwrx-project.co.uk/temp_source_code/photo_system.txt
It turns out that in the end I was trying to nest two tables one inside the other so this was part of my issue.
The way that I had it set up I was including the photo upload system, image_uploader.php, as a table with an action requesting the photo_system.php file which worked when I went to the image_uploader.php page directly.
I also had in my main page where the images were being uploaded a form that had been set to onsubmit = false and this was canceling out the form action of the included file.
I only found this out when I decided to try and code it straight into one file else I'd still be stuck now.

how to preview uploaded image in php form

i created PHP form for my site, with image upload option on it.
but it only show the image name before image submitted, but i want to view/preview the image/file before submit so how this possible in this?
"file36":{"label":"Select/Upload a Student's Photo","accept":"jpg|jpeg|png|gif|bmt","files":true,"attach":true,"database":true,"maxbytes":204800,"fieldtype":"fileupload","required":true,}},
As far as I know this is not possible with php until you upload the image.
However, you are able to do so with jquery.
Check out http://blueimp.github.io/jQuery-File-Upload/
The old way is to upload the image to a temp folder, create an iframe and load the response there. Then link the temp image from the iframe to a hidden field in the final form with JavaScript. When you submit the form place the image in the right folder. Finally run a cron job every 24-48h to empty out the temp folder.
The new way is the File API supported in IE10+ and all modern browsers. Fallbacks in Flash and Silverlight exist for older browsers.
You obviously misunderstand how file uploads w/ PHP work.
PHP is server side programming language, meaning, it can only execute and access scripts, that are on the server. So, for PHP to access / analyze a picture in any way, it has to be on the server already.
You can upload the file in a temporary directory, analyze it and, if it suits your needs, move to a permanent folder. Otherwise, just delete it.

How to get file temporary path in php while file upload

I am using Uploadify jQuery plugin for file upload.Here there is no browse button used.
it will upload the file using ajax on click event.So There is no file temp path is displaying.So i want to make it display.
So, I try to get file temp path using $_FILES['uploadfile']['tmp_name']. But it just return something like this /tmp/phpclqHXj.But i want display actual temp path like C:\Users\Diamantino\Desktop\file.jpg.Can i get this using php. any option in uploadify
plugin, anyway to get jquery or js.Any help would be appreciated.
My Browse button.
Thanks
There is no way to get the full path to the file from your user's computer. You can get the file name (in your example that would be "file.jpg") using $_FILES['uploadfile']['name']. Security issues would arise if you could get information about your user's local folder structure when they upload a file, so web browsers are very unlikely to send that data. If there is some browser that does send it, it won't appear in the $_FILES superglobal, nor will there be any other easy way to access is in php.
At the very least, you're looking for the filename found under:
$_FILES['uploadfile']['name']
Instead of C:\Users\Diamantino\Desktop\file.jpg, this will return the filename: file.jpg
However, with the use of simple Javascript, you may be able to accomplish getting the entire file path. With some logic on your end, you may be able to get the file path only when the file is uploaded. Here is an example, and some clarity on implementation.

How to restrict file types for a user in a web browser

Hi i have a question that is almost like other questions but with a small but very important difference.
How can i restrict file types for user with a popup showed in the browser.
The standard file input of html can limit that with a command but it does not work in all browser so it is not an option.
I do not want to validate the file extension after the user has chosen a file but before that, when he is choosing.
Founded this http://demo.swfupload.org/v220/index.htm, but it uploads the file after the user has chosen it, i do not want to do that, i want to upload it after the user submits the form.
So the problem looks like this that there are some ways of not letting the user send a not valid file but they are not good because, one is that i validate after the user have chosen the file and the other is that i upload the file before the user submits the form.
I just want a simple thing to not letting to "chose" a not valid file, it does not matter if it will be made with java script or flash, just let it work.
maybe you have some ideas on how to do it because i searched the whole internet and did not found any solution.
Uploadify supports that feature and is very customizable. You should have no problems setting it up in a way that it uploads the file when the user submits.
you can put a condition on the action file of php where all the fields get by submit the page.at that time you can put validation for the file type. if the file type is not as you mentioned then redirect the error message and let user to make try again.
Thanks.
<input type="file" accept="text/html,image/jpeg">
Also: Mozilla`s doc
Not sure if that's what you're after, but Flash FileReference.browse() method has a FileFilter structure as an input argument, and there you can limit the types of files that the upload dialogue will show.
flash.net.FileReference

Categories