如何用php查看用户上传的文件?

时间:2016-03-22 16:19:41

标签: php filesystems

我有一个脚本,允许用户在我的网站上传图片和视频。我需要查看它们,如何设置适当的权限?

chmod("upload/" . $_FILES["file"]["name"], 0755)

这不起作用,我希望我可以在Windows中使用这些文件,因为我需要处理它。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您不需要设置PHP上传的文件的chmod,因为Web服务器已经“拥有”该文件。

处理档案

要在浏览器中查看它,您需要确保将文件从临时目录移动到上载目录,其内容类似于以下内容:

// Define where the uploads directory is
$upload_dir = '/var/www/uploads/';
// Define the upload path from $upload_dir
$upload_file = $upload_dir . basename($_FILES['file']['name']);

// Move the tmp uploaded file to the uploads directory
if( !move_uploaded_file($_FILES['file']['tmp_name'], $upload_file) ) {
    // Handle the failure to move the temp file
    echo "File did not upload";
};

您可以通过var转储路径或检查文件是否存在来检查文件是否已移动到适当的位置。

// Is path correct?
var_dump($upload_file);
// Did the file get moved?
var_dump(file_exists($upload_file));

还有问题吗?

如果文件没有上传到服务器,则可能存在几个问题。请参阅此PHP文档page on uploads pitfalls。总结:

  • 您正在尝试直接访问该文件,而uploads目录位于您的webserver文档根目录之上,因此守护程序无法对其进行操作。
  • upload_max_filesize 默认为2Mb,这通常对于图片/视频而言太小。 (php.ini中)
  • max_execution_time 默认为60秒,如果是大型图片或慢速连接的视频,则可能超过60秒。 (php.ini中)
  • 上传较大的文件时,可能会超出
  • memory_limit 。 (php.ini中)

php.ini中查看这些内容,并根据需要增加其值以供选择。

要检查的另一个选项是,由于 .htaccess 文件mod重写,上传目录未被阻止提供图像。