【问题标题】:File uploader is not working and no files are moved to the designated folder.文件上传器不工作,没有文件被移动到指定文件夹。
【发布时间】:2014-03-25 03:29:36
【问题描述】:

我正在创建一个项目表单,管理员可以在其中将项目连同他们的图像一起添加到库存中。我想在“img/items”文件夹中将图像保存为“$item_id.jpg”。不幸的是,我的项目被添加到数据库中,但文件没有上传到指定的文件夹。以下是我的代码,您的帮助将不胜感激。

//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();

【问题讨论】:

    标签: php file-upload directory


    【解决方案1】:

    问题在于我的“move_uploaded_file”代码是准确的存储位置。在我的代码是:

    "img/items/$filename"
    

    它没有分配文件名,因此文件没有上传。但是这解决了它...

    "img/items/".$filename 
    

    关键是附加 $filename 变量,而不是将其作为字符串包含在内。感谢 user3316397 指出:)

    【讨论】:

      【解决方案2】:

      问题在于您的表单缺少enctype=”multipart/form-data”&gt;

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

      如果要添加任何&lt;input type="file"&gt; 并在$_FILE 数组中处理上传的文件,这是必需的

      【讨论】:

      • 我试过了,但它仍然没有上传图像:(。一切都很好。我没有收到错误、警告或通知,所以我不知道出了什么问题。下面是表单标题:
      【解决方案3】:

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

      【讨论】:

      • 感谢您的帮助,即使将表单标题更改为:
      • 在您的 php 解析代码中,将 $filename 更改为 $filename = $item_id 。 “.jpg”。您将其设置为 "$item_id.jpg" 这是错误的,因为您不能将变量附加到可用引号内的字符串。您必须使用 . 连接它
      • 感谢您的建议,但我仍然无法正常工作:(这是我的代码... $filename = $item_id.".jpg"; move_uploaded_file($_FILES['item_img' ]['temp_name'], "img/items/$filename"); mysql_close();
      • 尝试:move_uploaded_file($_FILES['item_img']['temp_name'], "img/items/" . $filename);
      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2016-01-25
      • 2018-07-01
      • 1970-01-01
      相关资源
      最近更新 更多