Forum Moderators: coopster
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]
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?
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()".
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" ?>/>
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.