PHP文件上传 - 文件无法从/ tmp /生产中读取,但它们位于localhost中

时间:2014-11-13 02:16:08

标签: php codeigniter ubuntu file-permissions linode

我坚持可能是文件权限问题。我已经花了10个小时直接在这上面,我就放弃了,因为我根本不知道如何解决它。我的脚本也适用于localhost。

我在CodeIgniter应用程序的控制器中有以下代码作为文件上传脚本的一部分:

$this->load->library('image_lib');

$config['upload_path'] = '/tmp/';
$this->load->library('upload', $config);
$this->upload->do_upload("selected_file");

// Variables used later when resizing an image
$originalPostImageFileUploadPath        = $this->upload->data()['full_path'];
$originalPostImageFileUploadFilePath    = $this->upload->data()['file_path'];
$originalPostImageFileRawName           = $this->upload->data()['raw_name'];
$originalPostImageFileExt               = $this->upload->data()['file_ext'];
$originalPostImageFileUniqId            = uniqid();


print_r($this->upload->data());
echo file_get_contents($originalPostImageFileUploadPath);

这是我上传文件时得到的输出:

Array
(
    [file_name] => fosters.jpg
    [file_type] => image/jpeg
    [file_path] => /tmp/
    [full_path] => /tmp/fosters.jpg
    [raw_name] => fosters
    [orig_name] => 
    [client_name] => fosters.jpg
    [file_ext] => .jpg
    [file_size] => 49408
    [is_image] => 1
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  file_get_contents(/private/tmp/fosters.jpg): failed to open stream: No such file or directory</p>
<p>Filename: ajax/product.php</p>
<p>Line Number: 45</p>

如您所见,脚本似乎正确上传文件,但file_get_contents()失败,因为它无法找到该文件。

我尝试过的事情:

使用以下代码创建脚本以查看www-data是否具有足够的权限:

echo (is_writable('/tmp/') ? "yes" : "no");
echo (is_readable('/tmp/') ? "yes" : "no");

上述两个输出都返回&#39;是&#39; (真)

更改/ tmp /目录的chmod: chmod 777 /private/tmp/(就像这一样糟糕)

upload_max_filesize更改为128M(文件远未达到此大小)

我甚至尝试运行以下命令来查看文件是否上传到/ tmp /目录。它似乎确实如此,但它会立即被删除(在不到0.1秒的时间内)。

watch --interval=0.1 ls 

我还能尝试什么? whoami返回www-data,我是否正确地说chown wwww-data /tmp/之类的内容应该解决问题?帮助

1 个答案:

答案 0 :(得分:0)

通常通过PHP发送的文件上传发送到临时/文件夹/文件并删除如果你不使用推荐的功能,包括:move_uploaded_file()例如我的POSTed数组有:

name
size
type
tmp_name

我使用这样的东西:

$fileName=$_FILES["resume"]["name"];
$fileSize=$_FILES["resume"]["size"];
$fileType=$_FILES["resume"]["type"];
$fileTmpName=$_FILES["resume"]["tmp_name"];  
$targetFile="/srv/www/uploads/".$fileName;  // Where-ever you want your file to be, you can delete manually later on in the script

/* various checking of size and type etc */

if(!is_uploaded_file($fileTmpName)) {
    echo "Couldn't upload file";
    die(0);
}
if(!move_uploaded_file($fileTmpName,$targetFile)) {
    echo "Couldn't move file";
    die(0);
}

/* now target file exists and stays put */