The file loader does not work and the files do not move to the specified folder.

I am creating an element form in which the administrator can add elements to the stock along with his images. I want to save the images as "$ item_id.jpg" in the folder "img / items". Unfortunately, my objects are added to the database, but the file does not load into the specified folder. Below my code will help you.

//Code for the form----------------------------------------------
<form class="stock" method="post" action="">
<table class="newstocktable">
    <tr>
        <th>Size:</th>
        <td> 
            <select name= "size">
            <option> </option>
            <?php
                require ('connection.php');
                $query = mysql_query("SELECT * FROM fyp_size")or die(mysql_error());
                while($result = mysql_fetch_array($query)){
                    echo '<option>'.$result['size'].'</option>';
                }
            ?>
            </select>
        </td>
    </tr>
    <tr>
        <th>Item name:</th>
        <td> 
            <input type="text" name="name" id="Name"  <?php if (isset($_POST['name'])=== true){echo 'value="', strip_tags($_POST['name']),'"';}?>>
        </td>
    </tr>
    <tr>
        <th>Description: </th>
        <td> 
            <textarea name="desc" <?php if (isset($_POST['desc'])=== true){echo 'value="', strip_tags($_POST['desc']),'"';}?>></textarea>
        </td>
    </tr>
    <tr>
        <th>Price (£): </th>
        <td> 
            <input type="text" name="price" <?php if (isset($_POST['price'])=== true){echo 'value="', strip_tags($_POST['price']),'"';}?>>
        </td>
    </tr>
    <tr>
        <th>Quantity: </th>
        <td> 
            <input type="text" name="quantity" <?php if (isset($_POST['quantity'])=== true){echo 'value="', strip_tags($_POST['quantity']),'"';}?>>
        </td>
    </tr>
    <tr>
        <th>Threshold: </th>
        <td> 
            <input type="text" name="threshold" <?php if (isset($_POST['threshold'])=== true){echo 'value="', strip_tags($_POST['threshold']),'"';}?>>
        </td>
    </tr>
    <tr>
        <th>Product image:</th>
        <td>
            <input type="file" name="item_img" id="item_img">
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" name="addnewstock" value= "Add Stock">
        </td>
    </tr>
</table>

//PHP parsing code-------------------------------------
$query = mysql_query("SELECT * FROM items WHERE name='$name' && 
size_id='$sizeid'")or die(mysql_error());
$numrows = mysql_num_rows($query);
if($numrows == 0){
    mysql_query("INSERT INTO items VALUES(
    '', '$sizeid', '$name', '$desc', '$price', '$quantity', '$threshold', now()
    )")or die(mysql_error());
    $item_id = mysql_insert_id();
    $query = mysql_query("SELECT * FROM items WHERE name='$name'")or die(mysql_error());
    $numrows = mysql_num_rows($query);
    if ($numrows == 1){
        $errors[] = 'Item was added.';
        $filename = "$item_id.jpg";
        move_uploaded_file($_FILES['item_img']['temp_name'], "img/items/$filename");
        mysql_close();
+3
source share
3 answers

Try assigning the form to the enctype attribute with a value of multipart / form-data.

+1
source

There is no problem in your form enctype="multipart/form-data">

<form class="stock" method="post" action="" enctype="multipart/form-data">

This is required if you want to add any <input type="file">and process the downloaded file in an array$_FILE

+1
source

, "move_uploaded_file" - , . :

"img/items/$filename"

, . ...

"img/items/".$filename 

$filename, . 3316397 :)

0
source

All Articles