Professional CodeIgniter

(singke) #1

Chapter 10: Launch


304


Last Remaining Issues


You get a call from Claudia. She ’ s very excited because her team has successfully uploaded a great deal
of information onto the site. “ However, we have a little problem, ” she says.

It appears that whenever you create a new product in the system and don ’ t upload a file, the system
freezes. The same thing happens when you edit an existing product and don ’ t upload a new image for it.

“ One more thing, ” she says. “ I know it ’ s kind of a pain, but my mother got on the test site the other day,
and nothing worked for her. I talked to her about it, and I think she ’ s got a really old browser that
doesn ’ t support all the things we have on the site. ”

You assure Claudia that you will look into the file upload issue and that one of the last tasks you had
before launch was the installation of a JavaScript and cookie sniffer to ensure that everyone had a
browser that could handle the site ’ s AJAX.

Debugging File Uploads


The file upload issue is a simple one to fix. You have two model functions that do the heavy lifting of
adding/updating products in the database: addProduct() and updateProduct(). Both of these live in
the MProducts model.

Each of these functions contains a section in which you set the config parameters for the incoming file.
All you have to do is wrap those sections of the code that deal with file uploads with a simple test to see
if $_FILES has something in it, and then, right before each specific upload, check to see if there is a name
associated with each file. If there is, perform the upload. If not, move on. Here ’ s the code:

function updateProduct(){
$data = array(
‘name’ = > db_clean($_POST[‘name’]),
‘shortdesc’ = > db_clean($_POST[‘shortdesc’]),
‘longdesc’ = > db_clean($_POST[‘longdesc’],5000),
‘status’ = > db_clean($_POST[‘status’],8),
‘grouping’ = > db_clean($_POST[‘grouping’],16),
‘category_id’ = > id_clean($_POST[‘category_id’]),
‘featured’ = > db_clean($_POST[‘featured’],3),
‘price’ = > db_clean($_POST[‘price’],16)
);
if ($_FILES){
$config[‘upload_path’] = ‘./images/’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘max_size’] = ‘200’;
$config[‘remove_spaces’] = true;
$config[‘overwrite’] = false;
$config[‘max_width’] = ‘0’;
$config[‘max_height’] = ‘0’;
$this- > load- > library(‘upload’, $config);

if (strlen($_FILES[‘image’][‘image’])){
if(!$this- > upload- > do_upload(‘image’)){
Free download pdf