PHP文件上载-生产环境中无法从/tmp/读取的文件,但它们位于localhost中
我陷入了一个可能是文件权限的问题。我已经连续花了10个小时在这个问题上,我已经快要放弃了,因为我真的不知道如何修复它。我的脚本也可以在localhost上完美地运行 我的CodeIgniter应用程序中的控制器中有以下代码作为文件上载脚本的一部分:PHP文件上载-生产环境中无法从/tmp/读取的文件,但它们位于localhost中,php,codeigniter,ubuntu,file-permissions,linode,Php,Codeigniter,Ubuntu,File Permissions,Linode,我陷入了一个可能是文件权限的问题。我已经连续花了10个小时在这个问题上,我已经快要放弃了,因为我真的不知道如何修复它。我的脚本也可以在localhost上完美地运行 我的CodeIgniter应用程序中的控制器中有以下代码作为文件上载脚本的一部分: $this->load->library('image_lib'); $config['upload_path'] = '/tmp/'; $this->load->library('upload', $config); $t
$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>
上述两个输出都返回“是”(真)
更改/tmp/目录的chmod:
chmod 777/private/tmp/
(尽管如此糟糕)
将upload\u max\u filesize
更改为128M
(文件的大小与此相差甚远)
我甚至试着运行以下命令来查看文件是否上传到/tmp/目录。看起来是的,但它会在瞬间被移除(不到0.1秒)
我还能试什么
whoami
返回www-data
,那么我说的chown-ww-data/tmp/
应该解决这个问题,对吗?帮助通常通过PHP上传的文件会被发送到临时/文件夹/文件,如果不使用建议的功能,则会被删除,这些功能包括:移动上传的文件()
例如,我发布的数组有:
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 */
这并没有回答OP的问题。OP指的是codeigniter.yea,但它称为一个php文件,这是他的问题。感谢您的回复。我没有使用move_uploaded_file()函数的原因是我正在对文件进行验证检查,并在脚本中稍后上传到Amazon S3。问题是,我的脚本在我的生产服务器上似乎根本不起作用,而且我似乎不知道应该更改哪个设置才能使其起作用:(
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 */