regarding helper function, am i doing this the right way? |
[eluser]ixxalnxxi[/eluser]
I created a method imageUpload() as a helper function. It takes two parameters, file destination and an address to a variable $image. It then tests if there is an image to upload, with if($_FILES['file_field']['tmp_name']). If a file is present, it attempts to upload and returns true while assigning $image to the file name. This method only returns false when there was an error uploading the file. Am I going about this design wise the right way? I normally don't pass things by reference but it seemed like a valid way for the method to determine whether everything went smoothly and get the image name.
[eluser]Teks[/eluser]
Does your function do something that the built-in File Uploading Class can't?
[eluser]ixxalnxxi[/eluser]
Teks, There's actually several things I need done regarding images upload that, as far as I know, aren't provided by the File Uploading Class, a. check if a file is present, if so b. upload the file c. assuming upload was successful, grab filename and upload it to DB, otherwise don't update DB at all and warn user. I am currently using the File Upload Class' methods within the imageUpload() method, but was wondering if there's a more efficient way to approach this.
[eluser]Teks[/eluser]
@ixxalnxxi: I could be wrong - I have not yet played much with the File Uploading Class - but reading through the documentation's tutorial, it seems that it should do everything you need. According to the tutorial, your procedure should be something like this: 1) present form to user 2) get information from user, and try to upload the file(s) - using $this->upload->do_upload() If the upload is unsuccessful (because the file is not present, or the target directory does not have the right permissions, or it does not comply with the restrictions you specified), the function should return FALSE, and in that case, you can retrieve the error with $this->upload->display_errors() 3) if there was an error, display it back to the user with $this->upload->display_errors() if there was no error, get any data you need about the file (such as filename, etc.) with $this->upload->data(), and save it to database. Does this not work as specified in the documentation?
[eluser]ixxalnxxi[/eluser]
The file field in my form is not mandatory, therefore $this->upload->do_upload() would not work because an empty file field is okay in this situation and $this->upload->do_upload() would return false, as if something went wrong. In this case, false is needed IFF: upload fails AND there is an actual file attempting to be uploaded,
[eluser]nikefido[/eluser]
I'd extend the file upload class and add inject that functionality in rather than create my own. You might want to consider that, but I do see what you are saying about that $this->upload->do_upload() "problem" (have you tested that?) That's not too much of a problem, it can be taken care of with a fairly simple if statement (although I haven't done this myself)
[eluser]ixxalnxxi[/eluser]
Quote:I’d extend the file upload class and add inject that functionality in rather than create my own. That's actually what I am doing now. I'll look into the improved if statement to see if that can help, thanks.
[eluser]nikefido[/eluser]
cool - Post your new extended class if you run into issues. |
Welcome Guest, Not a member yet? Register Sign In |