文件上传器无法工作,没有文件被移动到指定的文件夹。

3

我正在创建一个项目表单,管理员可以向库存中添加项目和它们的图像。我想将图像保存为“$item_id.jpg”在“img/items”文件夹中。不幸的是,我的项目被添加到数据库中,但文件并没有上传到指定的文件夹。以下是我的代码,非常感谢你的帮助。

//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个回答

1
问题出在您的表单中,您缺少enctype=”multipart/form-data”>
<form class="stock" method="post" action="" enctype=”multipart/form-data”>

如果您想添加任何<input type="file">并在$_FILE数组中处理上传的文件,则需要此内容。

我尝试了,但它仍然无法上传图片 :(. 一切都解析得很好。我没有收到错误、警告或通知,所以我不知道出了什么问题。以下是表单头:<form class="stock" method="post" enctype="multipart/form-data" action=""> - Rumesh

1
尝试给表单添加一个enctype属性,值为multipart/form-data。

谢谢您的帮助,即使将表单头更改为以下内容,它仍无法上传:<form class="stock" method="post" enctype="multipart/form-data" action=""> - Rumesh
在您的 PHP 解析代码中,将 $filename 更改为 $filename = $item_id . ".jpg"。您将其设置为“$item_id.jpg”,这是错误的,因为您无法在双引号内将变量附加到字符串中。您必须使用点进行连接。 - user3316397
谢谢您的建议,但我仍然无法让它工作 :( 这是我的代码... $filename = $item_id.".jpg"; move_uploaded_file($_FILES['item_img']['temp_name'], "img/items/$filename"); mysql_close(); - Rumesh
尝试使用以下代码:move_uploaded_file($_FILES['item_img']['temp_name'], "img/items/" . $filename); - user3316397

0
问题出在我的“move_uploaded_file”代码中,确切来说是存储位置的问题。之前我的代码是:
"img/items/$filename"

它没有分配字段名称,因此文件未上传。不过这样就解决了...

"img/items/".$filename 

关键是将$filename变量附加到字符串中,而不是将其包含为字符串。感谢user3316397指出这一点 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接