0

I use this script for uploading an image file. It should allow to upload jpeg, jpg and png files.

Everything works on remote server, but I can't upload it locally, on XAMPP server. I tried all tips mentioned in this thread Why would $_FILES be empty when uploading files to PHP? but no one worked.

Thanks for help

Form:

<form class="form-horizontal" action="potwierdz.php" method="POST" enctype="multipart/form-data">
    <fieldset>

        <!-- Form Name -->
        <legend>Dodawanie produktu</legend>

        <!-- Text input-->
        <div class="form-group">
          <label class="col-md-4 control-label" for="produkt_nazwa">Nazwa</label>  
          <div class="col-md-4">
              <input id="produkt_nazwa" name="produkt_nazwa" class="form-control input-md" required="" type="text">
              <span class="help-block">Podaj nazwę produktu</span>  
          </div>
        </div>

        <!-- Text input-->
        <div class="form-group">
          <label class="col-md-4 control-label" for="producent_nazwa">Nazwa producenta</label>  
          <div class="col-md-4">
              <input id="producent_nazwa" name="producent_nazwa" class="form-control input-md" required="" type="text">
              <span class="help-block">Podaj nazwę producenta produktu</span>  
          </div>
        </div>

        <!-- Textarea -->
        <div class="form-group">
          <label class="col-md-4 control-label" for="producent_adres">Adres producenta</label>
          <div class="col-md-4">                     
            <textarea class="form-control" id="producent_adres" name="producent_adres" required=""></textarea>
          </div>
        </div>

        <!-- Select Basic -->
        <div class="form-group">
          <label class="col-md-4 control-label" for="produkt_kategoria_nazwa">Kategoria</label>
          <div class="col-md-4">
            <select id="produkt_kategoria_nazwa" name="produkt_kategoria_nazwa" class="form-control">
                <option> </option>
            </select>
          </div>
        </div>

        <!-- File Button --> 
        <div class="form-group">
          <label class="col-md-4 control-label" for="obraz">Zdjęcie</label>
          <div class="col-md-4">
            <input id="obraz" name="obraz" class="input-file" type="file">
          </div>
        </div>

        <!-- Button -->
        <div class="form-group">
          <label class="col-md-4 control-label" for="wyslij"></label>
          <div class="col-md-4">
            <button id="wyslij" name="wyslij" class="btn btn-primary">Dodaj</button>
          </div>
        </div>

    </fieldset>
</form>

here is script:

<?php

    $errors= array();
      $file_name = $_FILES['obraz']['name'];
      $file_size =$_FILES['obraz']['size'];
      $file_tmp =$_FILES['obraz']['tmp_name'];
      $file_type=$_FILES['obraz']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['obraz']['name'])));

      $expensions= array("jpeg","jpg","png");

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"obrazki/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }

?>

It returns me "extension not allowed, please choose a JPEG or PNG file."

It's also printing Notice: Undefined index: obraz whenever I use $_FILES['obraz']['...'] variable.

Community
  • 1
  • 1
Webowiec
  • 15
  • 4
  • 3
    `echo $file_ext;` and also `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver Apr 19 '16 at 19:50
  • Have you checked the value of `$file_ext` and is error reporting on in case any other warnings/errors are being thrown? – Crackertastic Apr 19 '16 at 19:51
  • I would do validation when you're assigning your `$file_name`, `$file_type`, etc. variables, using something like `if (!isset($_FILES['obraz']['name'])) { throw new \Exception('Missing file name)"; }` – David Wyly Apr 19 '16 at 19:54
  • What version of PHP are you using? – RiggsFolly Apr 19 '16 at 20:03
  • `strtolower(end(explode('.',$_FILES['obraz']['name'])))` generates an error `Strict standards: Only variables should be passed by reference` its the `end()` that is causing the error. In PHP5.6 & PHP5.5 & PHP 5.4 and a Notice in PHP7.0.5 – RiggsFolly Apr 19 '16 at 20:04
  • echo $file_ext; prints nothing at all. Errors are reported from the beggining :) – Webowiec Apr 19 '16 at 21:13

2 Answers2

0

I would do validation when you're assigning your $file_name, $file_type, etc. variables.

Validation example:

if (!isset($_FILES['obraz']['name'])) { 
  throw new \Exception('Missing file name)"; 
}

Additionally, I would use array_pop() instead of end. This just simply gives you the last element in the array after you explode it.

That, and I would trim any potential whitespace off your $file_ext variable, as whitespace could be a potential problem (and may even be causing your issue):

$file_pieces = explode('.',$_FILES['obraz']['name']);
$file_ext = trim(array_pop($file_pieces));

Finally, during your validation, I would create a more descriptive error; one that echoes $file_ext:

if (in_array($file_ext,$expensions) === false){
    $errors[] = "extension '$file_ext' not allowed, please choose a JPEG or PNG file.";
}

If you're still having issues, do a var_dump($_FILES) or print_r($_FILES) to ensure you're pulling the file name down correctly. If that doesn't work, check to ensure that your environment is both configured and working properly.

David Wyly
  • 1,671
  • 1
  • 11
  • 19
  • Hi. Thanks for your reply. It seems to throw the exception you created. I had checked `var_dump($_FILES)` before I asked a question and it always returned me an empty array. – Webowiec Apr 20 '16 at 09:29
  • @Webowiec I copied your code and ran it in my environment, and it appears to work for me; I am getting the correct information when I `var_dump($_FILES)`. It is starting to sound like this is an environment-specific issue (instead of something programming related). – David Wyly Apr 20 '16 at 16:28
  • Yes, it was :D after restarting system it finally works fine – Webowiec Apr 21 '16 at 12:40
  • @Webowiec That is awesome! I should always remember to include "have you turned it off and back on again?" as part of my responses. Feel free to upvote and/or accept this answer if it helped you, or write your own answer if you had a particular way of solving it. – David Wyly Apr 21 '16 at 15:17
0

This line generates an error in PHP5.6 & PHP5.5 & PHP 5.4 and a Notice in PHP7.0.5

Strict standards: Only variables should be passed by reference

$file_ext=strtolower(end(explode('.',$_FILES['obraz']['name'])));

Replace it with

$t = explode('.',$_FILES['obraz']['name']);
$file_ext = strtolower($t[1]);

Or if you are using PHP7 then you can use

$file_ext = strtolower(explode('.',$_FILES['obraz']['name'])[1]);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • `$t[1]` offset error. `$_FILES['obraz']['name']` is missing – Webowiec Apr 19 '16 at 21:21
  • Now suddenly you can see error message! Well in that case you need to first check that the user has actually posted a file. And also check `$_FILES['obraz']['error']` – RiggsFolly Apr 19 '16 at 22:06