uploading and using images |
[eluser]Dan Tdr[/eluser]
Hey there, i was thinking if you guys could help me out with something.. I want to be able to upload a picture on my web server and put the file location in my database.(also renaming the uploaded images to something like thumb_1/2/3 would be something useful) basically i have a profile and i want to upload a photo to it. the database should contain something like : id, username, password, email, user_photo. i took the uploading files example from the code igniter user guide( file upload ) but i still don`t know how to put the picture in the database of the person that is editing his profile. oh and one more thing.. it would be very useful if you would know how to make it even more intresting: in the edit profile page we could have 2 fields for picture uploading.. one for avatar and another one for the profile pictures hope i expressed myself clear enough so that someone could help me.. thanks, Dan
[eluser]textnotspeech[/eluser]
As far as storing an image in a database, most people store the "image_name.ext" in the database and then generate a link from the query. Conversely you could store the "path/to/image/image_name.ext" but this could cause problems if you change the directory architecture. There is a way to store binary info in a database but I've never come across a reason to use this. As far as appending "_foo" to your image names I can give you my handy little append function: Code: // Takes two arguments, the image file name and the string you wish to append before the extension. ie. append("my_picture.jpg", "_thumb") As for how you associate that image with a user completely depends on your model, the short answer however is: "by user id". If you want to get fancy with filenames, you could use this function that will append or prepend strings to your filename: Code: // Takes three arguments, the image file name, the string you wish to append or prepend and the mode (default is append). Hope that helps!
[eluser]M4rc0[/eluser]
This is exactly what i want, maybe we can help each other here Dan. I've read the whole documentation and it's not clear how to save on database and display it later. Before saving to database or uploading the picture (photo or avatar, w/e), the filename should always be renamed, that should be standard on the library imo. So my first question was how to rename the picture before uploading, upload and then save the path/filename to database so i could display later. Looks like the first proccess is here, Thanks for sharing that code textnotspeech! I want to rename the whole filename, not just pend or append (append only for thumb). So i've done some change in your code, correct me if i'm wrong: Code: function rename_file($filename, $new_name) So either i want to generate a random hash or a custom name (such as id+date or something) i call that function and i have what i want. Now the question is, where do i put my new name on do_upload ? There isn't any parameter for filename. How did you do it textnotspeech ? Could it be like this maybe? Code: $myname = rename_file([i]what goes here?[/i],$this->post('name')); EDIT: Right, and what about the thumbnail new name? It should be $new_name.'_thumb'.$extension; If i add that on the function above (with a case for thumb), where will i add this new_filename for the thumb? Thanks
[eluser]textnotspeech[/eluser]
Well, this is a different situation than I thought. Personally, I don't use the upload helper. I would use the standard PHP move_uploaded_file function. If you do this, you can name the file anything you want as the "destination". You would use the destination variable in the SQL to save the path to the database. You can just including the full path/to/filename.ext in the upload_path of the config array if you want to use the CI library. Or, you could modify the $_FILES['userfile']['name'] to do what you want and then call the do_upload() function.
[eluser]M4rc0[/eluser]
Okay, so for uploading i have a few options to try. I think i'll try $_FILES together with do_upload() somehow. So i can rename, upload and save to database. My last (hopefully) question is about the image lib. I don't get how am i suppose to use resize() with a file image i've got from $_FILES. I don't like to make the user to use photoshop to resize his image and then upload. It should accept the image (valid extension and size) and the app do the rest (resize,rename,upload,save,..). The image lib sounds perfect for this BUT, it only shows how to resize the image after uploading? The documentation mentions: Code: $config['source_image'] = '/path/to/image/mypic.jpg'; What if i want to use the image lib with the image i have on the file field?
[eluser]M4rc0[/eluser]
textnotspeech, i believe the image library can do that! Quote:$config['new_image'] = '/path/to/new_image.jpg'; So i can concatenate like $config['new_image'] = '/path/'.$new_name.$ext; I'm really excited in trying this but i'm having trouble with the path.. I'm using /public/images for images, wich reside outside /system, so how can i fix this? After this i believe this topic is nailed, since the image library resize,rename and also take care of the upload! That leaves only the "save image path to db" wich is easy! What is the path in my case?
[eluser]Dan Tdr[/eluser]
hey M4rc0.. i have a question... could you help? in the upload library file we have something we may use.. but i don`t know hot to get the variable from there.. with the basic upload function found in the user guide from here : http://ellislab.com/codeigniter/user-gui...ading.html after you sucesfully uploaded a file you get the following page with some data we could use: Code: <html> so i found from were this info is outputed.. it`s from the Upload.php file in libraries Code: // -------------------------------------------------------------------- [code] <?php $upload_dir='uploads/' // your upload folder foreach ($user_profile as $field=>$profile){?> <img src="<?=base_url().$upload_dir.$profile?>" /> <?php } ?> i know this would work.. but i don`t know how to get if from the library.. and how to store it into the database.. could you guys help?
[eluser]M4rc0[/eluser]
Hi Dan, You can get all those upload parameters you mentioned with $this->upload->data()['parameter']; Check the array example in the end of "File Uploading Class" documentation with the data() function. What you want is [full_path] so: Code: $full_path = $this->upload->data()['full_path']; I see you are using a separate form to upload the picture, all you have to do is pass the path paramater to save on db: Code: $this->My_model->insert_picture($full_path); On your My_model you would have something like this: Code: (...) This is one example, just making things clear for you. Do as you want (there's probably a better way). I was happy with my solution above until i've found out that i STILL need to have the file already uploaded in the destination. I just wish i could use the image_lib to rename and upload like it does but with my file field.. It looks like i'll have to upload and only then rename&resize;, wich sounds redundant but possible. With raw php you can get the file, rename on the fly, resize and then upload. Is that how you did Dan? I see you are not renaming the images. But are you uploading and then resizing?
[eluser]textnotspeech[/eluser]
I couldn't remember if the image library would use the image name in the upload path parameter. Disregard using the $_FILES to rename. To go back a folder you would use "../". For example, if you have your site in a subfolder like www/site/, to get to the www folder you would use ../ to go up one directory to the www folder. You can use as many ../ as you need to reach the folder you need. My only experience is with *nix servers so if you're using IIS, I'm not sure if it's the same.
[eluser]M4rc0[/eluser]
textnotspeech, I've fixed my problem setting a base href on my template: <base href="http://localhost/project/" /> So on the path i can just put public/images I'm gonna use the redundant CI way. Upload the wrong thing and only then fix it. So i'm going to upload the image and then rename/resize with the image already there. It's uploading successfully now. With rename i gave up and i'm gonna use encrypt_name TRUE on the upload class. That solves things a bit since it's a random hash name. Now i'm trying the resizing. I'm still testing and i'll post my results. Dan, The code i posted above to get the data doesn't work. I'm doing this and it's working: $img_data = $this->upload->data(); now what you want is in $img_data['full_path']; The rest is the same. |
Welcome Guest, Not a member yet? Register Sign In |