Uploading files to the server
By default, a website is configured to allow an administrator to upload files of any type not more than 1 megabyte in the folder files.
Connect as an administrator and click on the wheel on the home page to go to the Management page.
Content
Click on Upload and choose a local file.
If the copy failed, an alert is displayed.
Make sure the Apache server is allowed to write in the directory files at the root of the website:
$ cd /var/www/sitename.net
$ sudo chgrp www-data files
$ chmod 775 files
NOTE: If the code in JavaScript is blocked, the form still works but without the ability to hide the Choose File
button.
To change the maximum size of a downloaded file or control its type, edit the block upload
:
upload.php
- define('FILES_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'files');
- function upload($lang, $slice=false) {
- $maxfilesize=$slice ? false : 1000000;
- $action='init';
- if (isset($_POST['upload_put'])) {
- $action='upload';
- }
- $filetypes=false;
FILES_DIR
gives the name of the folder where the downloaded files are copied.
$maxfiles
defines the maximum size of a downloaded file in bytes.
Set $filetypes
to the value of an array of MIME types to control the type of a downloaded file, e.g. array('image/jpeg', 'image/jpg', 'image/png')
.
The parameter $slice
of the function upload
allows to upload large files by packets of one megabyte.
Edit the action admin
and add true
to the call which builds the block $upload
:
admin.php
- $upload = build('upload', $lang, true);
Make sure the action fileupload
is enabled in aliases.inc:
aliases.inc
- $aliases = array(
- array(
- 'captcha' => 'captcha',
- // 'avatar' => 'avatar',
- 'rss' => 'rssfeed',
- 'file/upload' => 'fileupload',
Upload a file of several megabytes. The progression of the upload is displayed after each confirmation of the transfert of a packet.
The end of the copy is notified like for an upload in a single operation.
If the copy of a packet has failed, the transfer is interrupted and an alert is displayed.
To disable uploading files, set the block $upload
to false
in admin.php:
- $upload = false;
Disable the action fileupload
in aliases.inc:
- $aliases = array(
- array(
- 'captcha' => 'captcha',
- // 'avatar' => 'avatar',
- 'rss' => 'rssfeed',
- // 'file/upload' => 'fileupload',
Uploadging and playgin a video
Creating a block which can upload a video while playing it immediately takes a few minutes.
Click on the image to select a video.
Copy the file upload.php in the folder blocks calling it uploadvideo.php.
Restrict the type of uploadable files to MP4 videos:
- $filetypes=array('video/mp4');
Rename the function uploadvideo
. Remove all the code which tests if the file is uploaded in several parts:
- function uploadvideo($lang) {
- $maxfilesize=false;
- $videoupload_url = url('videoupload', $lang);
- $output = view('uploadvideo', $lang, compact('token', 'maxfilesize', 'filetypes', 'videoupload_url', 'errors', 'infos'));
Note the new action called videoupload
which will copy a video on the server.
Copy the file fileupload.php in the folder actions calling it videoupload.php.
videoupload
checks the type of the file and saves it in a subdirectory:
- $filetypes=array('video/mp4');
- $fname = FILES_DIR . DIRECTORY_SEPARATOR . 'videos' . DIRECTORY_SEPARATOR . $name;
Add a URL in aliases.inc for the action videoupload
:
- $aliases = array(
- array(
- 'captcha' => 'captcha',
- // 'avatar' => 'avatar',
- 'rss' => 'rssfeed',
- 'file/upload' => 'fileupload',
- 'video/upload' => 'videoupload',
The code for the view adds inserting a <video>
tag when a video is selected:
- <p id="videouploader"><img src="/files/images/html5video.png" alt="" title="Upload a video" /></p>
- $('#videouploader img').on('click', function(e) { e.preventDefault(); file.click(); });
- $('#videouploader').html('<video controls src="' + URL.createObjectURL(fd) + '"></video>');
Adding a button to interrupt the loading and delete the file on the server and separating selecting and playing a video and uploading it are easy extensions.
Comments