文件没有上传到php文件夹中

时间:2015-04-26 12:37:04

标签: php post file-upload

我需要在文件夹中上传文件,但文件永远不会进入文件夹。正确添加文件的名称,但文件夹中没有文件。出了什么问题?

Php错误:未定义的索引:第12-16行的照片

$name= $_FILES["foto"]["name"];
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
$temp= $_FILES["foto"]["temp_name"];
$error= $_FILES["foto"]["error"];

if ($error > 0)

   die("Error uploading file! code $error.");

   else
   {
       if($type=="image/png" || $size > 2000000)//condition for the file
   {

       die("Format  not allowed or file size too big!");
   }

   else   
   {
    move_uploaded_file($temp,"assets/");
    echo "Upload complete!"; 
    }
}

HTML:

<form id="form" style="margin-left: 200px"  action="addNewProduct.php"      method="post"><br>
            <br>
            <br>
            <div id="imageUpload">
                <label for="foto">Foto</label>
                <input type="file" name="foto" /><br>
            </div>
            <div id="infoForm">
                <label for="productNaam">Productnaam</label>
                <input type="text" name="productNaam"/><br>
                <br>
                <label for="beschrijving">Productbeschrijving</label>
                <input type="text" name="productBeschrijving" /><br>
                <br>
                <label for="btw">BTW</label>
                <input type="number" name="productBtw" /><br>
                <br>
                <label for="prijsinclbtw">PrijsInclBTW</label>
                <input type="number" name="productPrijsInclBtw" /><br>
                <br>
                <br>
                <br>

                <input type="submit" name="submit" value="Add new Pen">
            </div>
        </form>

2 个答案:

答案 0 :(得分:1)

您的$_FILES["foto"]["temp_name"];不正确,您应将其更改为$_FILES["foto"]["tmp_name"];

您的文件夹assets应存在于根文件夹中。

你的表格应该是这样的:

<form action="uploads.php" method="post" enctype="multipart/form-data">
    <input type="file" name="foto">
    <input type="submit" name="submit">
</form>

请尝试此操作,希望这有助于您:uploads.php

$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
// $temp= $_FILES["foto"]["temp_name"];

$file_name = $_FILES["foto"]["name"];      
$source = $_FILES['foto']['tmp_name'];
$dir = "./assets/";
$file = $dir . $file_name;
//$directory = "./assets/upload/$file_name";
if(!file_exists ($file ))
{
    move_uploaded_file($source,$file );
    exit();
}
else
{
    echo "File exist";
}

答案 1 :(得分:0)

上传文件时,form必须具有enctype属性。

<form enctype="multipart/form-data" id="form" style="margin-left: 200px" action="addNewProduct.php" method="post">
      ^^