By jeepfreak on
I'm trying to add upload functionality to a module I'm creating. I just want to upload a single file and write it's path to my database without installing any additional modules. I can get the form item to show up, but I can't get it to process the upload to save my life! I'm just writing it like a page until I can get it figured out, then I'll implement it into my module.
I've been searching for hours and hours and hours for a solution. PLEASE HELP!!
$form = '';
$form .= form_file(t('Upload a file.'), 'upload', 40, t("Upload a file."));
$form .= form_submit(t('Upload'));
$output = '';
$output .= form($form, $method = 'post', $action = null, $attributes = null);
print theme('page', $output);
$op = $_POST['op'];
$edit = $_POST['edit'];
if ($op == t('Upload')) {
//What in the world goes here?!?!
}
Comments
maybe somthing like this
maybe somthing like this, although it doesn't do anything with mysql
No dice.
Thank you for the response. Unfortunately, it did not work. Nothing was uploaded. I've been trying to use Drupal functions like file_save_upload, but nothing has worked!
BTW - I can handle the mySQL stuff, I just need to get the file uploaded.
Here's what I use to add
Here's what I use to add files to custom node types:
http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/yogadex/modul...
Unfortuanely for you, that is written for drupal 4.7 and your code appears to be earlier.
Your problem may have to do the forms encoding type. If your code is part of a hook_form(&$node, &$param), try this:
From your example I can't tell if you're using hook_form. If not, you'll have to figure out an attribute or something to set the encoding type.
Well, right now, I'm not
Well, right now, I'm not working with the form hook, I'm just inserting the code like a page within drupal until I can figure it out. Then I will put it into the form hook.
I changed the following line and now it acts a little more like it's uploading (larger files yield a longer pause, etc), but they still don't save anywhere.
In 4.6, look to the image module
In 4.6, look to the image.module as an example of how to handle files.
They do not simply save themselves. Drupal provides some methods (file_save_upload, file_check_upload) to help, but its not ideal. By the time you get your custom node handling previewing and submitting, it's a rat's nest (IMHO).
The upload module in 4.7 is OK, but not suitable for custom node types which know exactly what files they need. I think many developers are aware of the shortcomings of the file handling. Someday one of us will submit something better. Could be you... ;)
I believe berkes is playing
I believe berkes is playing with some concepts on that front.
http://cvs.drupal.org/viewcvs/drupal/contributions/modules/betterupload/
Various long discussions in the development archives on various appraoches.
-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain
-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide
Thanks for pointing me
Thanks for pointing me towards image.module. I spent a lot more time looking at it's code, but it's so much more complex than what I'm trying to do it's hard for me to follow.
I just don't get what the
I just don't get what the problem is. The following code results in:
* file_check_dir = SUCCESS
* file_check_upload = FAILURE
I also tried changing
$file = $_FILES['picture'];to$file = $edit['picture'];try
try
you can find the filename of the uploaded file in
$file->filenameor the filesize in$file->filesizeDamn...
* file_check_dir = SUCCESS
* file_check_upload = FAILURE
=(
This works for me
Assumed that 'module_upload_page' is your menu callback, then this works for me:
It works!!
It works!! Thank you! Thank you! Thank you! Thank you! Thank you!!
Do you have a complete listing of the working module
Hello,
I am trying to do this too, but im not that great at programming, and im getting very confused about where that bit of code goes in the module.
Do you have a complete listing of the working module code, I tried adding it to the page module but I think I may have put the code in the wrong place.
Would be great if you could post it up.
Many thanks
Nigel
Custom Module Upload
This would be an excellent addition to Custom Module Development documentation.
Apache is bandwidth limited, PHP is CPU limited, and MySQL is memory limited.
Example module with one image upload field
I've put together a module with an image upload field as an example.
Hope this helps.
Do I need to create a new table as well?
Thanks very much Tenrapid for that.
Do I need to create a new database for that and if so could you show what fields e,t,c
Thank you very much, this is really helpful!!!
Nigel
database table query
uups, here is the missing piece
You are the man! This helps
You are the man! This helps me out sooo much and I'm sure it will help a lot of other people too!! Thank you!
I just have one question...
why not use
instead of 2 queries
Again, Thank you!!
v4.7 version of nodewithupload?
Any chance that some kind person could provide a v4.7 version of this nodewithupload module? I have tried to convert it myself, but I'm getting very bogged down in the new Forms API. :-(
For what it's worth, here is my attempt at a v4.7 nodewithupload.install file:
And here is my hook_node_info() that replaces the v4.6 hook_node_name():