0

您好我正在尝试从我的 android 应用程序将图像文件上传到我的服务器。似乎我无法将任何文件上传到我的服务器而不是特定目录(图片)。如果我尝试上传到图片的子目录,那么 fopen 会返回 false。这是我的 php 文件

<?php
$base=$_REQUEST['image'];
$picname=$_REQUEST['picname'];
$binary=base64_decode($base);

header('Content-Type: bitmap; charset=utf-8');

//$shapicname = SHA1($picname);

$dir1 = 'pics/'.substr($picname,0,2);
$dir2 = substr($picname,2,2);

$targetfilename1 = $dir1;
$targetfilename2 =  $dir2;

if (!file_exists($dir1)) {
    mkdir($dir1); //create the directory
    chmod($targetfilename1, 0777); //make it writable
}

//chdir($dir1);

$file = fopen('/var/www/html/'.$dir1.'/'.$picname, 'wb');


if (!$file) {
   echo "false ".$dir1.$picname;
}
else {
   $fwrite = fwrite($file,$binary);

   fclose($file);

   echo "uploaded".$fwrite.$file;
}
?>

pics 文件夹归 apache 用户所有,拥有 0777 权限

有任何想法吗?

我正在发布 php 错误日志

[2012 年 8 月 2 日星期四 18:32:34] [错误] [客户端 xx.xx.Xxx.xx] PHP 警告:fopen():安全模式限制生效。uid/gid 为 0/0 的脚本不允许访问第 23 行的 /var/www/html/uploadshaimage.php 中的 uid/gid 48/48 拥有的 /var/www/html/pics/53 [Thu Aug 2012 年 2 月 18 日 32 分 34 秒] [错误] [客户端 xx.xx.Xxx.xx] PHP 警告:fopen(/var/www/html/pics/53/5344.jpg):无法打开流:没有这样的文件或第 23 行 /var/www/html/uploadshaimage.php 中的目录

4

3 回答 3

1
  1. 文件夹权限
  2. PHP.ini 设置 - 允许打开 url 或未启用文件功能。
  3. 路径错误 - 验证文件或路径是否正确或确实存在。
于 2012-08-02T16:31:22.617 回答
0

好的,我找到了 2 个有效的解决方案

A. 在 php.ini 上关闭 safe_mode(有点冒险!)

B. 将 pics 目录的权限递归地赋予子目录(gid 是问题!)

感谢大家的帮助

于 2012-08-03T08:50:33.263 回答
0

你知道,我不禁想知道,你确定这条路真的是'/var/www/html/'.$dir1.'/'.$picname吗?您正在使用相对路径$dir1,但在fopen. 如果$dir1从一开始就设置为绝对路径会发生什么?


FWIW,您输出的内容类型也错误。

于 2012-08-02T17:17:04.503 回答