PHP脚本和Ubuntu服务器权限问题

时间:2011-11-07 22:07:09

标签: php apache2 ubuntu-11.04

首先,我是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

2 个答案:

答案 0 :(得分:0)

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

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

的内容

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

答案 1 :(得分:0)

$target = "testimages/";
if(!is_writable($target))

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