查看使用 php 上传的照片的权限问题
Permission issues viewing photos uploaded with php
我正在学习使用 php 在 udemy 上上传图像文件的教程。我可以选择一张图片并将其上传到文件夹,没有任何问题。
当我点击图片上传到文件夹后,windows 照片查看器显示:"photo.png It appears that you don't have permission to view this file. Check the permissions and try again"。
当我检查权限时它说 "You must have read permissions to view the properties of this file"。
我使用chmod函数设置为0755,允许所有者读写,让其他人读取。我尝试更改 chmod 代码但没有帮助。
我认为这与我的服务器权限有关,但在 google 上找不到任何解决方案。我的图片已上传到 Abyss Web 服务器。
代码如下:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function upload_file() {
//setup
$tmp_name = $_FILES['file']['tmp_name'];
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES['file']['name']);
$max_file_size = 5000000; //5mb
$allowed_file_types = array('application/pdf; charset=binary');
$allowed_image_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
//check if image type is allowed
$image_check = getimagesize($tmp_name);
if(! in_array($image_check[2], $allowed_image_types)) {
//if not allowed image check if allowed file type
exec('file -bi' . $tmp_name, $file_check);
if(! in_array($file_check[0], $allowed_file_types)) {
return 'This file type is not allowed';
}
}
//check if file already exists
if(file_exists($target_file)) {
return 'Sorry that file already exists';
}
//check file size
if(file_exists($target_file)) {
return 'Sorry this file is too big';
}
//store the file
if(move_uploaded_file($tmp_name, $target_file)) {
chmod($target_file, 0644);
return 'Your file was uploaded';
}
else {
return 'There was a problem storing your file. Try again?';
}
}
if(! empty($_FILES)) {
echo upload_file();
}
?>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file">
<input type="submit" Value="upload">
</form>
由于使用专门用于测试动机的自定义 HTML 页面加载文件确实正确显示了图像,因此很可能是 盗链保护 问题。 (这是在对问题发表一些评论后发现的)。
例如,在 cPanel 中,有一个管理此功能的工具,它围绕着一个名为 .htaccess
的文件的使用。这个文件在 web 开发世界中用于很多事情。
有些人不喜欢他们受版权保护的图像被访问,因此避免没有经验的人(比方说,"people in userland")这样做的一种方法是启用此保护。这适用于您设置的任何给定文件扩展名。
解决此问题的一种方法是转到 cPanel 并禁用(或相应地修改)热链接保护功能。另一种方法是找到导致问题的 .htaccess
文件,这需要了解它的工作方式和使用的语法。
我正在学习使用 php 在 udemy 上上传图像文件的教程。我可以选择一张图片并将其上传到文件夹,没有任何问题。
当我点击图片上传到文件夹后,windows 照片查看器显示:"photo.png It appears that you don't have permission to view this file. Check the permissions and try again"。
当我检查权限时它说 "You must have read permissions to view the properties of this file"。
我使用chmod函数设置为0755,允许所有者读写,让其他人读取。我尝试更改 chmod 代码但没有帮助。
我认为这与我的服务器权限有关,但在 google 上找不到任何解决方案。我的图片已上传到 Abyss Web 服务器。
代码如下:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function upload_file() {
//setup
$tmp_name = $_FILES['file']['tmp_name'];
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES['file']['name']);
$max_file_size = 5000000; //5mb
$allowed_file_types = array('application/pdf; charset=binary');
$allowed_image_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
//check if image type is allowed
$image_check = getimagesize($tmp_name);
if(! in_array($image_check[2], $allowed_image_types)) {
//if not allowed image check if allowed file type
exec('file -bi' . $tmp_name, $file_check);
if(! in_array($file_check[0], $allowed_file_types)) {
return 'This file type is not allowed';
}
}
//check if file already exists
if(file_exists($target_file)) {
return 'Sorry that file already exists';
}
//check file size
if(file_exists($target_file)) {
return 'Sorry this file is too big';
}
//store the file
if(move_uploaded_file($tmp_name, $target_file)) {
chmod($target_file, 0644);
return 'Your file was uploaded';
}
else {
return 'There was a problem storing your file. Try again?';
}
}
if(! empty($_FILES)) {
echo upload_file();
}
?>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file">
<input type="submit" Value="upload">
</form>
由于使用专门用于测试动机的自定义 HTML 页面加载文件确实正确显示了图像,因此很可能是 盗链保护 问题。 (这是在对问题发表一些评论后发现的)。
例如,在 cPanel 中,有一个管理此功能的工具,它围绕着一个名为 .htaccess
的文件的使用。这个文件在 web 开发世界中用于很多事情。
有些人不喜欢他们受版权保护的图像被访问,因此避免没有经验的人(比方说,"people in userland")这样做的一种方法是启用此保护。这适用于您设置的任何给定文件扩展名。
解决此问题的一种方法是转到 cPanel 并禁用(或相应地修改)热链接保护功能。另一种方法是找到导致问题的 .htaccess
文件,这需要了解它的工作方式和使用的语法。