簡體   English   中英

PHP文件上傳-生產中無法從/ tmp /讀取文件,但它們位於localhost

[英]PHP file upload - files not readable from /tmp/ in production but they are in localhost

我被卡在可能是文件權限問題。 我已經花了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");

以上兩個輸出均返回“是”(true)

更改/ tmp /目錄的chmod 777 /private/tmp/chmod 777 /private/tmp/ (雖然很糟糕)

upload_max_filesize更改為128M (文件距離此大小不遠)

我什至嘗試運行以下命令來查看文件是否上傳到/ tmp /目錄。 看起來確實如此,但是它會在瞬間(不到0.1秒)內被刪除。

watch --interval=0.1 ls 

我還能嘗試什么? whoami返回www-data ,那么我說的是chown wwww-data /tmp/應該可以解決此問題嗎? 救命

通常,通過PHP上傳的POST文件會發送到一個臨時/ folder / file中,如果不使用建議的功能,則會刪除這些文件,其中包括: 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 */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM