Forum Moderators: coopster

Message Too Old, No Replies

uploading images

         

dave1236

3:32 am on Oct 29, 2009 (gmt 0)

10+ Year Member



All:

I am trying to understand the nuances of providing users the ability to upload images to my site.

My goal is to allow a user to upload a title, summary, and detail information for a particular promotion. If a user has a prepared image/file, I would like to add this to the form.

I have found the following script that provides for uploading an image...
<snip>

But this appears to simply upload an image.

Is there a mechanism to allow a user to complete a form which would include a file upload? In short, the text fields would also provide for addition of an upload.

Thanks in advance for your thoughts...

David

[edited by: dreamcatcher at 7:08 pm (utc) on Oct. 29, 2009]
[edit reason] No Urls Please. See TOS. [/edit]

mvaz

1:42 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



You must use the input type 'file' which will give the users the option to browse and select files.

dave1236

3:46 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



Thanks.

Let me clarify my question -

I have a form that includes:
- 4 text input fields.
- 1 image upload button.

In looking at sample scripts for uploads, there seems to always be a separate script that validates the image upload.

What I want to end up accomplishing is having the image uploaded, and then inserting the 4 text input fields and location of the upload to my database.

Can this be accomplished with one script and 'submit' button?

mvaz

3:49 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



Yes, you will have one form where the user inputs data and selects the image, and another form where you will have your scripts that will validate the <i>all</i> data before the image is stored on your file server and a link or location is stored on your database alongside the other data.

rocknbil

5:31 pm on Oct 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, two forms are not required. It can all be in one form.

Here is a general outline of the logic:


if ($_POST['some-input-indicating-submit']) {
process_data();
}
else { output_your_form(); }


function process_data() {
$errors = check_input_data();
if ($errors) { output_form_again_with_values_intact(); }
// the above should EXIT, no need for else
list ($errors,$image_filename) = upload_images();
if ($errors) { output_form_again_with_values_intact(); }
// ditto on exit, note that you want to check the upload
// BEFORE adding to database so you don't
// have dead records in your DB
// also will likely use $image_filename in your insert
update_database($image_filename);
output_success_response();
}

You would write all of those "functions." For example, you would put code from the standalone upload script into the function "upload_images()" and return an error value and the successful image upload file name - of course, it will be one or the other, one will be blank or null. Then what you would normally do in your database insert goes into "update_database()." You should also have a function somewhere that outputs the form, so it can be used for both "output_your_form()" and "output_form_again_with_values_intact()".

dave1236

3:11 am on Oct 30, 2009 (gmt 0)

10+ Year Member



Thanks for this feedback.

I have been able to successfully input my data into my mysql database.

Now my problem is displaying the image that I have uploaded. I used a 'longblob' format for the image that is uploaded to the database.

It does not appear that I have actually saved the file to a directory, but instead it resides in my db.

i have named the db field 'imgfile'. However, when I try to display the image, I get nothing other than the 'x box' which.

Any thoughts?

Here is the display code...

<img src = <?php echo "$imgfile" ?>/>

dave1236

8:58 pm on Oct 30, 2009 (gmt 0)

10+ Year Member



I have been able to upload the files to my server. The files show in the directory as I want the to. However, for some reason I cannot open these files, as I get a 403 Forbidden error?

Is it possible that the actual files are not being uploaded?

I am thoroughly confused now.

rocknbil

7:19 pm on Oct 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, I would go with a file system upload rather than storing the image in the database. I'ts just easier to manage (for me.)

Forbidden means a) you are uploading the files in a location not accessible from the web, or b) the place where you are storing them does not have read by all permissions, or c) the files themselves don't have read by all permissions.