move_upload_file()显示成功,但文件未反映在目标文件夹中

时间:2018-07-18 11:01:08

标签: php html file

代码已显示成功消息,因为文件已上传但未反映在目标目录中。目标目录必须具有写权限,并且必须在根目录中。

<?php
$currentDir = getcwd();
$uploadDirectory = "/tmp";

$errors = []; // Store all foreseen and unforseen errors here

$fileExtensions = ['csv']; // Get all the file extensions

$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
$fileTmpName  = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));

$uploadPath = $currentDir . $uploadDirectory . basename($fileName); 

if (isset($_POST['submit'])) {

    if (! in_array($fileExtension,$fileExtensions))
    {
        $errors[] = "This file extension is not allowed. Please upload a .csv file";
    }

    if ($fileSize > 2000000) {
        $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
    }

    if (empty($errors)) {
        $didUpload = move_uploaded_file($fileTmpName, $uploadPath);

        if ($didUpload) {
            echo "The file " . basename($fileName) . " has been uploaded";
        } else {
            echo "An error occurred somewhere. Try again or contact the admin";
        }
    } else {
        foreach ($errors as $error) {
            echo $error . "These are the errors" . "\n";
        }
    }
}


?>

	    <form action="upload.php" method="post" enctype="multipart/form-data">
			        Upload a File:
			        <input type="file" name="myfile" id="fileToUpload">
			        <i

2 个答案:

答案 0 :(得分:0)

检查您的$uploadPath = $currentDir . $uploadDirectory . basename($fileName);

例如var_dump($uploadPath); die();

可能是您必须在路径中添加额外的“ /”,而不是将字符串与“。”放在一起。

答案 1 :(得分:0)

首先是Localhost还是在线服务器上? 第二个Windows还是Linux? 现在,检查Apache的php.ini并搜索:

file_uploads = On 

应将其设置为开。还要寻找:

upload_max_filesize
post_max_size

也许您需要增加它们的值。