Forums

Upload photo

dianaj59 02 Jan, 2009
I want to have something on my form that will allow the user to upload a photo.
My form is simple html. How can I do this?
I want to save the photo in a folder with the name of two other fields the user completes.
Thanks for any help.
GreyHead 02 Jan, 2009
Hi dianaj59,

You can use a File Upload field to upload an image. Set the types and sizes of images that you want to allow in the File Uploads tab and add enctype='multipart/form-data' in the Form Tag Attachment box in the General Tab.

By default the files will be saved in the com_chronocontact/uploads folder. There was a recent post I made about moving files using code in the OnSubmit box. And Emmanuel posted a full plugin to handle image uploads just before Xmas.

Bob
dianaj59 03 Jan, 2009
Thank You.
I don't really understand what should go into the file uploads tab under field names to set the types and sizes of images.
I have an example of file_2:jpg|doc|zip{222222-1}, but where does this come from file_2? Is this just a generic name? I understand the jpg or doc or zip part of it, but for the name that should go with the photo, should that be a reference from within the form.....for example lastname, firstname:jpg|doc|zip{222222-1}??

Also what sort of html code do I put on my form that tells a user to upload their file? I will search for that, but thought I'd ask here anyway.

I will go read the items you referenced now and see if that answers my questions.
dianaj59 04 Jan, 2009
Please disregard my last post.
I entered enctype='multipart/form-data' into attachments under the general tab.
I entered [lastname][:]firstname]:jpg|doc|zip{222222-1} into name and size field under file upload tab.
My html code is:
<td>Please upload your photo:<br><input type="file" name="datafile" size="40"></td>


However I can not find my picture in the com_chronocontact\uploads folder.

Any ideas? thanks.
Max_admin 04 Jan, 2009
Hi diana,

in your case and based on your form code you should add:

datafile:jpg|doc|zip{222222-1}
at the name and size field!

Regards
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
dianaj59 04 Jan, 2009
That is it! That's what I didn't quite understand. It works great...
One more question.
How can I change the following line so that the data entered into two fields on the form are attached to the picture. Specifically "firstname" and "lastname"?

<td>Please upload your photo:<br><input type="file" name="datafile" size="40"></td>


Thanks.
GreyHead 04 Jan, 2009
Hi dianaj59,

You can't do it in the FormHTML because when that is written you don't know what the firstname and lastname are. You'll have to do a file rename in the OnSubmit Box.

It will be something like this
<?php
$name  = JRequest::getString('first_name', '', 'post');
$name .= " ".JRequest::getString('last_name', '', 'post');
$file_name_old = $attachment['datafile'];
$file_name_new = basename($file_name_old);
$file_name_new .= DS.$name;
rename($file_name_old, $file_name_new);
?>
NB Not tested and may need de-bugging

Bob
dianaj59 04 Jan, 2009
I tried your code as follows:(changing variable names as per my application).
<?php
$name  = JRequest::getString('firstname', '', 'post);
$name .= " ".JRequest::getString('lastname', '', 'post);
$file_name_old = $attachment['datafile'];
$file_name_new = basename($file_name);
$file_name_new .= DS.$name;
rename($file_name_old, $file_name_new);
?>


and got this error:
Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\gcc\components\com_chronocontact\chronocontact.php(371) : eval()'d code on line 3

The error points to this line of code in chronocontact.php:
	     * Run the On-submit 'pre e-mail' code if there is any
	     */
	    if ( !empty($rows[0]->onsubmitcodeb4) ) {
			eval( "?>".$rows[0]->onsubmitcodeb4 );
		}
		if($paramsvalues->savedataorder == 'before_email'){
			if ( !empty($rows[0]->autogenerated) ) {
				eval( "?>".$rows[0]->autogenerated );
			}
		}


I'm a little confused on this part of your code:
$file_name_new = basename($file_name);
$file_name_new .= DS.$name;
rename($file_name_old, $file_name_new);


Should the rename be
rename($file_name_old, DS.$name);


I'm just guessing here....thanks for any additional help.
GreyHead 04 Jan, 2009
Hi dianaj59,

Apologies, I missed a couple of quotes after the 'post's in these lines:
$name  = JRequest::getString('firstname', '', 'post');
$name .= " ".JRequest::getString('lastname', '', 'post');

And there's a second error too. The correct code is
$file_name_new = basename($file_name_old);

You can put the rename into one line if you prefer - If I've got it right it would be
rename($attachment['datafile'], basename($attachment['datafile']).DS.$name);

Bob

PS When the error points to eval()'d code on line 3 it will be code in one of the input boxes.

PPS Will go back and fix my earlier post
dianaj59 04 Jan, 2009
Using this code:
<?php
$name  = JRequest::getString('firstname', '', 'post');
$name .= " ".JRequest::getString('lastname', '', 'post');
$file_name_old = $attachment['datafile'];
$file_name_new = basename($file_name);
$file_name_new .= DS.$name;
rename($file_name_old, file_name_new);
?>


I get this error:
Warning: rename(,file_name_new) [function.rename]: No such file or directory in C:\xampp\htdocs\gcc\components\com_chronocontact\chronocontact.php(371) : eval()'d code on line 7

I'm still not clear on what eval()'d code on line 7 means....
GreyHead 04 Jan, 2009
Hi dianaj59,

Almost there, needs an extra $ in
rename($file_name_old, $file_name_new);

Bob
dianaj59 05 Jan, 2009
Oh yes....I forgot the $
Now here is my newest code (or yours I should say):
<?php
$name  = JRequest::getString('firstname', '', 'post');
$name .= " ".JRequest::getString('lastname', '', 'post');
$file_name_old = $attachment['datafile'];
$file_name_new = basename($file_name);
$file_name_new .= DS.$name;
rename($file_name_old, $file_name_new);
?>



....And below is the curious error. The JOHN DOE is my firstname entry and lastname entry.
Warning: rename(,\John Doe) [function.rename]: No such file or directory in C:\xampp\htdocs\gcc\components\com_chronocontact\chronocontact.php(371) : eval()'d code on line 7

Incidentally, I am putting this code as

On Submit code - before sending email:(PHP code with tags)



It doesn't matter if it is before or after sending mail does it?

You are a patient man, thanks for sticking with me through this!
GreyHead 05 Jan, 2009
Hi dianaj59,

Two things wrong here, you haven't yet picked up the missing $ from my last post - that's why the file nanme starts with a /

The other is messier. I left a space in the names so we've got 'John Doe' in place of 'John_Doe' or 'JohnDoe' . . . We need to trim spaces off and then deal with the possibility of 'messy names' like 'de Souza' or worse 'John le Carré' . . . (I hadn't hought of those).

Trimming the spaces off is easy enough. Dealing with spaces and special characters é á etc inside the name is much more difficult. Here's a version that's a bit of a sledge-hammer approach, you may be able to find something more practical. (Or it might be better to use the user's username if they have one.)
<?php
$name  = trim(JRequest::getString('firstname', '', 'post')); // strip off any spaces from the ends
$name .= trim(JRequest::getString('lastname', '', 'post'));
$name  = strtolower($name); // switch to all lower case
$name  = rawurlencode($name); // encode any special characters??
$file_name_old = $attachment['datafile'];
$file_name_new = basename($file_name_old);
$file_name_new .= DS.$name;
rename($file_name_old, $file_name_new);
?>
I'm not quite sure what you'll get with this. Should be OK with 'John Doe' names though, then test it with a few others.

Bob
dianaj59 05 Jan, 2009
I'm still not seeing what is wrong in regard to the rename line.

Here is exactly what I have:
rename($file_name_old, $file_name_new);


The $ is there.....isn't it? And yet I get that message as posted.
Where am I missing the $ ???
GreyHead 05 Jan, 2009
Hi dianaj59,

Sorry that looks OK - I think I mistook the error. Please will you also check the 'baseline' line as I had a mistake in there at one point. If that doesn't work then please send me a form backup and I'll try installing and debugging it here.

Bob
dianaj59 05 Jan, 2009
When I create a backup of the form, it saves it as .CFBAK
This forum doesn't allow that sort of extention.
So, I'm sending you the html code for the form as an attachment.
GreyHead 05 Jan, 2009
Hi dianaj59,

Just zip the .cfbak file up and it will attach OK. The forum doesn't like any text files with html tags in them.

Bob
dianaj59 05 Jan, 2009
okay, here is the form.[attachment=0]Teacher_Registration(2).zip[/attachment]
GreyHead 05 Jan, 2009
Hi dianaj59,

This version is tested and does work - though the result is messy for John le Carré!
<?php
$name  = trim(JRequest::getString('firstname', '', 'post')); // strip off any spaces from the ends
$name .= "_".trim(JRequest::getString('lastname', '', 'post'));
$name  = strtolower($name); // switch to all lower case
$name  = rawurlencode($name); // encode any special characters??
if ( $debug) echo "name: ".print_r($name, true)."<br /><br />";
$file_name_old = $attachments['datafile'];
if ( $debug) echo "file_name_old: ".print_r($file_name_old, true)."<br /><br />";
$file_name_new = dirname($file_name_old);
$file_name_new .= DS.$name;
if ( $debug) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
rename($file_name_old, $file_name_new);
?>
I found (at least) one more typo and one incorrect command - not my best day, sorry :-(

Bob
dianaj59 12 Jan, 2009
PERFECT !!!
This works wonderfully. I can mess around a bit with the odd name issue. For my needs I don't think that will be a big deal.
Thank you so much for all your work on this.
dianaj59 14 Jan, 2009
One more question on this.
How do I change this code to allow for NO upload. How do I say "if blank, ignore"?
<?php
$name  = trim(JRequest::getString('firstname', '', 'post')); // strip off any spaces from the ends
$name .= "_".trim(JRequest::getString('lastname', '', 'post'));
$name  = strtolower($name); // switch to all lower case
$name  = rawurlencode($name); // encode any special characters??
if ( $debug) echo "name: ".print_r($name, true)."<br /><br />";
$file_name_old = $attachments['datafile'];
if ( $debug) echo "file_name_old: ".print_r($file_name_old, true)."<br /><br />";
$file_name_new = dirname($file_name_old);
$file_name_new .= DS.$name;
if ( $debug) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
rename($file_name_old, $file_name_new);
?>
GreyHead 14 Jan, 2009
Hi Dianaj59,

<?php
$name  = trim(JRequest::getString('firstname', '', 'post')); // strip off any spaces from the ends
$name .= "_".trim(JRequest::getString('lastname', '', 'post'));
if ( $name != "_" ) {
    $name  = strtolower($name); // switch to all lower case
    $name  = rawurlencode($name); // encode any special characters??
    if ( $debug) echo "name: ".print_r($name, true)."<br /><br />";
    $file_name_old = $attachments['datafile'];
    if ( $debug) echo "file_name_old: ".print_r($file_name_old, true)."<br /><br />";
    $file_name_new = dirname($file_name_old);
    $file_name_new .= DS.$name;
    if ( $debug) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
    rename($file_name_old, $file_name_new);
}
?>

Bob
dianaj59 14 Jan, 2009
Not quite....the name fields can be filled in, but the upload of the datafile may not take place.

So, if datafile not uploaded, then do nothing;
else run the php code and attach the first and last name to the uploaded picture.

The names will always be there, they are required, but uploading the picture is not required.
Max_admin 14 Jan, 2009
Hi Diana, I'm not sure I can understand what you need here, if some file is not uploaded then Chronoforms will not take any action by default, do you want to change this ?

Cheers
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
dianaj59 14 Jan, 2009
oh, ??
When I enter my name criteria and submit with an uploaded file all goes well.
When I enter my name criteria and submit without uploading a file I get:
Warning: rename(,\dianaj_jones) [function.rename]: No such file or directory in C:\xampp\htdocs\gcc\components\com_chronocontact\chronocontact.php(371) : eval()'d code on line 13

any thoughts?
Max_admin 14 Jan, 2009
Hi, I'm not sure but may be try to disable uploads because you are doing the uploads manually, this needs some testing!

Regards
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
GreyHead 14 Jan, 2009
Hi Dianaj59,

OK, lets try this
<?php
if ( is_file($attachments['datafile']) ) {
    $name  = trim(JRequest::getString('firstname', '', 'post')); // strip off any spaces from the ends
    $name .= "_".trim(JRequest::getString('lastname', '', 'post'));
    $name  = strtolower($name); // switch to all lower case
    $name  = rawurlencode($name); // encode any special characters??
    if ( $debug) echo "name: ".print_r($name, true)."<br /><br />";
    $file_name_old = $attachments['datafile'];
    if ( $debug) echo "file_name_old: ".print_r($file_name_old, true)."<br /><br />";
    $file_name_new = dirname($file_name_old);
    $file_name_new .= DS.$name;
    if ( $debug) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
    rename($file_name_old, $file_name_new);
}
 ?>
dianaj59 14 Jan, 2009
YES! That did it. It works great whether I upload or not.

I still have a problem with this hosting site I picked. I might see if I can get a refund. They aren't very helpful....and I don't know why this would work on localhost, but not on their site....

If you have that'd be great, if not I'll try to find a live person at SiteGround to talk to...
thanks again.
dianaj59 14 Jan, 2009
CLOSE this issue.
Your code worked beautiful and when I tried this out on the hosting site it worked great there too!
Thank you so much for all your help!
Becca800 22 Jan, 2009
I got this to work! Awesome! I did not see the second page before, beautiful!

I have one question though: In my form they can upload jpg, gif or png. When I use this script, the file changes to a name with no extension. Although you can see the image on my browser, I was wondering if this might cause problems on other browsers where the image has no file extension. Or, will it cause harm with the almighty Google ranking system, to have incomplete files?

Is there a way to keep the file extension?
GreyHead 22 Jan, 2009
Hi becca800,

Hmmm, I missed that . . . try this version
    <?php
    if ( is_file($attachments['datafile']) ) {
        $name  = trim(JRequest::getString('firstname', '', 'post')); // strip off any spaces from the ends
        $name .= "_".trim(JRequest::getString('lastname', '', 'post'));
        $name  = strtolower($name); // switch to all lower case
        $name  = rawurlencode($name); // encode any special characters??
        if ( $debug ) echo "name: ".print_r($name, true)."<br /><br />";
        $file_path_old = pathinfo($attachments['datafile']);
        if ( $debug ) echo "file_name_old: ".print_r($file_path_old, true)."<br /><br />";
        $file_name_new = $file_path_old['dirname'];
        $file_name_new .= DS.$name.".".$file_path_old['extension'];
        if ( $debug ) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
        rename($attachments['datafile'], $file_name_new);
    }
    ?>

Bob

Later: corrected bug in old file name
Becca800 22 Jan, 2009
You are so genius. Okay, the problem with that (although I have not tried it) is that when my html page is created, the name of the file to call is correct, but just the first part, because I still do not know what extension the user upload. Do you know what I mean?

Update: I tried it and got a bunch of errors...file didn't exist, could not rename.
GreyHead 22 Jan, 2009
Hi Becca800,

No, you lost me there . . . can you give me an example.

Update: I found one bug and fixed it.

Bob
Becca800 22 Jan, 2009
When my form posts (using content submit), I am able to show the image by using the new name I just gave it from the form fields the user filled out. But that person could have uploaded a jpg, gif or png and so I am unable to automatically have the html just know which etension to add to the new file name. But I have a solution, I just need your help....I can just rename all uploads to .jpg How can I do that?
GreyHead 22 Jan, 2009
Hi becca800,

The code I've just posted add the correct file extension?? At least I hope it does! So I don't understand the problem - and you can't just rename gifs as jpgs - they won't open.

Bob
Becca800 22 Jan, 2009
I guess it is hard to explain, I haven't tried the new code yet because I have to run in a sec. But I will try again, and you are awesome for continuig this dialogue.

The script above renames the file and adds the correct extension. My form uploads all data into an article and creates a new article complete with html and the image in a specific area. I am able to automatically have the image name within the html as it pull the info from the form just as the renaming script does. But...my article does not know if it will be jpg, gif, or png. That is my problem. I have to still manually enter that part.

I just did a test where I upload a test.gif file and then renamed it to test.jpg and tried to pull it up on my browser and it worked.
Becca800 22 Jan, 2009
Okay I am back. No, the new code didn't work. The file was never saved this time in the upload folder and I got this error:
Warning: rename(,/home/mytown5/public_html/components/com_chronocontact/upload/jshdfj_jsdhkjh.jpg) [function.rename]: No such file or directory in /home/mytown5/public_html/components/com_chronocontact/chronocontact.php(421) : eval()'d code on line 13

I reread my post above and I wouldn't have understood that either. :-) My submit content form generates the html in the article and I am able to create the path to the image. And with this new script, I can create the path to the image and the image name up to the extension. I have no way of being able to write into the form which extension it will be. So when the script changes the file name, and my article has been created by the form, I will have to go in and add the extension manually. The only way I can see to circumvent this problem is have the script rename the file with a .jpg extension. I would at least like to try it.

Would it be something like:

    <?php
    if ( is_file($attachments['file_13']) ) {
        $name  = trim(JRequest::getString('text_1', '', 'post')); // strip off any spaces from the ends
        $name .= "_".trim(JRequest::getString('text_2', '', 'post'));
        $name  = strtolower($name); // switch to all lower case
        $name  = rawurlencode($name); // encode any special characters??
        if ( $debug ) echo "name: ".print_r($name, true)."<br /><br />";
        $file_path_old = pathinfo($attachments['file_13']);
        if ( $debug ) echo "file_name_old: ".print_r($file_path_old, true)."<br /><br />";
        $file_name_new = $file_path_old['dirname'];
        $file_name_new .= DS.$name.".".jpg['extension'];
        if ( $debug ) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
        rename($attachments['datafile'], $file_name_new);
    }
    ?>
Max_admin 22 Jan, 2009
Hi,

I tried to find what's wrong here but its a hard task when its too late and you are very tired but I see this in the path:
components/com_chronocontact/upload/

if this V3.0 then the path will have uploads/formname/filename

Warm regards
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
Becca800 23 Jan, 2009
I got it! Thanks for all your help. This worked for me:

<?php
if ( is_file($attachments['file_13']) ) {
    $name  = trim(JRequest::getString('text_1', '', 'post')); // strip off any spaces from the ends
    $name .= "_".trim(JRequest::getString('text_2', '', 'post'));
    $name  = rawurlencode($name); // encode any special characters??
    if ( $debug) echo "name: ".print_r($name, true)."<br /><br />";
    $file_name_old = $attachments['file_13'];
    if ( $debug) echo "file_name_old: ".print_r($file_name_old, true)."<br /><br />";
    $file_name_new = dirname($file_name_old);
    $file_name_new .= DS.$name.'.'.jpg;
    if ( $debug) echo "file_name_new: ".print_r($file_name_new, true)."<br /><br />";
    rename($file_name_old, $file_name_new);
}
?>


Changing the images all to jpg in the third to last line, I also removed the change to lowercase which I needed to fit into my entire application. Works swell. I can go to sleep. You guys are awesome.
GreyHead 23 Jan, 2009
Hi Becca800,

Great. I'm delighted that it's working for you. MY feeling is that you might want to amend the file upload permissions so that they only allow .jpg files. Otherwise you might have ome problems down the road.

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