Log in

View Full Version : Error with image file upload


Mythotical
01-24-2008, 04:06 AM
I have added the file image upload code while I wait on a response regarding file image upload to database.

Anyway, here is the code I'm using thus far:
if($_REQUEST['do'] == 'saved')
{
$upload = new vB_Upload_Image($vbulletin);
$upload->image =& vB_Image::fetch_library($vbulletin);
$upload->path = './'.$options['sponsor_imgpath'];
// if (!($upload->process_upload($vbulletin->GPC['img_file']))){
// eval(standard_error(fetch_error('there_were_errors _encountered_with_your_upload_x', $upload->fetch_error())));
// }

$sql = "INSERT INTO `" . TABLE_PREFIX . "sponsors`
(`title` , `url` , `image` , `description`, `inactive`, `img_file`)
VALUES (
'" . $db->escape_string($vbulletin->GPC['title']) . "',
'" . $db->escape_string($vbulletin->GPC['url']) . "',
'" . $db->escape_string($vbulletin->GPC['image']) . "',
'" . $db->escape_string($vbulletin->GPC['description']) . "',
'" . $db->escape_string($vbulletin->GPC['inactive']) . "',
'" . $upload->process_upload($vbulletin->GPC['img_file'] . "'
)");

$vbulletin->db->query_write($sql);
print_cp_message('Sponsor Added.', 'sponsors.php', 5);
}


EDIT: Fixed previous error, new problem.

When I click submit, I get the following error:
Database error in vBulletin 3.6.8:

Invalid SQL:
INSERT INTO `sponsors`
(`title` , `url` , `image` , `description`, `inactive`, `img_file`)
VALUES (
'Download Now',
'www.download.com',
'',
'blah blah blah again',
'0',
';

MySQL Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 9
Error Number : 1064

Thanks in advance
Steve :)

calorie
01-24-2008, 05:39 AM
Missing quote, see below:

INSERT INTO `sponsors`
(`title` , `url` , `image` , `description`, `inactive`, `img_file`)
VALUES (
'Download Now',
'www.download.com',
'',
'blah blah blah again',
'0',
>>>>>> ';

Mythotical
01-24-2008, 02:19 PM
I don't see how I'm missing a quote. Very stange.

--------------- Added 1201192811 at 1201192811 ---------------

Found the problem, had to switch this line:
'" . $upload->process_upload($vbulletin->GPC['img_file'] . "'

To this:
'" . $upload->process_upload($vbulletin->GPC['img_file']) . "'

--------------- Added 1201192853 at 1201192853 ---------------

Still there is a problem, when it goes to upload, seems it doesn't even try to upload or anything.

Lynne
01-24-2008, 02:47 PM
You may want to double-check your code for the upload path. I don't think you want to say:
$upload->path = './'.$options['sponsor_imgpath'];

I think you need to add something in there:
$upload->path = './'.$vbulletin->options['sponsor_imgpath'];

?? Maybe?

I had to play with upload code for the torrents on my site. I found it very frustrating because the way I did it, it would not spit out errors to me on the screen. I keep thinking I should rewrite it, but I haven't gotten around to it.

Mythotical
01-24-2008, 07:01 PM
Lynne, that is incorrect, I have my own settings, I am not using vBulletin's built in settings stuff. I did my own settings table, settings page, etc so its easier for upgrading and uninstalling.

Thats not the problem, not sure what is but I'm gonna have to do the file upload stand alone and go from there.

Mythotical
01-29-2008, 04:28 AM
Almost 5 days have passed and not another response. Can anyone help me get this upload stuff to work?

Dismounted
01-29-2008, 05:11 AM
print_r($vbulletin->GPC['img_file']);
Make sure it actually has data in it.

Mythotical
01-29-2008, 05:36 AM
LOL, I should have thought to do that, thats what happens when you have alot on your mind and too tired to think.

--------------- Added 1201592290 at 1201592290 ---------------

Dismounted: This is what print_r outputs: Array ( )

--------------- Added 1201592548 at 1201592548 ---------------

BTW, here is the code for my admin page that has the browse file field.

// Add Sponsor
if ($_REQUEST['do'] == 'add')
{
print_form_header("sponsors", "saved", "false", "true", "add_entry", "90%", "", "true", "post");
print_table_header("Add Sponsor");
print_input_row("Title", "title", $vbulletin->GPC['title'], $htmlise = true, $size = 35, $maxlength = 100, $direction = '', $inputclass = false);
print_input_row("URL", "url", $vbulletin->GPC['url'], $htmlise = true, $size = 35, $maxlength = 100, $direction = '', $inputclass = false);
print_input_row("Image", "image", $vbulletin->GPC['image'], $htmlise = true, $size = 35, $maxlength = 100, $direction = '', $inputclass = false);
print_input_row("Description", "description", $vbulletin->GPC['description'], $htmlise = true, $size = 35, $maxlength = 100, $direction = '', $inputclass = false);
print_radio_row('Suspended', 'inactive', array(0 => 'No', 1 => 'Yes'), $vbulletin->GPC['inactive']);
echo '<tr><td class="alt2">Image File</td><td class="alt2"><input type="file" name="img_file" /></td></tr>';
print_submit_row("Add Sponsor");
print_table_footer();
}

if($_REQUEST['do'] == 'saved')
{
$upload = new vB_Upload_Image($vbulletin);
$upload->image =& vB_Image::fetch_library($vbulletin);
$upload->path = './'.$options['sponsor_imgpath'];
// if (!($upload->process_upload($vbulletin->GPC['img_file']))){
// eval(standard_error(fetch_error('there_were_errors _encountered_with_your_upload_x', $upload->fetch_error())));
// }

$sql = "INSERT INTO `" . TABLE_PREFIX . "sponsors`
(`title` , `url` , `image` , `description`, `inactive`, `img_file`)
VALUES (
'" . $db->escape_string($vbulletin->GPC['title']) . "',
'" . $db->escape_string($vbulletin->GPC['url']) . "',
'" . $db->escape_string($vbulletin->GPC['image']) . "',
'" . $db->escape_string($vbulletin->GPC['description']) . "',
'" . $db->escape_string($vbulletin->GPC['inactive']) . "',
'" . $vbulletin->GPC['img_file'] . "'
)";
print_r($vbulletin->GPC['img_file']);
$vbulletin->db->query_write($sql);
print_cp_message('Sponsor Added.', 'sponsors.php', 5);
}

Dismounted
01-29-2008, 08:18 AM
If it's empty, it means that there's (obviously) no data to insert in the first place. Have you actually cleaned it with the cleaner? Because if you haven't, there's no way it'll magically appear in the GPC array.

Mythotical
01-29-2008, 01:50 PM
Dismounted: Here is my GPC array.
$vbulletin->input->clean_array_gpc('r', array(
'sid' => TYPE_INT,
'title' => TYPE_STR,
'url' => TYPE_STR,
'image' => TYPE_STR,
'description' => TYPE_STR,
'sclicks' => TYPE_STR,
'inactive' => TYPE_STR,
'img_type' => TYPE_STR,
'img_file' => TYPE_FILE
));

As you can see I have img_file in there. So to answer your question, yes I have cleaned it.

calorie
01-29-2008, 02:07 PM
$vbulletin->input->clean_array_gpc('r', array(
'sid' => TYPE_INT,
'title' => TYPE_STR,
'url' => TYPE_STR,
'image' => TYPE_STR,
'description' => TYPE_STR,
'sclicks' => TYPE_STR,
'inactive' => TYPE_STR,
'img_type' => TYPE_STR
));

$vbulletin->input->clean_array_gpc('f', array(
'img_file' => TYPE_FILE
));

Mythotical
01-29-2008, 04:02 PM
Thanks alot calorie, that fixed my problem with the computer to web folder issue, I will work on the database storing after my trip.

mokujin
09-23-2008, 08:44 PM
Hi there,
I have some similar code like Steve M.
But I need when the file is uploaded then auto-renamed to SQL ID just Inserted.
Can someone help me?
Thanks

Dismounted
09-24-2008, 05:06 AM
After you insert the data, you can fetch the ID used by using the function insert_id().