简体   繁体   English

PHP上传的文件检索错误

[英]PHP Uploaded Files Retrieving Error

I am building on a simple upload system, below I have the upload system part of the code and the listing. 我建立在一个简单的上传系统上,下面是代码和清单中的上传系统部分。 So, I have the directory were all the files uploaded are listed, but when the user clicks on the download button it doesn't work, plus the fact that the code that displays the size shows 0 but the file does get uploaded. 所以,我有目录,列出了所有上传的文件,但是当用户单击下载按钮时,它不起作用,加上显示大小的代码显示为0,但文件确实被上传的事实。

I found out if the files get uploaded on my root directory it is fully functional. 我发现文件是否已上传到我的根目录中,它可以正常运行。 And when I change the directory to the root directory the files do get shown and the size and downloading part works ok. 当我将目录更改为根目录时,文件确实会显示,并且大小和下载部分都可以正常运行。

Upload System Code 上载系统代码

 if(isset($_FILES['image'])){
  $errors= array();
  $file_name = $_FILES['image']['name'];
  $file_size =$_FILES['image']['size'];
  $file_tmp =$_FILES['image']['tmp_name'];
  $file_type=$_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));


  $extensions= array("jpeg","jpg","png", "gif");

  if(in_array($file_ext,$extensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG, PNG, or GIF file.";
  }

  if(empty($errors)==true){
      move_uploaded_file($file_tmp, "dir/.$file_name");
  }else{
     print_r($errors);
  }

} }

Retrieving Uploaded Files Code: 检索上传的文件代码:

     $resource = opendir("dir/.");

        while (($entry = readdir($resource)) != false) {
        $download = '<a href="' . $entry . '"> Download </a> ';

        if ($entry != '.' && $entry != '..') {
            echo "<tr> <td>" . $entry . "</td> <td>" . formatSizeUnits(filesize($entry)) . "</td> <td> $download </td></tr>";   
        }
    }

readdir() doesn't return the full path of the files it finds, literally only their name. readdir()不会返回找到的文件的完整路径,实际上只会返回它们的名称。 Since you opened up dir/ as your source directory, you get somefile.txt , not dir/somefile.txt , which means your filesize is FAILING, because it's looking in the wrong plage. 由于您打开dir/作为源目录,因此得到somefile.txt ,而不是dir/somefile.txt ,这意味着您的文件大小为FAILING,因为它查找的是错误的文件。 That's not 0 bytes , it's a boolean FALSE being cast to integer 0. 不是0 bytes ,而是布尔值FALSE,被强制转换为整数0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM