Upload files, verify with input from form

MasterC 20 Aug, 2008
Hello,

I'm wondering if there is a way to setup Chronoforms to upload files submitted by my users and direct the files to a specific folder other than the default (com_chron../upload)? And then also have the files renamed according to user input? For example:

User1 logs in, gets the menu with the form, clicks on the link, and has 3 boxes presented to them. One box for their username, the second box for their last name, and finally the input box for the file they will upload.

The username and last name boxes are merely to pass a simple check to allow them to upload the file into their username_LASTNAME directory.

Right now I'm using a PHP script to do this outside of Joomla, but I'm hoping I can move it into Joomla. That script is attached. Is it too complex to just copy and paste the code into a new form, change a few parameters (such as the location of the uploads to /a/new/path) and have it "just work"? We would very much like to move this "inside" of Joomla for various reasons.

Thanks for any insight.

-Chad
GreyHead 20 Aug, 2008
Hi Chad,

There's no way to do this in the 'standard' ChronoForms - but you could fairly easily write some php in one of the OnSubmit boxes that would do this for you.

I'll take a closer look at your script in the morning.

Bob
cjmicro 20 Aug, 2008
You can use php script inside your forms (inside the proper tags) or in the onsubmit area. I don't know how to do that, but I know you can use php in those areas.

Cheryl
GreyHead 21 Aug, 2008
Hi Chad,

For the most part, your script just replicates what ChronoForms already does in handling the uploads. The only real difference is in showing the results. You might want to take the chunk of the script from lines 350 to 369 and put this into the OnSubmit after box for your form to display the uploaded form info to the student. (It will need some tweaking to work with the CF/Joomla variables.)

This script saves all the files into /home/user/uploads - so it's similar to ChronoForms. To save files in different folders you can copy and amend a chunk of code from the handle_uploaded_files() function at the end of chronocontact.php

Bob
MasterC 21 Aug, 2008
Thanks for the info Bob! I'll look into it, my PHP skills are, well, slim. But I have a co-worker who is more skilled in the art who may be able to make the necessary tweaks.

One quick downside I can think of is that I don't want to change the "default" for Chronoforms. I only need a hanful of forms to point to a different directory (the professor's home directory) for the file uploads only for those classes form. So changing chronocontact.php seems like "bad juju" since I only have a few forms that will use this condition.

Thanks again for the insight,

-Chad
GreyHead 21 Aug, 2008
Hi Chad,

You don't need to hack ChronoForms - just copy the code chunk and use it as a 'code template' to use in one of the OnSubmit boxes. Doing that won't affect any of your other forms.

Bob
MasterC 21 Aug, 2008

Hi Chad,

You don't need to hack ChronoForms - just copy the code chunk and use it as a 'code template' to use in one of the OnSubmit boxes. Doing that won't affect any of your other forms.

Bob



Got it, thanks that makes sense after I re-read it. 🙂

-Chad
revive 01 Oct, 2008
Hey Greyhead... do you think you could look at this post http://www.chronoengine.com/forums.html?cont=posts&f=5&t=11239 and point me in the right direction? I didn't want to keep tagging on the end of this post as it seemed to be heading in a slightly different direction than my form needs.

BTW, I tried to implement the code from the bottom of the chronocontact.php into the HTML of the template, within PHP brackets,.. and this is the error I get:
Fatal error: Cannot redeclare handle_uploaded_files() (previously declared in /home/thedaybr/public_html/components/com_chronocontact/chronocontact.php:575) in /home/thedaybr/public_html/components/com_chronocontact/chronocontact.html.php(319) : eval()'d code on line 11


Thank U!
Max_admin 01 Oct, 2008
Hi revive, this is completely off topic and not compatible with V3.0, I will reply there and lets ignore this post!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 02 Oct, 2008

Hi Chad,

For the most part, your script just replicates what ChronoForms already does in handling the uploads. The only real difference is in showing the results. You might want to take the chunk of the script from lines 350 to 369 and put this into the OnSubmit after box for your form to display the uploaded form info to the student. (It will need some tweaking to work with the CF/Joomla variables.)

This script saves all the files into /home/user/uploads - so it's similar to ChronoForms. To save files in different folders you can copy and amend a chunk of code from the handle_uploaded_files() function at the end of chronocontact.php

Bob



Hi,

So I am just getting back around to this project. I'm a little lost on what code to put into the OnSubmit box (I am using the before email since I won't be sending an email). I copied the code below:
<table align="center"class="table">
  <tr>
   <td class="table_header" colspan="2"><b>Your file(s) have been uploaded.  Please keep the following confirmation number: </b> <? echo($file_tag) ?> </td>
  </tr>

  <tr>
   <td class="table_body">
    <br />
    <? For($i=0; $i <= $file_uploads-1; $i++) {
       If($_FILES['file']['name'][$i]) {
         $file=$i+1;
         Echo("<b>File #".$file.":</b> <i>".$_FILES['file']['name'][$i]."</i>   <br/><br/>\n");
       }
    } ?>
    <br />
    <a href="<?=$_SERVER['PHP_SELF'];?>">Go Back</a>
    <br />
   </td>
  </tr>
</table>

<? $uploadedfile = JFile::upload($original_name, $Class.'uploads'.DS.$name.DS.$uid);>


So how do I find the variables that CF/Joomla uses to match up with mine? Also, I'm pulling 2 variables from my form from drop down boxes. One for the class section and one for the week the homework is for (here's my form):
<style type="text/css">
@import url(templates/system/css/all_forms.css);
</style>

<h2>Use this form to upload your homework</h2>

<fieldset>
<legend class="legend-title">Choose your class</legend>
<select name="Class">
<option name="1703" value="Section 1703">Section 1703</option>
</select>
</fieldset>

<fieldset>
<legend class="legend-title">Identify yourself</legend>
Last Name: <input type="text" name="lname" label="Last Name" />
uNID: <input type="text" name="uid" label="University ID" onclick="this.value = 'u';" value="uIDNumber" />
</fieldset>

<fieldset>
<legend class="legend-title">Upload your homework</legend>
Upload file 1:<input type=file name=upfile1 /><br />
Upload file 2:<input type=file name=upfile2 /><br />
Upload file 3:<input type=file name=upfile3 /><br />
Upload file 4:<input type=file name=upfile4 /><br />
Upload file 5:<input type=file name=upfile5 /><br />
</fieldset>

<fieldset>
<legend class="legend-title">Describe your homework</legend>
Which week are you submitting homework for?
<select name="Week">
<option name="week1" value="week1">Week 1</option>
<option name="week2" value="week2">Week 2</option>
<option name="week3" value="week3">Week 3</option>
</select>
<br />
Which assignment is this?
<textarea></textarea>
</fieldset>

<fieldset>
<input class="button" type="submit" value="Upload" />
<input class="button" type="reset" value="Clear Form" />


I'm trying to pull the selected option for the Class and for the Week. The Class will determine which main directory the file gets uploaded to (eg: /home/classes/1703) and the week value will determine which subdirectory it goes in (eg: /home/classes/1703/week3).

Any additional help would be much appreciated.

-Chad
Max_admin 03 Oct, 2008
Hi Chad,

you cant use the before email if no emails are setup, It will not run!

I look at your code and I cant find the variable $file_uploads defined anywhere!

hint: you should start with some simple code on the form submit first to check that files are uploaded and you can retrieve their names then jump to the advanced work you need, I see you are doing everything at once and this will make it very hard to find what does not work.

Cheers

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 03 Oct, 2008

Hi Chad,

you cant use the before email if no emails are setup, It will not run!

I look at your code and I cant find the variable $file_uploads defined anywhere!

hint: you should start with some simple code on the form submit first to check that files are uploaded and you can retrieve their names then jump to the advanced work you need, I see you are doing everything at once and this will make it very hard to find what does not work.

Cheers

Max



Indeed, thanks. I did notice some of my variables were pulled from the previous script. I changed a couple of those, and wrote some different code to echo back to me just so I can see that it is indeed working.

I will move my code to the after email OnSubmit then, thanks for the tip!

-Chad
Max_admin 03 Oct, 2008
great, after you finish it will be great if you added the final code here for everybody!

Regards

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 06 Oct, 2008

Hi Chad,

you cant use the before email if no emails are setup, It will not run!

I look at your code and I cant find the variable $file_uploads defined anywhere!

hint: you should start with some simple code on the form submit first to check that files are uploaded and you can retrieve their names then jump to the advanced work you need, I see you are doing everything at once and this will make it very hard to find what does not work.

Cheers

Max



Hi,

So I am now just focusing on the file uploading to a different directory. I copied the function from chronocontact.php and pasted it into my On Submit code box (after email sent) box. I removed all the other code in that box I was working with, and changed the location of the upload directory thusly:

function handle_uploaded_files($uploadedfile, $filename, $limits = TRUE, $directory = FALSE)
{
    $uploaded_files = "";
    $upload_path = '/home/username/public_html/uploads';
    if ( is_file($uploadedfile) ) {
        $targetfile = $upload_path.$filename;
        while ( file_exists($targetfile) ) {
            $targetfile = $upload_path.rand(1,1000).'_'.$filename;
        }
        move_uploaded_file($uploadedfile, $targetfile);
        $uploaded_files = $targetfile;
    }
    return $uploaded_files;
}


I turned on debugging, and tried to submit an uploaded file. I got back:
_POST: Array ( [Class] => Section 1703 [lname] => mylastname [uid] => u01234567 [Week] => week1 )
function handle_uploaded_files($uploadedfile, $filename, $limits = TRUE, $directory = FALSE) { $uploaded_files = ""; $upload_path = '/home/username/public_html/uploads'; if ( is_file($uploadedfile) ) { $targetfile = $upload_path.$filename; while ( file_exists($targetfile) ) { $targetfile = $upload_path.rand(1,1000).'_'.$filename; } move_uploaded_file($uploadedfile, $targetfile); $uploaded_files = $targetfile; } return $uploaded_files; } 


But no file was uploaded to that directory. Apache has rwx for that directory, and there is world x on the home directory for the user (to allow permission to enter the directory). To ensure it wasn't a permission issue, I also tried /tmp; still no file is uploaded. But if I look in the directory that chronocontact.php is calling (components/com_chronocontact/upload) then I find my files.

So it would appear that my "override" isn't working. Any hints?

Thanks!

-Chad
Max_admin 06 Oct, 2008
Hi Chad,

please don't use the handle_uploaded_files function because this is outdated, we now use the core Joomla file system functions so Joomla controls this process the it should work with the same joomla user!

go up in the file and you should find some lines start with JFile and JFolder which has many functions to deal with file system.

this example code is from the new ChronoComments :


if (JFile::exists(JPATH_ROOT.DS.'plugins'.DS.'content'.DS.'ChronoComments.php')) JFile::delete(JPATH_ROOT.DS.'plugins'.DS.'content'.DS.'ChronoComments.php'); 

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 06 Oct, 2008
I don't seem to have JFile or JFolder in my chronocontact.php file. According to Joomla I have version: 2.5 J1.5 RC3.1 from date: 16.04.2008 Is there a newer version available for licensed use?

Thanks,

-Chad
Max_admin 06 Oct, 2008
Hi Chad,

of course V3.0 is available for licensed use, you will need to send me through the contact us to get the licensed patch for V3.0 but you can work with the normal V3.0 for now, however upgrading from V2.5 to V3.0 will need to remake your forms by copying all settings, code..etc!

you still can use my code above on any J1.5 version!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 06 Oct, 2008
Thanks, I have sent a message. However, I don't understand how I can use the code above. Can you elaborate on your example how I might use it?

Thanks!

-Chad
Max_admin 07 Oct, 2008
Hi Chad,

tell me what you want to do with the file , the above 2 lines are joomla code to check if some file exists and delete it if so, a full list of functions is here :
http://api.joomla.org/Joomla-Framework/FileSystem/JFile.html
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 07 Oct, 2008
I'm trying to create a homework upload form. The student will select their class, input their name and ID, select the files to upload, choose which week the file is for and provide a description if necessary. Then hit submit.

On the backend the file will be uploaded to the appropriate directory on the filesystem based on the class variable the student selected (I'll give an example below). If one doesn't exist, a directory will be created for the week selected from the form (week1, week2, etc) and a subdirectory will be created with the students last name and ID (lastname_ID) and the file will be renamed to date-filename.ext where date is in the format hh_mm-mo.dd.yyyy

So if I were Roger Rabit, id u1234567 and I had homework final_project.doc for week 5 to upload for class WRTG 1703 I would do the following:

Select the class WRTG 1703 from the first drop down;

Type in my Last name in the input box, and my UID in the other input box;

I would click browse and select my homework assignment, if I had more than 1 (up to a max of 5) I would select the others in the same fashion;

I would select the week the homework is for, week5;

I would hit the Upload button. I would be greeted with a screen that returns the status of my upload (success / failure, if failure, why) and the resulting filename for confirmation that my file was indeed uploaded.

On the backend php would create the necessary folders if required and my homework would be uploaded to the directory /home/classes/WRTG1703/week5/Rabit_u1234567/23_59-10.07.2008-final_project.doc Some checking probably needs to be enabled to ensure the student doesn't overwrite their own file, however I think using the date-filename will reduce the likeliness enough that it really doesn't matter.

The other form that needs to be created to complete the project is a form to allow the graded homework to be uploaded to the students home directory and have some new name, something like final_project-corrected.pdf so the student can get feedback for their assignments.

I hope that helps clear up any confusion and possibly help to give you an idea of what I am looking to do.

Thanks!

-Chad
GreyHead 08 Oct, 2008
Hi MasterC,

I would deal with this piece "Type in my Last name in the input box, and my UID in the other input box;" by having the students as registered members on the site and requiring them to login to upload - this will save you a lot of hassle in the long run in sorting out problems with mistyped UIDs.

You then just need two drop downs and a file upload box. If students hsve more than one piece to upload then have them refresh the form and start over. That way you are just dealing with one file at a time.

Then you have all the info you need to write the handling code in the OnSubmit After box. This isn't too difficult but it's not trivial either. It's not something that I can write here for you off the top of my head. You have the basic example of the CF code and Max has pointed you to the Joomla Jfile docs.

Bob

PS the graded work upload needs a second similar form for the teacher to use to re-submit. Probably this should re-upload the file with a slightly modified name and email a link to the student to pick it up. You might also want to capture and record the grade if this is part of the same system.
MasterC 09 Oct, 2008
Thank you for the response! I am using their name from Joomla to fill in the box for Last name, but we don't have their ID as part of their authentication credentials so they will still have to fill in that box; but I have taken steps to minimize the possibility for error in that box.

I'll be working on the form using the examples posted in this thread, if I have any more problems or questions I'll post back. Thanks for all the help so far!

-Chad
MasterC 27 Jan, 2009

hint: you should start with some simple code on the form submit first to check that files are uploaded and you can retrieve their names then jump to the advanced work you need, I see you are doing everything at once and this will make it very hard to find what does not work.


Taking these words of wisdom I'm working on simply echoing back out the name of the uploaded file. Nothing fancy at all, but when I try to get the variable and echo it out, when I save my form, I get a page that says I can't post to index2.php (an apache error page). If I refresh and get back into my Administrator backend, the changes I made to the On Submit - after sending email box are gone. Here's the simple code I'm trying to use right now:
<?php
$hw = JFile::getName('homework');
echo "Your filename is $hw";
?>
I've tried variations, none of them produce usable results.

Any hints on just getting this part working would be much appreciated.

-Chad
GreyHead 27 Jan, 2009
HI Masterc,

I've no idea why you are having saving problems in the Back-end.

But . . . ChronoForms moves the files from the temp upload area into an uploads folder inside the component. The file info is in the $attachments array and it is this that you should probably be checking. There are several recent examples of code snippets for renaming or moving uploaded files that you may find useful.

Bob
Max_admin 27 Jan, 2009
Hi Chad,

do you get some error code ? you can use it to search the forums here, you can ask your host too and use the error log!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 27 Jan, 2009
Thank you for the responses. I ended up finding some info on grabbing variables, here's what I ended up using the echo back the name of the uploaded file:
<?php
$filename = JRequest::getVar('homework');
echo "Your file is $filename";
?>


And with this hopefully I'll be able to move forward a bit! 😀

Thanks for the info,

-Chad
Max_admin 27 Jan, 2009
Great, glad you figured it!🙂

Cheers,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
MasterC 29 Jan, 2009
Hello,

I'm like a thorn in your side I'm sure! 🙂

So I'm having some pretty good success figuring some of this out, but I'm hitting a brick wall while trying to move a file. Here's what I've got:
<?php
$filename = JRequest::getVar('homework');

JFolder::create(JPATH_COMPONENT.DS.'uploads'.DS.'deleteme');

$newdir = JPATH_COMPONENT.DS.'uploads'.DS.'deleteme';
echo "Your file is $filename <br />";
$safefile = JFile::makeSafe($filename);
echo "We are now copying $safefile to $newdir";
JFile::upload($safefile, $newdir.DS.$safefile);
?>


I get the correct variables echoing out, but I get an error that says:
Warning! - Failed to move file.

The echo statements echo back the correct path and filename. So I assume it's "hanging" on the last line there:
JFile::upload($safefile, $newdir.DS.$safefile);


Should I be using something else? Groking the JFile doc page it seemed like that was the correct function to use.

Right now I'm just trying to keep it simple while I practice and learn. I just want to move the file from one directory to the other, once I get that working and figured out I can introduce more complexity (if/else statements, etc) to make it more functional.

Any thoughts?

Thanks!

-Chad
GreyHead 29 Jan, 2009
Hi MasterC,

ChronoForms has already moved the uploaded file - I think you just want the php file move command.

Please check the file moving and renaming post from a couple of weeks back.

Bob
MasterC 29 Jan, 2009
Thanks! That was it! I thought the JFile::upload sort of "replaced" a move command or was an all purpose move command (similar to Linux mv used to rename or move a file).

Excellent, I have it working with:

<?php
$filename = JRequest::getVar('homework');

JFolder::create(JPATH_COMPONENT.DS.'uploads'.DS.'deleteme');

$newdir = JPATH_COMPONENT.DS.'uploads'.DS.'deleteme';
$olddir = JPATH_COMPONENT.DS.'uploads'.DS.$formname;
echo "Your file is $filename <br />";
$safefile = JFile::makeSafe($filename);
echo "We are now copying $olddir.DS.$safefile to $newdir.DS.$safefile";
JFile::move($olddir.DS.$safefile, $newdir.DS.$safefile);
?>


In my echo statements it doesn't look pretty because it doesn't "interpret" the DS to be a directory slash, it actually prints out DS; but the final function to move the file works so I'm golden. Time to move to more things I don't understand. I'll be back I'm sure! 😀

Thanks again,

-Chad
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger