0

I have a file type input and i want to upload an image. i re-wrote the code like 5times thinking i did a mistake but so far i can't see it. Because it is not working. html code

<form method="post" enctype="multipart/form-data">
<input type="file" name="primaryimg" id="primaryimg"></br>
<input type="submit" value="Sell!" name="uploaditem" />
</form>

And this is my php code

if(isset($_POST['uploaditem'])){
    $filebasename = basename($_FILES['primaryimg']['name']);
    $ext = strtolower(substr($filebasename, strrpos($filebasename, '.') + 1));
    if (($ext == "jpg" || $ext == "gif" || $ext == "png" || $ext == "jpeg") && ($_FILES['primaryimg']['type'] == "image/jpg" || $_FILES['primaryimg']['type'] == "image/gif" || $_FILES['primaryimg']['type'] == "image/png" || $_FILES['primaryimg']['type'] == "image/jpeg")){
        $desired_dir="img/products/sellers/$user/$itemname/";
        $file_name=$_files['primaryimg']['name'];
        if (file_exists($desired_dir . $file_name)){
            $errormsg="Filename already exists.";
        }else{
            move_uploaded_file($_FILES['primaryimg']['tmp_name'], $desired_dir . $file_name);
            $errormsg = $file_name . "successfully uploaded.";
        }
    }else{
        $errormsg = "Error! File is not a valid jpg, gif, png or jpeg";
    }
}

So i tried echo'ing the $filebasename where the basename($_FILES) is located to see what value its getting. Apparently its empty. I did a $lol=$_POST['primaryimg']; and echo'd $lol and it did show the item name. so for some reason the $files stays empty and the move_upload_file does not work either.

This is what i see the code itself The Code echo/print

ones putting the error report on i see the following error

Notice: Undefined index: primaryimg in D:\Dropbox\UoC\phphosting\buyathing\sellanitem.php on line 21

After the error shown below that the image is not a jpg/jpeg...it should print the filebasename and as u can see it is EMPTY.

i went to php.ini to see if file_upload was on and it is. the image i tried uploading was 550k but my max filesize was 2M so that isnt the problem either. I have been googling for 5hours but no luck. So if someone out there can help me before i lose my mind would be great :') Thanks in advance.

radicarl
  • 327
  • 2
  • 9
  • 2
    Turn on error reporting at the top of your php - `ini_set('display_errors', 1); error_reporting(-1);` and update your answer with any errors. Might be worth seeing if `print_r($_FILES);` actually returns anything too. – Darren Jan 12 '17 at 02:36
  • How can you possibly maintain code without any indentation? Or, how can you expect us to voluntarily try and wade through code without any indentation? – miken32 Jan 12 '17 at 04:39
  • @Darren according to the error report i am getting the following Notice: Undefined index: primaryimg in D:\Dropbox\UoC\phphosting\buyathing\sellanitem.php on line 21 the $_FILES is not fetching the image name – Jurick Pastechi Genaro Jan 12 '17 at 13:11
  • @JurickPastechiGenaro Hey mate, can you just try `print_r($_POST)` and let me know if that's coming through? Ps, what is line `21`? Would it be this line -> `$file_name=$_files['primaryimg']['name'];` ? Because superglobals like `$_FILES` **are** case-sensitive and you need to type them uppercase. – Darren Jan 13 '17 at 00:50
  • @Darren yes i did change $_files to $_FILES as i also saw it later on. And as i mentioned the $_POST is blank. One thing i did notice is that, since i have a post form inside a post forum(if you see my code) for somereason if i remove the form where the file inputs are and put them appart. the $_FILES do get value but the $_POST's from my textbox that i also needs are now empty .-. Right now i am just re-creating a new structure and see if i can make it work lol – Jurick Pastechi Genaro Jan 13 '17 at 02:59
  • Why do you have a form inside a form? – Darren Jan 13 '17 at 03:00
  • @JurickPastechiGenaro Might be best if you read [**this**](http://stackoverflow.com/a/3430226/2518525) – Darren Jan 13 '17 at 03:08
  • 1
    Yep i figured lol. I decided to split the forms and give each its own button. Put the first button hidden and javascript the second button to execute the first button before continuing the codes from the second button...complicated but it works .-. – Jurick Pastechi Genaro Jan 13 '17 at 04:38

1 Answers1

0

I think You didn't create a directory, when you move a file, there should be a folder exists.first You should create a directory to this path img/products/sellers/".$user."/".$itemname."/.

use php basic function mkdir() .

Here You can also find some more info. Then try to upload the image in specific loaction.I hope this would help otherwise comment, I will look into it.