最近在用Drupal主题做一个网站,其中这个主题包含了Slider这个图片幻灯的module。同过后台上传图片文件可以在前台动态展现。但发现,只要是重启了系统后,之前上传的图片就自动消失找不到了,重新上传,但下次启动后还是找不到。
为了彻底解决问题,仔细检查了slider模块需要用到的文件及数据库表。最终解决问题步骤如下:
1. 首先在数据库,发现slider上传过程中用到了slider_upload和file_managed两张表记录上传的文件。
2. 在sites/all/modules/slider下,编辑slider.module文件:
/**
* Form submission handler for slider_form().here we perform both validation and submission
*/
function slider_form_submit($form, &$form_state)
{
$var=array('jpg', 'png', 'gif');
if(isset($form_state['values']['category']))
{
$limits = array( 'extensions' => 'jpg png gif', 'file_size' => 200000000);
$validators = array('file_validate_extensions' => array($limits['extensions']),);
$test = file_default_scheme() . '://slideshow';
if(!file_prepare_directory($test,'0777'))
{
form_set_error('Folder Creation', t('Please make a directory to upload the image.'));
}
if ( $file = file_save_upload('category', $validators, $test,$replace = FILE_EXISTS_RENAME))
{
$_file_name=$file->filename;
$query = db_insert('slider_upload')->fields(array('title','image_path','path'));
$query->values(array(
'title' => $form_state['values']['title'],
'image_path' => $_file_name,
'path' => $form_state['values']['path'],
));
$query->execute();
$pieces1 = explode(".", $_file_name);
$var1=in_array($pieces1[1],$var);
$form_state['redirect'] = 'admin/structure/slider';
}
else
{
form_set_error('fileupload', t('Your file should be in valid format.'));
}
}
}
function slider_preprocess_page(&$variables)
{
global $base_url;
$output='';
$output.='<div class="main_view">';
$output.='<div class="window">';
$output.='<div class="image_reel">';
$result= db_query("SELECT title,image_path, path FROM {slider_upload}")->fetchAll();
foreach ($result as $row)
{
/**默认是在sites default files slideshow下**/
$output.='<a href="'.$base_url.'/node/'.$row->path.'" title="'.$row->title.'"><img src="'.$base_url.'/files/slideshow/'.$row->image_path.'" alt="'.$row->title.'" /></a>';
}
$output.='</div>';
$output.='</div>';
$output.='<div class="paging">';
for($slide=1;$slide<=count($result);$slide++)
{
$output.='<a href="#" rel="'.$slide.'" title="'.$slide.'">'.$slide.'</a>';
}
$output.='</div>';
$output.='</div>';
$variables['slideshow']=$output;
}
function slider_form_edit_submit($form, &$form_state)
{
$var=array('jpg', 'png', 'gif');
if(isset($form_state['values']['category']))
{
$limits = array( 'extensions' => 'jpg png gif', 'file_size' => 200000000);
$validators = array('file_validate_extensions' => array($limits['extensions']),);
$test = file_default_scheme() . '://slideshow';
if(!file_prepare_directory($test,'0777'))
{
form_set_error('Folder Creation', t('Please make a directory to upload the image.'));
}
if($_FILES['files']['name']['category']){
if ( $file = file_save_upload('category', $validators, $test,$replace = FILE_EXISTS_RENAME))
{
$_file_name=$file->filename;
}
else
{
form_set_error('fileupload', t('Your file should be in valid format.'));
}
}
else
{
$_file_name=$form_state['values']['image_name'];
}
$query = db_update('slider_upload')
->fields(array('title' => $form_state['values']['title'],'image_path' => $_file_name,'path' => $form_state['values']['path']))
->condition('id',$form_state['values']['id'])
->execute();
$form_state['redirect'] = 'admin/structure/slider';
}
3. 编辑好 上述指定img上传目录和显示目录后,可看到效果。但重启系统后问题依旧。
注: file_default_scheme()对应的是public
4. 折腾了半天,原来是drupal的设置中文件系统上传默认目录一栏未作修改,默认是sites/default/files,这里我修改成files,并且slideshow这个文件夹权限要改成777。修改完成后,重启系统,并未出现图片丢失问题。
这里图片丢失的原因,难道是每次启动网站是sites/default/files下面的文件都要清空?