0

I was just trying to create a simple image uploading script.

This is what I used, but it seems there is some problem and shows an error

<?php
    define("FILEREPOSITORY", "./");
    if(isset($_POST['submit'])){
        $user =$_POST['user'];
        $ext_boo = FALSE;
        $size_boo = FALSE;
        $user_boo = FALSE;
        if(strlen($user)<=0){
            echo "No Username";
            $user_boo = FALSE;
        }
        if($_FILES['picture']['size'] <=1024000){
            $size_boo = TRUE;
        }
        else{
            echo "Too large";
            $size_boo = FALSE;
        }
        if(is_uploaded_file($_FILES['picture']['tmp_name'])){
            //mime type
            switch($_FILES['picture']['type']){
                case "image/jpeg":
                    $extension = ".jpeg";
                    $ext_boo = TRUE;
                    break;
                case "image/gif":
                    $extension = ".gif";
                    $ext_boo = TRUE;
                    break;
                case "image/png":
                    $extension = ".png";
                    $ext_boo = TRUE;
                    break;
            }
            if($ext_boo && $size_boo && $user_boo){
                $result = move_uploaded_file($_FILES['picture']['tmp_name'], FILEREPOSITORY."/images/".$user."".$extension."");
                if($result)
                    echo "Uploaded";
                else
                    echo "Some problems";
            }
        echo "Wrong file type";
        }
    }
    else{
        echo "<table>
        <form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
        <tr>
        <td>User:</td>
        <td><input type=\"test\" name=\"user\" /></td>
        </tr>
        <tr>
        <td>File:</td>
        <td><input type=\"file\" name=\"picture\" /></td>
        </tr>
        <input type=\"submit\" name=\"submit\" value=\"upload\" />
        </form>
        </table>";
    }
?>

Can anybody help me to find out the problem?

Error after trying var_dump($_FILES);:

array (size=1)
  'picture' => 
    array (size=5)
      'name' => string 'sample.png' (length=10)
      'type' => string 'image/png' (length=9)
      'tmp_name' => string 'C:\wamp\tmp\php74BB.tmp' (length=23)
      'error' => int 0
      'size' => int 7575
Wrong file type
8
  • 1
    What is the error that you get? Commented Mar 11, 2014 at 21:50
  • $user_boo never gets true Commented Mar 11, 2014 at 21:51
  • @BOMEz Updated the question with the Error statement Commented Mar 11, 2014 at 21:52
  • Do a var_dump($_FILES) and see what it says after you test for $_POST['submit']. It seems $_FILES has no index named picture for some reason. Commented Mar 11, 2014 at 21:57
  • @BOMEz : It shows array (size=0) empty Commented Mar 11, 2014 at 21:59

1 Answer 1

1

You never set $user_boo to TRUE anywhere. I switched it to start off as TRUE (assume it is true until you later check to see if it's false on line 10).

Also on line 37 I changed the file path. You had an extra / in your upload location. Code below worked on my machine. Make sure you have the www user set to owner on whatever location you are trying to upload to and that it has permissions to write.

<?php
    define("FILEREPOSITORY", "./");
    if(isset($_POST['submit'])){
        $user =$_POST['user'];
        $ext_boo = FALSE;
        $size_boo = FALSE;
        $user_boo = TRUE;
        if(strlen($user)<=0){
            echo "No Username";
            $user_boo = FALSE;
        }
        if($_FILES['picture']['size'] <=1024000){
            $size_boo = TRUE;
        }
        else{
            echo "Too large";
            $size_boo = FALSE;
        }
        if(is_uploaded_file($_FILES['picture']['tmp_name'])){
            //mime type
            switch($_FILES['picture']['type']){
                case "image/jpeg":
                    $extension = ".jpeg";
                    $ext_boo = TRUE;
                    break;
                case "image/gif":
                    $extension = ".gif";
                    $ext_boo = TRUE;
                    break;
                case "image/png":
                    $extension = ".png";
                    $ext_boo = TRUE;
                    break;
            }
            var_dump($ext_boo,$size_boo,$user_boo);
            if($ext_boo && $size_boo && $user_boo){
                $result = move_uploaded_file($_FILES['picture']['tmp_name'], FILEREPOSITORY."images/".$user."".$extension."");
                if($result)
                    echo "Uploaded";
                else
                    echo "Some problems";
            }
        echo "Wrong file type";
        }
    }
    else{
        echo "<table>
        <form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
        <tr>
        <td>User:</td>
        <td><input type=\"test\" name=\"user\" /></td>
        </tr>
        <tr>
        <td>File:</td>
        <td><input type=\"file\" name=\"picture\" /></td>
        </tr>
        <input type=\"submit\" name=\"submit\" value=\"upload\" />
        </form>
        </table>";
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.