Voor een tweetalige website gebruik in op de pagina waar een nieuws item kan worden togevoegd het volgende formuliers:

    <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>


De bedoeling van het formulier is dat er eerst een record aan de database tafel (news) word toegevoegd met de Nederlandse waarden (language_abbr_nl,news_title_nl,news_content_nl), gevolgd door een foto die in de tafel news_photos terecht moet komen (news_id, photo) en die geupload moet worden naar een bepaalde folder. Welke weer gevolgd word door de tweede insert in de tafel news, dit keer met de Engelse waarden (language_abbr_en,news_title_en,news_content_en)

Note: door de opbouw van de database heb ik voor beide records slechts 1 foto nodig.

In mijn controller heb ik de volgende actie:


	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_el = filter_input(INPUT_POST, 'language_abbr_el', FILTER_SANITIZE_STRING);
		$language_abbr_en = filter_input(INPUT_POST, 'language_abbr_en', FILTER_SANITIZE_STRING);		
		$news_title_el    = filter_input(INPUT_POST, 'news_title_el', FILTER_SANITIZE_STRING);
		$news_title_en    = filter_input(INPUT_POST, 'news_title_en', FILTER_SANITIZE_STRING);
		$news_content_el  = filter_input(INPUT_POST, 'news_content_el', 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_el,$news_title_el,$news_content_el);
		
		if(!empty($_FILES['file']))
		
		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);
		}
		
	}	


Die volgens mij goed zou moeten zijn. De taal rows gaan inderdaad de database in, maar de insert in de news_photos tafel en de upload werken niet. Ik blijf Undefined Index: file errors krijgen

Wat doe ik hier fout? Alle hulp is van harte welkeom
Hi Ozzie en Frank. Ik had indrdaad mijn bed opgezocht. Ik keek scheel van moeheid. :)

Bedank beide voor de input. Ik gebruik inderdaad een (soort) Framework. Het is eigenlijk meer een combinatie van verschillende frameworks.

@Ozzie Ik heb gedaan wat je aanrade, de var_dump in de index geplaatst, juist boven de bootstrap en hij geeft de array leeg aan. Wat zijn mijn opties?

Alvast bedankt
Maak een tijdelijk standalone PHP script waarin je je upload functionaliteit test op dezelfde manier.

Krijg je wel resultaat voor $_FILES --> het ligt aan je framework en je moet $_FILES anders benaderen
Krijg je geen resultaat voor $_FILES --> ergens in je PHP/webserver instellingen zorgt iets ervoor dat file uploads geblokkeerd worden of mislukken (en mogelijk loop je hierna alsnog tegen je framework aan)

Op deze manier kun je een aantal zaken uitsluiten en heb je na afloop een goede indicatie waar je het probleem kunt zoeken (en hopelijk oplossen).
Hoi Thomas. Bedankt voor je reactie. Weet echt niet zo goed wat ik moet doen? Ik heb meer websites met de zelde opzet. Het enige verschil is dat ik in deze website (omdat het tweetalig is) Alto Router gebruik
Even van Stackoverflow overgenomen:

Although very rare, but from the PHP Manual page comment :

If the $_FILES array suddenly goes mysteriously empty, even though your form seems correct, you should check the disk space available for your temporary folder partition. In my installation, all file uploads failed without warning. After much gnashing of teeth, I tried freeing up additional space, after which file uploads suddenly worked again.
And here's the check-list for file uploading in PHP:

Check php.ini for file_uploads = On, post_max_size, and upload_max_filesize. Make sure you’re editing the correct php.ini – use phpinfo() to verify your settings. Make sure you don’t misspell the directives as 8MB instead of the expected 8M!
Do not use javascript to disable your form file input field on form submission!
Make sure you do not have two input file fields with tha same name attirbute
Make sure your directory has read+write permissions set for the tmp and upload directories.
Make sure your file destination and tmp/upload directories do not have spaces in them.
Make sure all FORMs on your page have /FORM close tags.
Make sure your FORM tag has the enctype="multipart/form-data" attribute. No other tag will work, it has to be your FORM tag. Double check that it is spelled correctly. Double check that multipart/form-data is surrounded by STRAIGHT QUOTES, not smart quotes pasted in from Word OR from a website blog (WordPress converts straight quotes to angle quotes!). If you have multiple forms on the page, make sure they both have this attribute. Type them in manually, or try straight single quotes typed in manually.
Make sure your FORM tag has method="POST". GET requests do not support multipart/form-data uploads.
Make sure your file input tag has a NAME attribute. An ID attribute is NOT sufficient! ID attributes are for use in the DOM, not for POST payloads.
Also make sure that the file you are uploading does not have any non-alpha numeric characters in it.
Once, I just spent hours trying to figure out why this was happening to me all of a sudden. It turned out that I had modified some of the PHP settings in .htaccess, and one of them (not sure which yet) was causing the upload to fail and $_FILES to be empty.
do not leave an "_" (underscore) into the name attribute of the input tag
Finally, try uploading very small files.



Toevoeging op 15/06/2015 20:13:07:

bron: http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php
Donald Boers op 15/06/2015 18:24:54
Hoi Thomas. Bedankt voor je reactie. Weet echt niet zo goed wat ik moet doen? Ik heb meer websites met de zelde opzet. Het enige verschil is dat ik in deze website (omdat het tweetalig is) Alto Router gebruik

Uhm, je gebruikt een MVC-framework, maar weet niet hoe file uploads werken? Curieus.

Probeer het zo eens, sla het volgende script op als test.php of whatever en probeer iets te uploaden:
<?php
header('Content-Type: text/html; charset=UTF-8');

function escape($in) {
    return htmlspecialchars($in, ENT_QUOTES, 'UTF-8');
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    ?><h2>$_POST</h2>
    <pre><?php echo print_r(array_map('escape', $_POST), true) ?></pre>
    <h2>$_FILES</h2>
    <pre><?php echo print_r($_FILES, true) ?></pre><?php
    exit;
}

?>
<!DOCTYPE html>
<html>
<head>
<title>yolo swag file upload</title>
<style type="text/css">
label { width: 150px; display: block; float: left; }
</style>
</head>

<body>
<form action="<?php echo escape($_SERVER['PHP_SELF']) ?>" method="post" accept-charset="UTF-8" enctype="multipart/form-data">
<p><label for="name">type iets</label><input type="text" name="name" id="name" /></p>
<p><label for="file">selecteer iets</label><input type="file" name="file" id="file" /></p>
<p><button type="submit">verzenden</button>
</form>
</body>
</html>

Reageren