上传.xlsx无法使用php

时间:2017-03-14 18:21:56

标签: php

将上传内容发送到我的网站,该网站目前只在localhost上运行。尝试上传.xlsx文件会给我预定义的错误"抱歉,您无法上传此文件。抱歉,您的文件未上传。"代码如下:

    <?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$uploadFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($uploadFileType != "xlsx") {
    echo "Sorry, you cannot upload this file.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

形式:

    <form id="uploadbanner" enctype="multipart/form-data" method="post" action="uploads.php">
<input id="fileupload" name="myfile" type="file" />
<input type="submit" value="Submit" id="submit" />
</form>

2 个答案:

答案 0 :(得分:0)

将输入文件重命名为name="fileToUpload"将解决您的问题。

<form action="" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

答案 1 :(得分:-1)

试试这个:

$target_file = $_FILES["myfile"]["name"];
$uploadOk = 1;
$uploadFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check file size
if ($_FILES["myfile"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($uploadFileType != "xlsx") {
    echo "Sorry, you cannot upload this file.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["myfile"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["myfile"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}