我试图制作一个非常简单的php上传器,只上传一个.CSV文件,我需要它来覆盖服务器上已经存在的文件。我试着处理上传部分,直到它工作起来。我已经在上传目录中设置了777权限,但在上传文件时不断出错。覆盖部分我还不能编码它,我可能需要一些帮助来制作它。.csv文件是1.5MB。上传文件时我得到的错误是说文件的扩展名错误,但我110%确信扩展名是.csv。
这是代码:
上传.php
<?php
// Configuration - Your Options
$allowed_filetypes = array('.csv',); // These will be the types of file that will pass the validation.
$max_filesize = 5000000; // Maximum filesize in BYTES (currently 5MB).
$upload_path = 'upload/'; // Upload directory
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not a *.CSV file.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is larger than 5MB.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
?>
HTML:
<form enctype="multipart/form-data" action="upload.php" method="GET">
Update CSV File: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
看起来您正在检查值为userfile
的$_FILE
索引,但您的表单正在发送值为uploaded_file
的索引
$filename = $_FILES['userfile']['name'];
应该是
$filename = $_FILES['uploaded_file']['name'];
您可以通过更改从GET到POST的方法此外,由于文件输入的名称为"uploaded_file"。同时更改
$_FILES['userfile']['name'] to $_FILES['uploaded_file']['name'];
应该肯定有效!