I have found a really good event php code that enables a user to add an event to a calender as shown below, the only problem is I want the user to be able to upload a picture of the event as well. I managed to modify it so the user can enter a url to a picture to display this way but I thought this was pretty lame so I'm thinking of a way to involve a image upload within the exsisting form to get round this.

As my php knowledege is very basic I have tried merging a upload php code into the original form but no luck as yet, can any of you guys get me on the right track?

Below is the code and $event_flyer is what I wish to change to an image upload.

the first example shown on this page http://us2.php.net/manual/en/features.file-upload.php is pretty good I have managed to include this in the event add form the only issues I'm confussed on are how I include the file path for the uploaded image as since the form requires the image to be uploaded before I would be able to extract a file name from the uploaded image and i would need that to place into a hidden field so the url reference would be included on submission to the database... I could use another type of upload php script where it prints out the uploaded image file path after upload but I could get this value to be included in a hidden field so all could then be added to the database.. but the only problem with this is that I require information from an original query string, the upload event form is brought about by clicking on a date on another .php page with a calender that then pops up a window with the date clicked on to pass this within the query string to the form and hence the database, so i would loose this info if done this way, my working demo is here http://dj-promo.co.uk/calender.php with the included upload added field but as you can see the upload file part is not working as yet..

Below what I could get together with the first example on php.net

<?php
/***
this is a simple and complete function and
the easyest way i have found to allow you
to add an image to a form that the user can
verify before submiting

if the user do not want this image and change
his mind he can reupload a new image and we
will delete the last

i have added the debug if !move_uploaded_file
so you can verify the result with your
directory and you can use this function to
destroy the last upload without uploading
again if you want too, just add a value...
***/

function upload_back() { global $globals;

/***
1rst set the images dir and declare a files
array we will have to loop the images
directory to write a new name for our picture
***/

/***
if we are on a form who allow to reedit the
posted vars we can save the image previously
uploaded if the previous upload was successfull.
so declare that value into a global var, we
will rewrite that value in a hidden input later
to post it again if we do not need to rewrite
the image after the new upload and just... save
the value...
***/

/***
now verify if the file exists, just verify
if the 1rst array is not empty. else you
can do what you want, that form allows to
use a multipart form, for exemple for a
topic on a forum, and then to post an
attachement with all our other values
***/

/***
now verify the mime, i did not find
something more easy than verify the
'image/' ty^pe. if wrong tell it!
***/

if(!eregi('image/', $_FILES['attachement']['type'])) {

echo 'The uploaded file is not an image please upload a valide file!';

} else {

/***
else we must loop our upload folder to find
the last entry the count will tell us and will
be used to declare the new name of the new
image. we do not want to rewrite a previously
uploaded image
***/

if ($event_txt != ""){
echo "<p><strong>Today's Events:</strong></p>
$event_txt
<hr/>";
}
/***
now the form if you need it (with the global...):

just add the hidden input when you write your
preview script and... in the original form but!
if you have send a value to advert your script
than we are remaking the form. for exemple with a
hidden input with "reedit" as value or with a
$_GET method who can verify that condition
***/

You should be able to use a single form for everything, though IIRC all the form elements will come through in the $_FILE array rather than the $_POST array.

DJ Hands3

03-26-2009, 01:31 AM

I'v been trying and it dosent make any sense to me, so what your saying above is when the form is submitted the picture uploads and can also at the same time tell the database where its stored on the server whilst submitting what ever else is on the form? I'v tried I just cant understand how the form can tell the database where the image is stored before its even uploaded to the server or is it a case of somehow telling the text input the name of the directory and file name into a vairable from the php script?

code so far:

<?php
/***
this is a simple and complete function and
the easyest way i have found to allow you
to add an image to a form that the user can
verify before submiting

if the user do not want this image and change
his mind he can reupload a new image and we
will delete the last

i have added the debug if !move_uploaded_file
so you can verify the result with your
directory and you can use this function to
destroy the last upload without uploading
again if you want too, just add a value...
***/

function upload_back() { global $globals;

/***
1rst set the images dir and declare a files
array we will have to loop the images
directory to write a new name for our picture
***/

/***
if we are on a form who allow to reedit the
posted vars we can save the image previously
uploaded if the previous upload was successfull.
so declare that value into a global var, we
will rewrite that value in a hidden input later
to post it again if we do not need to rewrite
the image after the new upload and just... save
the value...
***/

/***
now verify if the file exists, just verify
if the 1rst array is not empty. else you
can do what you want, that form allows to
use a multipart form, for exemple for a
topic on a forum, and then to post an
attachement with all our other values
***/

/***
now verify the mime, i did not find
something more easy than verify the
'image/' ty^pe. if wrong tell it!
***/

if(!eregi('image/', $_FILES['attachement']['type'])) {

echo 'The uploaded file is not an image please upload a valide file!';

} else {

/***
else we must loop our upload folder to find
the last entry the count will tell us and will
be used to declare the new name of the new
image. we do not want to rewrite a previously
uploaded image
***/

if ($event_txt != ""){
echo "<p><strong>Today's Events:</strong></p>
$event_txt
<hr/>";
}
/***
now the form if you need it (with the global...):

just add the hidden input when you write your
preview script and... in the original form but!
if you have send a value to advert your script
than we are remaking the form. for exemple with a
hidden input with "reedit" as value or with a
$_GET method who can verify that condition
***/

Have your upload_back() function return the information you need to insert into your db. I'm assuming (haven't read all your code) that you are determining the storage location based on the filename. Since this info is available inside upload_back(), simply build your filename, etc inside the function and return it. Something like:

function upload_back()
{
// function code

$file_location = ''; // insert filename, path, etc.

return $file_location;
}

Usage example:

$file_storage_location = upload_back();
// $file_storage_location can now be used in your query to insert the location into your db

The above i tried but had no luck just chucks out the file name on every day on the calender - i will include img tags to get the picture to dispaly once the original issue is sorted the original code for the calender php is below