Undefined index file?

Hi have the following form:

    <form action="/admin/add_news_item" method="post" name="news-form" id="news-form" enctype="multipart/form-data">
      <h2><?php echo $heading; ?></h2>
      <p>Use the form below to add a new news item. To add a news item fill out a title and content in Dutch followed by a title and content in English , and choose a photo. When you're don click the <strong>Add news item</strong> button.</p>
      <input name="language_abbr_nl" id="language_abbr_nl" type="hidden" value="nl">
      <fieldset>
        <label>News title (Dutch)&nbsp;<span>*</span></label>
        <input name="news_title_nl" id="news_title_nl" type="text" class="textfield" tabindex="1" required>     
      </fieldset>
      
      <fieldset >
        <label>News content (Dutch)</label>
        <textarea name="news_content_nl" id="news_content_nl" class="textarea h100" tabindex="2"></textarea> 
      </fieldset>
      
      <input name="language_abbr_en" id="language_abbr_en" type="hidden" value="en">
      <fieldset>
        <label>News title (English)&nbsp;<span>*</span></label>
        <input name="news_title_en" id="news_title_en" type="text" class="textfield" tabindex="3" required>     
      </fieldset>
      
      <fieldset >
        <label>News content (English)</label>
        <textarea name="news_content_en" id="news_content_en" class="textarea h100" tabindex="4"></textarea> 
      </fieldset>
      
      <fieldset>
        <label>News photo&nbsp;&nbsp;<span>(<a href="#" title="The best dimensions for a photo are 1000 x 400 px." class="masterTooltip">?</a>)</span></label>
        <input name="file" id="file" type="file" tabindex="5" required>
      </fieldset>
      <fieldset class="top-margin"> 
        <button type="submit" class="form_button" tabindex="6">Add news item</button>
      </fieldset>
    </form>

I furthermore have the following action in the controller:

    public function add_news_itemAction()
    {
        $photo_path   = APP_PATH.'/../httpdocs/images/news_photos/';        
        include_once   (APP_PATH.'/helpers/zebra_image.php');
        
        $validextensions        = array("jpeg", "jpg", "png");
        $original_name          = $_FILES['file']['name'];
        $ext                    = explode('.', basename($original_name));
        $file_extension         = strtolower(end($ext));
        $new_name               = md5(uniqid()) . "." . $file_extension; 
        $new_photo_path         = $photo_path . $new_name;
        
        $language_abbr_nl = filter_input(INPUT_POST, 'language_abbr_nl', FILTER_SANITIZE_STRING);
        $language_abbr_en = filter_input(INPUT_POST, 'language_abbr_en', FILTER_SANITIZE_STRING);        
        $news_title_nl    = filter_input(INPUT_POST, 'news_title_nl', FILTER_SANITIZE_STRING);
        $news_title_en    = filter_input(INPUT_POST, 'news_title_en', FILTER_SANITIZE_STRING);
        $news_content_nl  = filter_input(INPUT_POST, 'news_content_nl', FILTER_UNSAFE_RAW);
        $news_content_en  = filter_input(INPUT_POST, 'news_content_en', FILTER_UNSAFE_RAW);
        
        $news_id          = $this->page->add_news_item($language_abbr_nl,$news_title_nl,$news_content_nl);
        
        if ( is_uploaded_file($_FILES['file']['tmp_name']) )
        {
            $this->create_image($_FILES['file']['tmp_name'], $new_photo_path , 1000, 400, ZEBRA_IMAGE_CROP_CENTER);
            $this->page->add_news_photo($news_id,$new_name);
            $insert = $this->page->add_news_item($language_abbr_en,$news_title_en,$news_content_en);
        }
        
    }

As you can see from the form is this about inserting The content for one language first, followed by the upload in insert of a photo holding the news_id from the just inserted news_item, followed by the insert of the second language (this one doesn’t need a photo attached to it.

When I submit the form in inserts for the 2 languages are going well but I get the following errors for file

Undefined index: file in C:\wamp\www…on line 138

Undefined index: file in C:\wamp\www…on line 153

Does anyone see what i’m doing wrong?

Thank you in advance

I just added a var_dump at the very top of the action:

        var_dump($_FILES);
        exit;

and I get an empty array. file_uploads is on. I took out all other form elements, but keep getting an empty array. What can be the reason?

Seems like there is a lot of useful information relevant to your situation in this stack overflow thread.

http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php

Hi Oddz. Thanks for the reply. I saw that article as well but none of the points discussed has changed the situation, I keep getting an empty array.

And what does the code around lines 138 and 153 in whatever file(s) they’re in look like?

Hi Mittineague. Thanks for the reply. Its all as in the action described in my first post. Or do you mean something different?

I looked at the code you posted, yet didn’t see anything that would obviously be line 138 or line 153 - the example code snippets are not in anyway near that long.

Which line(s) is what?

Thanks again:

Line 138 $original_name = $_FILES[‘file’][‘name’];

Line 153 if ( is_uploaded_file($_FILES[‘file’][‘tmp_name’]) )

Try inserting the following line before line 138

echo ‘<pre>’; print_r( $_FILES ); echo ‘</pre>’; die;

Hi John. Thanks for the reply. I tried that as well and it gives me an empty array.

@donboe
Whoops, I was using a tablet and overlooked your “var_dump(_$_FILES)” post #2

I copied and pasted your script, added the first couple of error checking lines, remmed out missing files, etc and managed this

<?php
error_reporting(-1); ini_set('display_errors',1);

Array
(
    [file] => Array
        (
            [name] => chicken-chill-out.jpeg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpOCYo4C
            [error] => 0
            [size] => 45440
        )
)

Are you running this on a local development environment or remote server?

Hi Oddz. Locally. Sorry maybe I should have mention this :frowning:

Does the file size exceed the max file size for uploads?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.