Make your own free website on Tripod.com

you wish...
Uploading Files
August 8th, 2001
by Martin Raymond


the.php.script

Honestly, you don't need PHP to upload a file using a form, BUT you can't control where it's saving the files. So can you guess what the PHP does? It moves the file to where you want it on the server.

Your code will basically look something like this: (example file upload.php)

<?

    
// name of the directory that your uploads will be kept in
    
$dirname "files";

    
// the path that the uploads directory will be placed in
    // the full path of something like: http://www.codephobia.com/test/
    
define(PATH"/usr/local/etc/httpd/htdocs/test/");
    
    
// gets just the file name and extension from the file input
    // in the form
    
$filename $myfile_name;
    
    
// check if they actually placed something in the file input
    
if ($filename != "")
    {
        
// check if the directory exists
        // if it doesnt exist, make the directory
        
if (!$dir = @opendir(PATH.$dirname))
            
// make the directory (sets chmod to 700)
            
mkdir(PATH.$dirname0700) or die("<b>Error:</b> could not make directory.");
            
        
// copy the file from the temporary upload position to where you want it
        
copy($myfilePATH.$dirname."/".$filename) or die("<b>Error:</b> could not copy file.");
        
        
// delete the temporary uploaded file
        
unlink($myfile) or die("<b>Error:</b> could not delete uploaded file.");
        
        
// tell them it worked
        
echo "File uploaded successfully.";
    }
    else
    {
        
// tell them to browse for a file
        
echo "You must select a file to upload.";
    }

?>


Let's go over this a little more closely. The following code, simply put, will be the name of the folder the file ends up in. This could be a users profile folder or where you store images from a news script. This folder will be '/files/'. You can change this to whatever you like. We'll call this the destination folder.

    // name of the directory that your uploads will be kept in
    
$dirname "files";


Next, we have the full path to where the destination folder is to be created. We'll call this the destination path. Be sure to change this so that is works with your server. You should be able to find a path in any FTP program.

    // the path that the uploads directory will be placed in
    // the full path of something like: http://www.codephobia.com/test/
    
define(PATH"/usr/local/etc/httpd/htdocs/test/");

That takes care of where we are putting the file. Now we need to get some info on the file itself, so now we get the name and file extension.

    // gets just the file name and extension from the file input
    // in the form
    
$filename $myfile_name;

If you uploaded the file 'c:\images\big_image.jpg', $filename would be 'big_image.jpg'. When we upload this file, it puts it in a temporary directory on the server. When we copy this temp file to the desired destination folder, we also need to give the file a name. In this case we'll just keep the original file name, so we need to get it. $myfile was the file input from the form in the html. The PHP generates a variable automatically, and $myfile_name now returns the name of that file.


Of course we don't want to try to move a file if they didn't select one in the form. So we use this code to check if they did or not.

    // check if they actually placed something in the file input
    
if ($filename != "")
    {
        // selected a file
        ...
        ...
    }
    else
    {
        // tell them to browse for a file
        
echo "You must select a file to upload.";
    }


Of course, the code above is missing the code in the IF-ELSE statment. If the file did infact exist, it would do the first group of commands. This is where we would create the destination folder, copy the temp file to the destination folder, delete the temp file, and tell the user that it all worked great. It should look something like this:

You first make sure the destination folder exists:

        // check if the directory exists
        // if it doesnt exist, make the directory
        
if (!$dir = @opendir(PATH.$dirname))
            
// make the directory (sets chmod to 700)
            
mkdir(PATH.$dirname0700) or die("<b>Error:</b> could not make directory.");

The script checks to see if your destination folder has aready been created. If it hasn't, it makes it. If it can't create the folder, it outputs an error. If this happens you probably just need to chmod the destination path so you can create the folder. The new folder will be chmod'd to 700.

Now that we have somewhere to put the file, let's copy it there.

        // copy the file from the temporary upload position to where you want it
        
copy($myfilePATH.$dirname."/".$filename) or die("<b>Error:</b> could not copy file.");

Simple. Just copies the file from the temp spot to your destination folder. If it doesn't work, you'll get a nice error telling you.

If everything is going good so far, you wont need that ugly temp file anymore. Let's delete it!

        // delete the temporary uploaded file
        
unlink($myfile) or die("<b>Error:</b> could not delete uploaded file.");

This 'should' work, but if you don't have permission to delete it, you'll get an error. If everything worked great, you get the following message, and the script is done.

        // tell them it worked
        
echo "File uploaded successfully.";




PHP and MySQL Web Development

Beginning PHP4

A Programmer's Introduction to PHP 4.0

In Association with Amazon.com


stats