Help:Uploading Files

From wiki.thepatternofeverything.org
Jump to: navigation, search

There are several potential stumbling blocks in getting file upload working correctly

  • php.ini file usually is here /usr/local/lib/php.ini

Frequently Asked Questions

Q: How do Wikipedia and other Wikimedia projects have a "Select a License" menu on the file upload page? How do we add a similar menu to our own wiki?

A: The Mediawiki:Licenses page is where the contents of the menu are defined. If you add options to that page the menu will magically appear.

Q: Fatal error: mime_magic could not be initialized, magic file is not available in /opt/lampp/htdocs/includes/MimeMagic.php on line 506

Why do I get this error, what can I do? Is there something I can put into LocalSettings.php?

Fatal error: mime_magic could not be initialized, magic file is not available in /opt/lampp/htdocs/includes/MimeMagic.php on line 506

A: Answer needed here please!

Q: After setting $wgEnableUploads = true, I still can't upload any image files. I get the following error (using MW 1.5.6):
Could not copy file "/tmp/phpWdGsMs" to "/var/www/vhosts/railgunn.com/httpdocs/pipelinewiki/images/temp/20060317020027!Weird_uvs.jpg".

A: Make sure the permission in all the files below images are all set to rwx on linux/unix chmod -R777 images/ (-R recursively)

and - if php safe_mode is on - that /images and all its subdirectories are owned by the same owner as the main script (index.php)

Q: How do I enable uploading files on my MediaWiki installation? Template:MW 1.5Template:MW 1.4

  • Add "$wgEnableUploads = true;" to your LocalSettings.php. This causes an "Upload File" link to be displayed in the "toolbox" menu.
    • Note that in versions up to 1.4 this used to be add "$wgDisableUploads = false;"

Don't forget to log-out and log-in again to see the "Upload file" link (this happens with 1.4.7 version)

Q: How do I enable uploading files on my MediaWiki install for IIS6 and mediawiki 1.5?

Q: But it says it has to be "specially set up to be secure"... how can I be secure?

  • I'd highly recommend you clean up your config, yes...
  • Also, I'd recommend you upgrade to the latest version... 1.2.0 has tighter controls on uploaded filetypes. See the RELEASE_NOTES file.

Q: Why do I get the message: "The file you uploaded seems to be empty. This might be due to a typo in the file name. Please check whether you really want to upload this file."

One reason for this is that something below mediawiki such as PHP or the web server is limiting the size of http requests. See the next question.

Q: What affects the maximum file size that can be uploaded?


  • Change "upload_max_filesize = <Number>M" in your php.ini file, where <Number> is the limit in MBytes.
  • Change "post_max_size =<Number>M" in your php.ini file as well and restart apache.
  • If this php4 parameter is too low, you get the error message "The file you uploaded seems to be empty. This might be due to a typo in the file name. Please check whether you really want to upload this file."
  • Apache has a directive "LimitRequestBody", which controls the maximum number of bytes allowed in ANY http request. If this limit is not somewhat larger than upload_max_filesize, the above message will appear for files near or above this limit. This directive may appear in various places in the apache configuration files, or in an .htaccess file. The effect of this directive may be global, or limited to a particular virtual host or directory, depending on where it occurs. Consulting the Apache documentation is advised if you run across this directive to understand how it is being used.
Some distributions use this directive to guard against denial of service attacks. For example the php module configuration for Apache2 on RedHat limits the request body size to 524288 bytes. Look at /etc/httpd/conf.d/php.conf. -- Other distributions may place the directive in other places according to their individual configuration file policies.
  • MediaWiki's SpecialImport.php<code> page has a hardcoded <code>MAX_FILE_SIZE on the form that you may need to increase manually. [1]
  • PHP's memory_limit may also place a limit on the maximum file size. See the note below if you find that you are getting a memory exhausted error in the Apache error log.

Cautions

  • The main thing you want to be sure of is that if someone uploads a .php file or other script it won't execute on your webserver!
  • The second thing you want to be sure of is that if someone uploads a .html (or .htm or .shtm or...) file with embedded javascript, it won't execute (potentially able to steal session and password cookies from visitors).

Q: How do I do that?

In httpd.conf, add these lines:

Alias /upload /..../upload
<Directory "/..../upload">
Options None
php_flag engine off
</Directory>

Replace "...." with your path name.

Safe Mode Restrictions

If you get a safe mode Restrictions in effect error, it is caused by your host running php in safe mode. PHPO will not be able to create directories for the uploaded files.

Q: Is there a workaround to this safe mode problem? I would like to be able to use images in my wiki. Embedding external images is nice but it requires absolute URL's, which I hate. Couldn't Mediawiki handle some sort of "static-images" folder?


## If you want to use image uploads under safe mode, 
## create the directories images/archive, images/thumb and 
## images/temp, and make them all writable, along with images/ . Then uncomment 
## this, if it's not already uncommented:
# $wgHashedUploadDirectory = false;

(you can find this variable in includes/DefaultSettings.php)


See the PHP manual entry on safe mode for details.

A quote from a comment to the PHP manual: Beware: Safe mode will not permit you to create new files in directories which have different owner than the owner of the script. That is, in safe mode, images/archive, images/thumb and images/temp must be owned by the user who also owns the apache process. [for /images itself it is not the owner of the apache process but the owner of the script, ie index.php]


A small note: After adding the the additional directories, and chmodding them to 777, I still had a problem with safe mode where it failed to upload because the scripts could not access the uploaded files that apache was making (in safe mode, scripts owned by one user cannot access scripts owned by another). To get around this, I did the following from shell as root:

  • Find the default owner for files on your site - I use ensim, so the first site admin, is the user 'admin1'
  • Create a new group called 'wiki_users': 'groupadd wiki_users'
  • Add admin1 (or whatever your admin user is) to that group: 'usermod -G wiki_users admin1'
  • CD to the wiki root folder and: 'chown -R apache:wiki_users *'
  • That makes all files owned by apache and members of the wiki_users group. So, apache should be able to upload and move files about, and admin1 should be able to access all scripts.

I'm not sure how secure this is - I use it on a private server, but it worked for me.

Another small note: After I tried these suggestions, my upload still didn't work. I set $wgHashedUploadDirectory to false, but made the directories writable. This worked for me.

Another HowTo
This worked for me(with a shared host under safe_mode):

 # wikiroot/images, wikiroot/images/temp, wikiroot/images/archive, wikiroot/images/thumb
 # all set to 0777 to allow apache to write to them
 $wgHashedUploadDirectory = false;
 $wgUploadPath       = "$wgScriptPath/images";
 $wgUploadDirectory  = "/absolute_path_to_wiki_root/images";

Images Not Working

Q: Can anyone help with this problem?? http://meta.wikimedia.org/wiki/Talk:Uploading_files#Images_Not_Viewable

A: I found guidance here: http://www.linuxquestions.org/questions/showthread.php?postid=1722568 . I added an index.html file to the images directory, and double-checked that my permissions were set, and it started working. Elonka 03:11, 11 November 2005 (UTC)

Fatal error: Allowed memory size of 8388608 bytes exhausted

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 80629 bytes) in .../includes/Parser.php on line 206

Editing pages with lots of content.

php.ini -- memory_limit = 8M -- increase it.

This also resolves "The file you uploaded seems to be empty. This might be due to a typo in the file name. Please check whether you really want to upload this file." when uploading files, when all other things mentioned on this page do not work.

The memory_limit set in php.ini may be overridden by LocalSettings.php. Check for a line that says something like:

ini_set( 'memory_limit', '20M' );

In this case, update the LocalSettings.php file instead.

"Sorry, not logged in" when uploading files

If having trouble uploading files, it could be a permissions issue.


How to Upload Non Image Files?

I have to upload non image files like *.xls, *.doc etc. How to do this?

Add
$wgFileExtensions = array('gif','png','jpg','jpeg','xls','doc');
to the LocalSettings.php.


Certains types of files are banned from upload (exe, php, html, etc).

See $wgFileBlacklist in DefaultSettings.php -- Uploading Non Image File

It doesn't work --Testperson 08:07, 11 July 2006 (UTC)

Try adding $wgCheckFileExtensions = false;

It doesn't work either, but what it's explained under does: -- dgil 28 July 2006

$wgFileExtensions = array('gif','png','jpg','jpeg','xls','doc','ppt','wmv','swf');
$wgVerifyMimeType = false;
$wgStrictFileExtensions = false;
$wgCheckFileExtensions = false;

Is it possible to let only some users upload Images?

Yes. The easiest way to do this, under 1.5, is to remove the upload access from users, and assign it to a separate group.

Add the line

$wgGroupPermissions['uploadaccess' ]['upload']          = true;

to the end of your /LocalSettings.php file. This overrides the following line in your /includes/DefaultSettings.php file (don't edit that file!):

$wgGroupPermissions['user' ]['upload']          = true;

Anyone you want to give upload access to, add them to the uploadaccess group (Special:Userrights). This gives them global upload access, but you can restrict it. Sysops, under this change, still keep upload access.

The file is corrupt or has an incorrect extension. Please check the file and upload again.

Q: What should I do? I add $wgFileExtensions = array( 'png', 'gif', 'jpg', 'jpeg', 'txt', 'bin', 'rpm', 'pdf', 'java', 'doc', 'ppt', 'tar.gz', 'zip', 'rar' ); but some files (such as .doc and .zip files) will not upload.

A: Mediawiki is trying to validate the mime type of your upload. You can either turn this feature off or fix it.

To turn it off:

To fix it:

Or to fix otherwise:

  • Check the MIME Magic file used by PHP. In a phpinfo() section, check the mime_magic section to see if it's a) switched on (the mime_magic.magicfile exists in the php.ini & points to a correct magic-formatted file) and b) doesn't produce any errors. To check the latter, add (or alter if they exist) the lines "mime_magic.debug = On" and "display_startup_errors = On" to the php.ini & then run one of the wiki's maintenance scripts from the command line (rebuildImages.php is a good candidate) - redirect the error output to a file & check if there are any errors in the magic file. --Barthax 12:41, 31 January 2006 (UTC)
  • The MIME type detection depends upon the fileinfo PECL extension. Instructions for fedora core 5 are:
yum install php-devel
pecl install fileinfo
chcon -t httpd_sys_script_exec_t  /usr/lib/php/modules/fileinfo.so
cat <<EOF > /etc/php.d/fileinfo.ini
; Enable fileinfo extension module
extension=fileinfo.so
EOF

I get a Blank Screen (IE) and a Blank Upload Form (FireFox) after uploading an image

Q: I am using apache 1.3 and wiki 1.5. Looking at the directory, the file is not uploaded. Any suggestions?

A: Check the limit of upload_max_filesize and post_max_size in your php.ini.

I did as your instruct but I still can't upload files. A blank screen is always displayed after uploading images.

  • I'm experiencing the same problem. I can upload small .wmv files (which were added to the allowed uploads), but not large ones... somehow it all must be related... Jcobbers 18:40, 30 January 2006 (UTC)
  • Large (>3meg) wmv files won't work (This page cannot be displayed IE error). However, small wmv's work, and large (>10meg) flash or mp3 files do work. Also, the small wmv files that upload are claimed to be MIME type: text/plain (it is not!). In LocalSettings.php I have set:
# enable other files to be uploaded
$wgFileExtensions = array('gif','png','jpg','jpeg','xls','doc','ppt','wmv','swf');
$wgVerifyMimeType = false;
$wgStrictFileExtensions = false;
$wgCheckFileExtensions = false;

I suspect the MIME type encoding is wrong. So something related to that is either choking or disabling uploads for WMV files. Jcobbers 13:53, 31 January 2006 (UTC)

I fixed it! I'm running on a linux environment, so 'file -bi <name of file>' is a valid command. When I turned ON this method of identifying the mime type in LocalSettings.php, it worked.

# Sets external mime detector program. B/c using mime_content_type($file) did not work for wmv types (returned text/plain, 
# and then it was searched for malicious code in the file--causing something to hang).
$wgMimeDetectorCommand= "file -bi"; #use external mime detector (linux)

Why did I need to do this? Because wmv files were recognized as text/plain when not using 'file -bi' -- and the Sanitizer in SpecialUpload tries to scan the file for malicious code. It is too big of a file, and it choked. For more details, read this thread [2] or contact me Jcobbers 20:58, 2 February 2006 (UTC)

I get the blank upload form too, but it's not just Firefox but all browsers (including IE and Safari). Is this a different problem or is this related? I'm not using Apache, though, and I have to use "ugly" URL's.—Gniw 20:20, 14 February 2006 (UTC)

I had this problem with large files (under Fedora Core 4 and Apache) and fixed it by increasing the memory limits in LocalSettings.php:

ini_set( 'memory_limit', '64M' ); # Did not work when set to 20M

and in php.ini

memory_limit = 64M      ; Maximum amount of memory a script may consume (8MB)
...
upload_max_filesize = 100M

15:48, 25 February 2006 (UTC)


I have it installed locally to organize my work (apache on windows,php,mysql), I did all that, still, doesn't work, cannot upload files larger than 5 or 6 megs, get a blank upload form. Ideas?
I had the same problem, tried everything above but it still didn't work. Increase the 'post_max_size' variable in php.ini.