0

我正在做一个简单的文件上传练习,我从我的 WebDev 类中得到——实际上我们只需要复制代码并集成它来满足我们的需求。我试图为我的项目这样做,可悲的是它会继续回显同样的错误。

<?php

$allowed_filetypes = array('.jpg','.gif','.bmp','.png', '.jpeg'); 
$max_filesize = 524288; 
$upload_path = 'uploads/'; 

$filename = $_FILES['userfile']['name']; 
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
var_export($_FILES, $ext);

 if(!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not allowed.');


 if(filesize($_FILES['usrfile']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is too large.');


 if(!is_writable($upload_path))
  die('You cannot upload to the specified directory, please CHMOD it to 777.');


 if(move_uploaded_file($_FILES['usrfile']['tmp_name'],$upload_path . $filename))
     echo 'Your file upload was successful, view the file <a href="' . $upload_path .  $filename . '" title="Your File">here</a>';
  else
     echo 'There was an error during the file upload.  Please try again.';

?>

它不断给我“错误的文件类型”错误,数组中包含所有定义的类型。

<form id='upload' action="uploadfile.php" method="POST" enctype="multipart/form-data">
    <table>
        <tr>
            <td >Choose a File to Upload</td> 
        </tr>
        <tr>
            <td >Select File</td>
            <td  ><input type="file"  name="userfile"></td>
        </tr>
        <tr>
            <td colspan=2 id="sub"><input type="submit" name="submit" value="submit" ></td>
        </tr>
    </Table>
</form>
4

3 回答 3

0

您可以使用它,因为首先您必须始终回显 $_FILES 数组,然后开始调试。将这一行从

$allowed_filetypes = array('.jpg','.gif','.bmp','.png', '.jpeg'); 

$allowed_filetypes = array('jpg','gif','bmp','png', 'jpeg'); 

更改以下行

if(filesize($_FILES['usrfile']['tmp_name']) > $max_filesize)  

if(filesize($_FILES['usrfile']['size']) > $max_filesize)

并删除该行

$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

并更改此行

if(!in_array($ext,$allowed_filetypes))  

if(!in_array($_FILES['usrfile']['type'],$allowed_filetypes))
于 2012-05-01T05:22:40.537 回答
0

$filename = $_FILES['usrfile'][' name '];

于 2012-05-01T05:16:51.957 回答
0

'tmp_name'索引包含一个临时文件名,而不是文件真正拥有的文件名。那存储在'name'. 有关信息,请参阅此页面

此外,您应该:

  1. 检查'error'索引中的错误。
  2. 用于pathinfo获取扩展名,
  3. 在搜索数组之前将其小写,
  4. 为上传的文件名添加一些随机性以避免覆盖现有文件。
于 2012-05-01T05:11:33.220 回答