我无法通过php上传文件

时间:2015-05-07 17:09:53

标签: php html mysql upload

我正在尝试通过php将图像上传到数据库中,但我遇到了以下问题。

Upload failed
Here is some more debugging info:
Notice:  Undefined index: filetoUpload in C:\Users\Konstantina\Desktop\Upload.php on line 39

Upload.php代码:

$uploaddir = 'upload/';

if(!file_exists($uploaddir)){
    if(mkdir($uploaddir,0777,true)){}else{echo "fail to create folder";}

}
    $uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);

    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }

    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:';
    echo($_FILES['filetoUpload']['errors']);

Insert.html代码:

<form action="Upload.php" method="post" enctype="multipart/form-data" name="myform" id="myform">
            <label >  Select image </label>
             <input type="file" name="fileToUpload" > 

<input id="upload" type="submit" name="submit" value="Upload">

        </form> 

我已开启file_uploads=On

2 个答案:

答案 0 :(得分:0)

您输出错误的索引以检索错误:

echo($_FILES['filetoUpload']['errors']);

应该是:

echo($_FILES['fileToUpload']['errors']);

除此之外,我们可以根据手册了解错误:

http://php.net/manual/es/features.file-upload.errors.php

答案 1 :(得分:0)

我认为您使用上传目录的完整路径

$uploaddir = 'upload/';

使用

 $uploaddir = '/var/www/upload/';    

你也有

    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:';
    echo($_FILES['filetoUpload']['errors']); // here to is small t while in other you have To