Hey there,

I'm using the Multimedia asstet field in a custom content type. When I try to upload a file, I get this error message:

Warning: filesize(): stat failed for temporary://test.jpg in file_save() (line 573 of /var/www/includes/file.inc).
Warning: filesize(): stat failed for temporary://test.jpg in file_save() (line 573 of /var/www/includes/file.inc).
The specified file temporary://test.jpg could not be copied, because no file by that name exists. Please check that you supplied the correct filename.
An error occurred and no file was uploaded.

In the Drupal logs (admin/reports/dblog) I get these error messages:

LOCATION:
http://localhost/en/media/browser?render=media-popup&types%5Bimage%5D=im...file_directory=null&file_extensions=jpg+jpeg+gif+png+...+ico&max_filesize=null&uri_scheme=public&plugins=undefined

REFERRER:
http://localhost/en/media/browser?render=media-popup&types%5Bimage%5D=im...file_directory=null&file_extensions=jpg+jpeg+gif+png+...+ico&max_filesize=null&uri_scheme=public&plugins=undefined

MESSAGE:
The file permissions could not be set on temporary://test.jpg.

and also in another log entry (with same location/referrer)

MESSAGE:
Warning: filesize(): stat failed for temporary://test.jpg in file_save() (line 573 of /var/www/includes/file.inc).

File permissions

I double checked the file/directory permissions of my temp folder, which is sites/default/files/temp:

# ls -l sites/default/files/
drwxr-xr-x 2 www-data www-data   4096 Nov 10 15:08 temp

And I can see temporary files from the Devel module inside the temp directory, so file permissions cannot be the cause. Uploading files through the media browser works, too.

Database entry

After the attempt to upload a file, a new entry can be found in file_managed table of drupal's database:

mysql> select * from file_managed where uri like "temp%" and uri not like "temp%devel%" limit 20;
+------+-----+----------+----------------------+------------+----------+--------+------------+-------+
| fid  | uid | filename | uri                  | filemime   | filesize | status | timestamp  | type  |
+------+-----+----------+----------------------+------------+----------+--------+------------+-------+
| 2820 |  84 | test.jpg | temporary://test.jpg | image/jpeg |        0 |      1 | 1320934211 | image |
+------+-----+----------+----------------------+------------+----------+--------+------------+-------+
1 row in set (0.01 sec)

I have to delete this row before I can make another attempt to upload the same file again.

Active Conrtib Modules

The strange thing is: The file upload works without problems in another Drupal installation. I assume it has something to do with another contrib module, not sure which one though.

Active modules on host where upload doesn't work

Here's a list of the activated contrib modules on the host where uploading doesn't work:

Administration           Administration menu (admin_menu)            7.x-3.0-rc1
Chaos tool suite         Chaos tools (ctools)                        7.x-1.0-rc1
Date/Time                Date (date)                                 7.x-2.0-alpha4
Date/Time                Date API (date_api)                         7.x-2.0-alpha4
Date/Time                Date Popup (date_popup)                     7.x-2.0-alpha4
Date/Time                Date Views (date_views)                     7.x-2.0-alpha4
Development              Devel node access (devel_node_access)       7.x-1.2
Domain Access            Domain Access (domain)                      7.x-2.15
Domain Access            Domain Configuration (domain_conf)          7.x-2.15
Domain Access            Domain Navigation (domain_nav)              7.x-2.15
Domain Access            Domain Settings (domain_settings)           7.x-2.15
Domain Access            Domain Theme (domain_theme)                 7.x-2.15
Fields                   Countries (countries)                       7.x-1.0
Fields                   Email (email)                               7.x-1.0
Fields                   Fieldgroup (field_group)                    7.x-1.1
Fields                   Link (link)                                 7.x-1.0
Fields                   Node Reference (node_reference)             7.x-2.0-beta3
Fields                   References (references)                     7.x-2.0-beta3
Media                    File entity (file_entity)                   7.x-1.0-rc2
Media                    Media (media)                               7.x-1.0-rc2
Media                    Plupload integration module (plupload)      7.x-1.0-beta3
Meta tags (quick)        Extra functionality (metatags_quick_extra)  7.x-2.2
Meta tags (quick)        Meta tags (quick) (metatags_quick)          7.x-2.2
Multilingual - Internat  Block languages (i18n_block)                7.x-1.1
Multilingual - Internat  Contact translation (i18n_contact)          7.x-1.1
Multilingual - Internat  Field translation (i18n_field)              7.x-1.1
Multilingual - Internat  Internationalization (i18n)                 7.x-1.1
Multilingual - Internat  Menu translation (i18n_menu)                7.x-1.1
Multilingual - Internat  Multilingual content (i18n_node)            7.x-1.1
Multilingual - Internat  Multilingual select (i18n_select)           7.x-1.1
Multilingual - Internat  Path translation (i18n_path)                7.x-1.1
Multilingual - Internat  String translation (i18n_string)            7.x-1.1
Multilingual - Internat  Synchronize translations (i18n_sync)        7.x-1.1
Multilingual - Internat  Taxonomy translation (i18n_taxonomy)        7.x-1.1
Multilingual - Internat  Translation redirect (i18n_redirect)        7.x-1.1
Multilingual - Internat  Translation sets (i18n_translation)         7.x-1.1
Multilingual - Internat  Variable translation (i18n_variable)        7.x-1.1
Other                    Backup and Migrate (backup_migrate)         7.x-2.2
Other                    Entity API (entity)                         7.x-1.0-rc1
Other                    Entity tokens (entity_token)                7.x-1.0-rc1
Other                    Pathauto (pathauto)                         7.x-1.0
Other                    Slider Field (sliderfield)                  7.x-1.0
Other                    Title (title)                               7.x-1.0-alpha2
Other                    Token (token)                               7.x-1.0-beta7
Other                    Variable (variable)                         7.x-1.1
Other                    Variable admin (variable_admin)             7.x-1.1
Other                    Variable realm (variable_realm)             7.x-1.1
Other                    Variable store (variable_store)             7.x-1.1
Taxonomy                 Taxonomy CSV import/export (taxonomy_csv)   7.x-5.7
User interface           Wysiwyg (wysiwyg)                           7.x-2.1
Views                    Views (views)                               7.x-3.0-rc1
Views                    Views UI (views_ui)                         7.x-3.0-rc1

Active modules on host where upload works fine

 Chaos tool suite         Chaos tools (ctools)                               7.x-1.0-rc1
 Chaos tool suite         Custom content panes (ctools_custom_content)       7.x-1.0-rc1
 Chaos tool suite         Page manager (page_manager)                        7.x-1.0-rc1
 Chaos tool suite         Views content panes (views_content)                7.x-1.0-rc1
 Context                  Context (context)                                  7.x-3.0-beta2
 Context                  Context UI (context_ui)                            7.x-3.0-beta2
 Date/Time                Calendar (calendar)                                7.x-3.0-alpha1
 Date/Time                Date (date)                                        7.x-2.0-alpha4
 Date/Time                Date API (date_api)                                7.x-2.0-alpha4
 Date/Time                Date Tools (date_tools)                            7.x-2.0-alpha4
 Date/Time                Date Views (date_views)                            7.x-2.0-alpha4
 Development              Devel (devel)                                      7.x-1.2
 Development              Theme developer (devel_themer)                     7.x-1.x-dev
 Features                 Events calendar feature (events_calendar_feature)  7.x-1.0
 Features                 Features (features)                                7.x-1.0-beta4
 Media                    File entity (file_entity)                          7.x-1.0-rc2
 Media                    IMCE (imce)                                        7.x-1.5
 Media                    Media (media)                                      7.x-1.0-rc2
 Media                    Plupload integration module (plupload)             7.x-1.0-beta3
 Multilingual             Localization update (l10n_update)                  7.x-1.0-beta2
 Multilingual - Internat  Internationalization (i18n)                        7.x-1.1
 Other                    Colorbox (colorbox)                                7.x-1.2
 Other                    Pathauto (pathauto)                                7.x-1.0
 Other                    Token (token)                                      7.x-1.0-beta7
 Other                    Variable (variable)                                7.x-1.1
 Panels                   Mini panels (panels_mini)                          7.x-3.0-alpha3
 Panels                   Panel nodes (panels_node)                          7.x-3.0-alpha3
 Panels                   Panels (panels)                                    7.x-3.0-alpha3
 Spam control             ASCII art CAPTCHA (ascii_art_captcha)              7.x-1.0-alpha1
 Spam control             CAPTCHA (captcha)                                  7.x-1.0-alpha3
 Theme Tools              Omega Tools (omega_tools)                          7.x-3.0-rc3
 User interface           IMCE Wysiwyg API bridge (imce_wysiwyg)             7.x-1.0
 User interface           Wysiwyg (wysiwyg)                                  7.x-2.1
 Views                    Views (views)                                      7.x-3.0-rc1
 Views                    Views UI (views_ui)                                7.x-3.0-rc1

Any hints?

CommentFileSizeAuthor
#2 debug_backtrace_output.txt111.14 KBfloeschie

Comments

floeschie’s picture

I added the debug($file) on top of the file_save() function in DRUPAL_ROOT/include/file.inc (line 571).

Uploading the file using the Multimedia asset field (which doesn't work) I get this debugging output:

User notice:
stdClass::__set_state(array(
   'uid' => '84',
   'status' => 1,
   'filename' => 'test.jpg',
   'uri' => 'temporary://test.jpg',
   'filemime' => 'image/jpeg',
   'filesize' => false,
   'source' => 'upload',
   'destination' => 'temporary://test.jpg',
   'timestamp' => 1321024615,
   'type' => 'image',
   'field_replacement' => 
  stdClass::__set_state(array(
     'uid' => '84',
     'status' => 0,
     'filename' => 'test.jpg',
     'uri' => 'temporary://test.jpg',
     'filemime' => 'image/jpeg',
     'filesize' => false,
     'source' => 'upload',
     'destination' => 'temporary://test.jpg',
     'timestamp' => 1321024615,
     'type' => 'image',
  )),
   'fid' => '2828',
   'display' => true,
))
in file_save() (line 573 of /var/www/conductix.com/www/includes/file.inc).

Using the Media browser's (admin/content/media) "Add file" functionality (which works), I get this debug output:

User notice:
stdClass::__set_state(array(
   'uid' => '84',
   'filename' => 'test.jpg',
   'uri' => 'public://test.jpg',
   'filemime' => 'image/jpeg',
   'filesize' => 56443,
   'timestamp' => 1321024778,
   'status' => 1,
   'is_new' => true,
))
in file_save() (line 573 of /var/www/conductix.com/www/includes/file.inc).
floeschie’s picture

StatusFileSize
new111.14 KB

And here is the output of the debug_backtrace() function (see PHP doc).

floeschie’s picture

Seems the "Select media" button from the Multimedia asset field passes a HTTP_GET parameter "file_directory=null" to the drupal_get_query_parameters() function... But I dunno where the button gets link target from...

floeschie’s picture

Disabling the plupload module makes the error message appear as well in the Media Browser, when I try to upload a file without the drag & drop feature...

floeschie’s picture

Ok, one step closer:

The chmod() function in DrupalLocalStreamWrapper::chmod() (line 343 of includes/stream_wrappers.inc) is muted (which imho is a bug itself... bad coding style!). Unmuting it gives me this error:

Warning: chmod(): No such file or directory in DrupalLocalStreamWrapper->chmod() (line 344 of /var/www/includes/stream_wrappers.inc).

Debugging the ouput of DrupalLocalStreamWrapper::getLocalPath() gives the correct local path (/var/www/sites/default/files/temp/test.jpg) so the temporary:// URI is translated correctly.

floeschie’s picture

Next hint:

It seems no temporary file is created in upload_temp_dir (which defaults to /tmp), so there's no file available which could be copied to temporary:// ...

floeschie’s picture

Project: D7 Media » File utils
Version: 7.x-1.0-rc2 » master
Component: Media Browser » Code

This error is not Media plugin related, as it also appears when using the regular Image field (for example when editing the user's profile image).

floeschie’s picture

Status: Active » Closed (duplicate)

This seems to be a duplicate of some wird move_uploaded_file() behaviour, see reported issue.

The problem resides in function drupal_move_uploaded_file() (includes/file.inc, line 1595-ish). Replacing the function with this code fixed it:

function drupal_move_uploaded_file($filename, $uri) {
  $realpath = drupal_realpath($uri);
  $result = move_uploaded_file($filename, $realpath);
  return $result;
}

Still I would like to know why this happens only on some hosts. I don't have safe mode on or open_base_dir restrictions active...

floeschie’s picture

Issue summary: View changes

corrected some typo's