Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Handling File Uploads and Downloads    Marked Cool    (Review this resource)
Author: econner
Posted On: 2004-11-28
Category: Home > PHP Tutorials

Popularity: 4 points out of 10    

Description: This tutorial will show you two methods for file uploading and a nice way to display the progress of a download.

Total Hits: 7311     Total Votes: 1     Total Points: 3 (1 reviews)    

Page Navigation:  [1]


Handling file uploads & downloads


This tutorial will first show you how to handle file uploads and then how to create an image gallery. There are two methods for uploading files. One is using php to create an ftp session and uploading files from there. With the other, files are grabbed directly from the computer and "pasted" to the server via post vars. We will go over both in detail.

FTP
Using ftp, you can easily connect to a server and nearly all the commands available on your ftp client are available to you there. Setting up a connection is rather simple:

// set up connection
$conn = ftp_connect($server);

// login with username and password
$login = ftp_login($conn, $username, $password);;

Using php's ftp commands to upload files is pretty much obsolete, I would only use it if you needed some kind of special permissions, but if you still want to do it look here: http://php.net/ftp_put. There are some pretty useful functions that you might need it for though. You can chmod files (this is also available with the chmod command, but will probably fail due to lack of permissions) with ftp you can login and simply chmod files using the ftp_chmod command:

ftp_chmod($conn, 0777, "example.txt");

This will attempt to chmod the file to 777 (read, write, executable by all). Another useful function is ftp_get. You can connect to a certain ftp server with a php script using the format shown above and then download files to the system with the ftp_get command:

ftp_get($conn, $local_file, $server_file, FTP_BINARY);

$conn is the ftp connection id, $local_file is the name the file will be once it is dowloaded, $server_file is the name of the file on the location you are downloading from and FTP_BINARY is the mode FTP is in (binary is for image type files and ascii (FTP_ASCII) is for text type files).

Ok, now I will show you how to login to a certain server, begin downloading a file and display the progress of the download. (Of course it won't be live since php is all executes server side), but by refreshing, the progress will be viewable. This will make use of some more of the ftp functions, they all start out: ftp_nb_*. We will use these functions because they return the state of the download. Also, these functions allow "asynchronous" processing as the php manual calls it, meaning other operations can be executed while the file is downloading. The other difference of these functions is that they work with open files so we will use the fopen function too. First we will connect to the server, open the file to read from and start downloading a file:

php:
$conn = ftp_connect($server);
$login = ftp_login($conn, $username, $password);

// open some file for writing
$file = 'index.php';
$fp = fopen($file, 'w');

// start the download
$ret = ftp_nb_fget($conn_id, $fp, $download_file, FTP_BINARY);


Now we will need to process the download progress and display it. We will use a while loop which will tell us the state of the download, knowing the state, we can use the filesize function to determine the current amount downloaded.

php:
$s = ftp_size($conn, $download_file); // need to find the size of the file we will download first


while ($ret == FTP_MOREDATA) // do while there is more file to be downloaded
{
     clearstatcache(); // this is important because the results of filesize() are cached
     $d = filesize($file); // find the current size of the file being downloaded

     if ( $d > 0 )
     {
          // calculate percentage
          $i = ($d/$s)*100;
          //print the current percentage downloaded
          print("$i% downloaded");
     }

     // Continue downloading...
     $ret = ftp_nb_continue($conn);
}



Uploading via POST
Thats pretty much all. Now onto handling uploads via a form post. Basically if you can put a file field on an html form you can handle file uploads. There are only a few things that need to be done to get an uploaded file. First you must make sure you include enctype='multipart/form-data' in your form tag. Second create a form with a file field (<input type="file" name="inputFile">) and a submit button. When a user hits Submit, their file will be uploaded to a temporary location, to get the file to a permanent location it is only a matter of moving the uploaded file:

php:
<?php
/* check to make sure the file was uploaded correctly */
if (is_uploaded_file($_FILES["inputFile"]["tmp_name"]))
{
    
/* move the file to a permanent location */
    
move_uploaded_file($_FILES["inputFile"]["tmp_name"], "/path/to/upload/" . $_FILES["inputFile"]["name"]);
}
?>


If you have register_globals turned on, $_FILES will contain several things:

$_FILES["inputFile"]["name"]
      contains the name of the file as it is on the uploader's machine
$_FILES["inputFile"]["tmp_name"]
      contains the temporary name of the file uploaded on the server
$_FILES["inputFile"]["type"]
      contains the MIME type of the file, you could use this to check if a file is an image for example one would be: "image/gif"
$_FILES["inputFile"]["size"]
      contains the size of the file in bytes
$_FILES["inputFile"]["error"]
      contains the error code of the file, if you'd like to read up on this it is available at       http://us2.php.net/manual/en/features.file-upload.errors.php

Thats all for now. Have fun with files :).




Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.