Hi, I'm after a bit of help. I've created 2 presets, but the folders aren't actually being created in the file system and images will be upload to /sites/default/files/imagefield_thumbs. The images I upload are being placed in the /sites/default/files folder , but when I try and view the file directly I get a 404 not found.

clean URLs+ file system is public.

The permissions are ok, uploads are possible.

.htaccess have

ewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

and

SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
Options +FollowSymlinks

Does anyone know what the problem might be?

Thanks in advance.

Comments

develCuy’s picture

I'm using the following patched imagecache_create_url function:

function imagecache_create_url($presetname, $filepath, $bypass_browser_cache = FALSE, $absolute = TRUE) {
  $args = array('query' => empty($bypass_browser_cache) ? NULL : time());
  $file_directory = file_directory_path();

  // Determine the path of the derivative inside the files directory.
  $derivative_path = 'imagecache/'. $presetname .'/'. _imagecache_strip_file_directory($filepath);

  // Then construct a full path and see if anyone wants to alter it.
  $altered_path = $old_path = $file_directory .'/'. $derivative_path;
  drupal_alter('file_url', $altered_path);

  // If any module has altered the path, then return the alteration...
  if ($altered_path != $old_path) {
    // ...but use url() so our $bypass_browser_cache parameter is honored.
    return url($altered_path, $args);
  }

  // It was unchanged so use the download method's prefix.
  $prefix = array(
    FILE_DOWNLOADS_PUBLIC => $file_directory,
    FILE_DOWNLOADS_PRIVATE => 'system/files',
  );
  $path = $prefix[variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)] .'/'. $derivative_path;

  // Temporarily turn off clean urls setting by default.
  global $conf;
  $bk = $conf['clean_url'];
  $conf['clean_url'] = 0;

  $rs = url($path, $args + array('absolute' => $absolute));

  // Restore clean urls setting.
  $conf['clean_url'] = $bk;

  return $rs;
}
jrsinclair’s picture

I had the same issue. Patched imagecache_create_url() function fixed the issue for me, although I don't know if this is really the best approach. But thank you develCuy for finding at least some solution.

Josias’s picture

It worked for me!

Thanks.

sandrewj’s picture

Status: Active » Postponed (maintainer needs more info)

For anyone using this patch:
Please verify:
Apache serves the image files in your files directory (including in imagecache)

This patch seems to be a work-around for the public file system callback not be triggered when requesting a image that is not yet generated.

If this patch works for you, I believe it would be better to generate the image in the imagecach_create_url function and still return the nice looking url.

function imagecache_create_url($presetname, $filepath, $bypass_browser_cache = FALSE, $absolute = TRUE) {
  $args = array('query' => empty($bypass_browser_cache) ? NULL : time());
  $file_directory = file_directory_path();

  // Determine the path of the derivative inside the files directory.
  $derivative_path = 'imagecache/'. $presetname .'/'. _imagecache_strip_file_directory($filepath);

  // Then construct a full path and see if anyone wants to alter it.
  $altered_path = $old_path = $file_directory .'/'. $derivative_path;
  drupal_alter('file_url', $altered_path);

  // If any module has altered the path, then return the alteration...
  if ($altered_path != $old_path) {
    // ...but use url() so our $bypass_browser_cache parameter is honored.
    return url($altered_path, $args);
  }

  // It was unchanged so use the download method's prefix.
  $prefix = array(
    FILE_DOWNLOADS_PUBLIC => $file_directory,
    FILE_DOWNLOADS_PRIVATE => 'system/files',
  );
  $path = $prefix[variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)] .'/'. $derivative_path;

  imagecache_generate_image($presetname, $filepath)

  $rs = url($path, $args + array('absolute' => $absolute));
  return $rs;
}

imagecache_generate_image only generates the image if it does not exist.

sandrewj’s picture

if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC && variable_get('clean_url', '0')){
		imagecache_generate_image($presetname, $filepath);
}

A little bit better option for the end of the function. The if statement checks that the file system is public and clean urls are disabled.

develCuy’s picture

Thank you @sandrewj, I have tested your version and worked fine, having the nice url is great for SEO!
Just removed variable $rs which is not needed:

function imagecache_create_url($presetname, $filepath, $bypass_browser_cache = FALSE, $absolute = TRUE) {
  $args = array('query' => empty($bypass_browser_cache) ? NULL : time());
  $file_directory = file_directory_path();

  // Determine the path of the derivative inside the files directory.
  $derivative_path = 'imagecache/'. $presetname .'/'. _imagecache_strip_file_directory($filepath);

  // Then construct a full path and see if anyone wants to alter it.
  $altered_path = $old_path = $file_directory .'/'. $derivative_path;
  drupal_alter('file_url', $altered_path);

  // If any module has altered the path, then return the alteration...
  if ($altered_path != $old_path) {
    // ...but use url() so our $bypass_browser_cache parameter is honored.
    return url($altered_path, $args);
  }

  // It was unchanged so use the download method's prefix.
  $prefix = array(
    FILE_DOWNLOADS_PUBLIC => $file_directory,
    FILE_DOWNLOADS_PRIVATE => 'system/files',
  );
  $path = $prefix[variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)] .'/'. $derivative_path;

  if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC && variable_get('clean_url', '0')){
    imagecache_generate_image($presetname, $filepath);
  }

  return url($path, $args + array('absolute' => $absolute));
}
sandrewj’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)
jadowd’s picture

This still doesn't work for me. I have memcache installed, and am using nginx over everything else... thoughts?

sandrewj’s picture

Are you using the patch in #6? Do you have clean urls disabled? Is your file system public?

If you answered no to any of those questions you can try the patch in #4, which will generate the image even on a private filesystem or with clean urls enabled.

zonezen’s picture

Thank you @sandrewj.

jadowd’s picture

Still working with rc1, I just want to make sure that the changes outlined in this thread, specifically the addition of the following code:

-------------------------------------8<-------------------------------------

if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC && variable_get('clean_url', '0')){
imagecache_generate_image($presetname, $filepath);
}

------------------------------------->8-------------------------------------

has been added to the source for this module? I seem to have recalled having this fixed, but somewhere along the way, I must have done an upgrade, but the problem returned. It would be nice not to have to apply this hack over and over again across the many sites that I maintain.

thanks,

jadowd’s picture

yeah, I'm doing updates today, and the fix is not in the mix. Can you re-apply the patch to the code base, and push it out in a release candidate 3 iteration of the module?

mattwmc’s picture

Thank you Lord for this!

I've been trying to figure this out for days.

Appears to be a Linux issue: clean urls off and file set to private makes imagecache work.

Now this patch works with clean urls on and files set to public on linux.

I found this through here: https://www.redfoxhosting.com/forums/threads/5720-RESOLVED-Drupal-Uberca...

Which made me google clean urls off and imagecache.

This should really be added to the list of things and fixes to check when trying to figure out why imagecache isn't working.

Note: I used the patch in #6.

#4 gave me some kind of T variable error or whatever.

jerry’s picture

Version: 6.x-2.0-beta12 » 6.x-2.0-rc1
Category: support » bug
Status: Closed (fixed) » Active

Setting this back to active, as I encountered the problem myself today and the fix does not appear to be in the current dev release.

roball’s picture

Status: Active » Needs review

Try the following content in the .htaccess file residing in the sites/example.com/files directory that serves as the public file system path for the site www.example.com:

SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
Options None
Options +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_URI} ^/sites/example\.com/files/imagecache/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ /index.php?q=sites/example.com/files/$1 [L,QSA]

  RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
  RewriteRule ^.* - [L,F]
</IfModule>