【问题标题】:PHP Script and Ubuntu Server Permissions IssuePHP 脚本和 Ubuntu 服务器权限问题
【发布时间】:2011-11-07 22:07:09
【问题描述】:

首先我是 PHP 和 Linux 的新手。我在尝试设置文件上传脚本时遇到问题。它停在 !is_writable 处。我觉得这是服务器上的权限问题,但我将所有内容都设置为 777。我错过了什么吗?

服务器是设置为具有本地访问权限的虚拟机的 Ubuntu 服务器。

页面代码

ini_set('error_reporting', E_ALL);
echo "<br/>";

//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']);

//This is the directory where images will be saved 
$target = "testimages/"; 
$allowed_filetypes = array('.jpg','.bmp','.png'); // These will be the types of file that will pass the validation.
$target = $target . basename( $_FILES['photo']['name']); 


$filename = $_FILES['photo']['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 allowed.');

// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($target))
die('You cannot upload to the specified directory, please CHMOD it to 777.');

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 

// Connects to your Database 
mysql_connect("localhost","phptestuser","ct6697") or die(mysql_error()) ;
mysql_select_db("Testing") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO employees ( name, email, phone, pic) VALUES ('$name', '$email', '$phone', '$pic')");

尝试了以下两个答案,但仍然无法正常工作,还有其他建议吗?

来自 ls -al 的列表:

drwxrwxrwx  9 root       root       4096 Nov  7 16:09 .
drwxr-xr-x 14 root       root       4096 Nov  7 16:53 ..
drwx------  2 www-data   www-data   4096 Nov  7 15:05 .gnupg
-rwxrw-rw-  1 yoderman94 www-data   2464 Nov  8 07:31 Add.php
-rwxrw-rw-  1 yoderman94 www-data    629 Nov  7 15:27 ImageUpload.php
-rwxr--r--  1 yoderman94 yoderman94 2115 Nov  7 15:45 PHP_Test.php
drwxr-xr-x  2 yoderman94 yoderman94 4096 Nov  1 14:39 _mmServerScripts
-rw-r--r--  1 root       root        177 Oct 27 11:32 index.html
-rw-rw-r--  1 yoderman94 yoderman94   44 Nov  1 10:19 phpinfo.php
drwxrwxrwx  2 www-data   www-data   4096 Nov  7 16:09 testimages
drwxrwxrwx  2 www-data   www-data   4096 Nov  7 15:08 tmp

【问题讨论】:

    标签: php apache2 ubuntu-11.04


    【解决方案1】:

    尝试chown - 将上传文件夹的所有者更改为网络服务器,通常是www-data;

    只需运行类似:sudo chown www-data:www-data ./testimages/

    并尝试使用绝对路径上传目录。

    【讨论】:

      【解决方案2】:
      $target = "testimages/";
      if(!is_writable($target))
      

      建议:尝试设置$target等于文件的绝对路径,可能是/var/www/testimages

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-04
        • 2015-01-28
        • 1970-01-01
        • 2014-10-30
        • 2018-01-11
        • 2011-05-17
        • 2011-07-31
        • 2014-12-07
        相关资源
        最近更新 更多