Skip to content
Advertisement

PHP image upload – several errors

So, I’m trying to upload an image using move_uploaded_file(). Here is the code:

HTML FORM:

        <form method="post" action="?page=prod&action=register" enctype="multipart/form-data">
            <fieldset class="field-prods">
                <label>Name:</label> <br>
                <input type="text" name="txtName">
            </fieldset>

            <fieldset class="field-prods">
                <label>Price:</label> <br>
                <input type="number" name="nmbPrice">
            </fieldset>

            <fieldset class="field-prods">
                <label>Image:</label> <br>
                <input type="file" name="fileImage">
            </fieldset>

            <button type="submit" class="btn-prods">Register</button>
        </form>

PHP SCRIPT:

if ($_POST != null) {
            if (!empty($_POST['txtName']) && !empty($_FILES['fileImage']) && !empty($_POST['nmbPrice'])) {
                Product::insert($_POST['txtName'], $_FILES['fileImage'], $_POST['nmbPrice']);

                $target = "img/prods/" . $_FILES['fileImage']['name'];
                $fileTmpName = $_FILES['fileImage']['tmp_name'];
                move_uploaded_file($fileTmpName, $target);
    }
}

But I’m getting lot of warnings:

*Warning: Array to string conversion*
*Warning: move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: No such file or directory*
*Warning: move_uploaded_file(): Unable to move "F:ProgramsXampptmpphp7CE5.tmp" to "img/prods/livraria-print1.jpg"*

Can someone please help me with that?

Advertisement

Answer

# Array to string conversion

This warning is in insert function: you passed array ($_FILES['fileImage']) instead of string.

It seems that you want to store address of image, if that so it is better to store the image first then try to save the address of image.

# move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: …

This warning is clear. check your directories and sure that the path you used is correct.

# move_uploaded_file(): Unable to move

And this one is because of prev warning.

10 People found this is helpful
Advertisement