1

I have to create a CMS for a webstore for a webscript unit i'm taking. I have to upload a photo of the product but i keep getting and error when i try to upload. I search it a lot and tried a lot of things, i changed the folder to read & write, i did the 'chmod -R 777...', nothing. I'm starting to think the problem is with my code. I appreciate all help, thanks!

I always this error: Warning: File upload error - unable to create a temporary file in Unknown on line 0

<?php

    $dbhost = '127.0.0.1';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'ezcart';

    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

    if ($conn->connect_error) {

        die("Could not connect to the database: " . $conn->connect_error);

    }

    $name = $_FILES['photo']['name'];
    $size = $_FILES['photo']['size'];
    $type = $_FILES['photo']['type'];
    $tmp_name = $_FILES['photo']['tmp_name'];

    if (isset($name)) {

        if(($type == 'image/jpeg' || $type == 'image/jpg' || $type = 'image/png') && ($size <= 3145728)) {

            $path = 'prod_photos/';

            if (move_uploaded_file($tmp_name, $path.$name)) {

                $sql = "INSERT INTO product (prodCat, prodDes, prodNam, prodPho, prodPri, prodSto, prodSup) VALUES ('$_POST[prodCat]', '$_POST[prodDes]', '$_POST[prodNam]', '$name', '$_POST[prodPri]', '$_POST[prodSto]', '$_POST[prodSup]')";

                if ($conn->query($sql) === TRUE) {

                    echo '<span>Product added sucessfully.</span>';

                }

            }

        }

        else {

            echo '<span>Please choose a valid photo.</span>';

        }

    }

    $conn->close();

?>
8
  • What about your form, does it contain a POST method and a valid enctype? Commented Apr 11, 2015 at 16:13
  • Yes, they are allright. Commented Apr 11, 2015 at 16:16
  • Check to see what your upload max settings are and that your tmp folder isn't full. Plus, Google your error message. Too many reasons why it could fail. Commented Apr 11, 2015 at 16:19
  • The upload max setting is ok, i googled it and i cant really find something useful, i tried the main awnsers to it. Commented Apr 11, 2015 at 16:21
  • Are you on Windows or UNIX? See also chown php.net/manual/en/function.chown.php Commented Apr 11, 2015 at 16:22

1 Answer 1

1

You should check your php.ini and look for the 'upload_tmp_dir' option.

After that, check the permission of your tmp dir, and try to chmod it again.

If you want to know what upload_tmp_dir your server is using, you can simply use this:

die(ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir());
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.