Uploading of files

Last update on November 25, 2008 09:00 AM by deri58
Published by deri58

Uploading of files





>


The PHP language allows managing files uploading through HTML form.

Form for sending files


The first step is to create an html form which will allow the user to open a dialog box for selecting the file to send:

<FORM method="POST" action="[your file PHP for upload.php]" ENCTYPE="multipart/form-data">
<INPUT type=hidden name=MAX_FILE_SIZE VALUE=2048>
<INPUT type=file name="name_of_file">
<INPUT type=submit value="send">
</FORM>



You should however not forget the attribute ENCTYPE="multipart/form-data" which is however very important to disclose the form.

The field MAX_FILE_SIZE is however an indication of the maximum size to be uploaded by the browser. However this is not sufficient to ensure the maximum size of uploaded files. The value of the maximum size of the uploaded file is variable in the configuration file php.ini.

PHP Configuration to enable upload


The file configuration php.ini contain the guidelines that allows or not the sending of files through a form


file_uploads= On/Off determine files upload.
upload_tmp_dir = directory sets the temporary directory to host the uploaded file.
upload_max_filesize= 2M determines the maximum size allowed for the file. If the file exceed the limit, the server will send an error code.
post_max_size =indicates the maximum data size of a form. This directive takes precedence over upload_max_filesize, it must be ensured to have more than post_max_size upload_max_filesize


If you cannot access the configuration (for example: site hosted on the server of the ISP or a shared host), you can check the configuration through phpinfo.

<?
phpinfo();
?>

File recovery with PHP


The file as well as the information can be accessed through the variant superglobale $_FILES[].

To view the content, you can use the following script:
<pre><? print_r($_FILES); ?></pre>
The code will be as follows :

Array
(
[name_of_file] => Array
(
[name] => YourImage.jpg
[type] => image/jpg
[tmp_name] => complete_path_of_uploaded_file
[error] => 0
[size] => 1000
)

)

The above is JPEG image of 1mb size.

The fields $_FILES[name], $_FILES[type], $_FILES|error], $_FILES[size] allows to perform assessement of type of file, size, name verify errors.


You can thus examine errors as follows:
<?
if ($_FILES['nom_du_fichier']['error']) {
switch ($_FILES['name_of_file']['error']){
case 1: // UPLOAD_ERR_INI_SIZE
echo"The file exceed the limit allowed by the server ] (file php.ini) !";
break;
case 2: // UPLOAD_ERR_FORM_SIZE
echo " The file exceeds the limit allowed in the HTML form!");
break;
case 3: // UPLOAD_ERR_PARTIAL
echo " Sending the file has been interrupted during transfer
!";
break;
case 4: // UPLOAD_ERR_NO_FILE
echo " The file you sent has zero size
!");
break;
}
}
else {
// $_FILES['name_of_fle']['error'] value 0 or UPLOAD_ERR_OK
// no error
}
?>



The function move_uploaded_files() enable image transfer from temporary directory to destination directory

<ital>
<?

if ((isset($_FILES['name_of_file']['file'])&&($_FILES['name_of_file']['error'] == UPLOAD_ERR_OK)) {
$path_destination = '/var/www/files/';
move_uploaded_file($_FILES['name_of_files']['tmp_name'], $path_destination.$_FILES['name_of_file']['name']);
}
?> </ital
Best answers for « Uploading of files » in :
Uploading file on mediafire Show Uploading file on mediafire Mediafire is a free hosting website which allows you to share your files, images and other stuffs with other people. It is very simple to upload files and share them! This tip is designed to show you how...
Uploading Large Files Show Uploading Large Files You have surely encountered various difficulties for sending files through e-mail due file limitation. You have found the elucidation to your trouble. There are several means to transfer files and data from one...
Unable to attach files ShowUnable to attach files It may happen that when uploading files as attachment under Outlook Express, it takes a long time and finally do not send same to recipients. This may be due to high volume size of the file which however unable...
Managing iPod Without iTunes ShowManaging iPod Without iTunes Sharepod How to install Sharepod on your computer? How to use Sharepod? Yamipod Amarok How to install Amarok using Yast on your computer? Gtkpod Ephpod How to install Ephpod on your computer Managing songs...
Publish a PDF file on your website ShowPublish a PDF file on your Website Displaying a link to a PDF on your Website Displaying the PDF on your website Displaying a link to a PDF on your Website Proposing a PDF link on your own website is an operation that can seem...
Download Subdownloader ShowSubDownloader is an application to download and upload files. It can automatically find subtitles for files: DivX, MPEG, AVI, etc and DVDs. Each is done very quickly with a simple and intuitive interface. It allows automatically know the language. ...
FTP commands ShowThe FTP protocol FTP (File Transfer Protocol) is a protocol — meaning a standard language that lets two machines communicate — used so that computers of different types (or with different operating systems) can transfer files over a...
File sharing in Windows XP ShowAdvantages File sharing involves making the content of one or more directories available through the network. All Windows systems have standard devices making it easy to share the content of a directory. However, file sharing may lead to security...