View Single Post
  #2 (permalink)  
Old Jun 27th, 2006, 09:53
mr-ecommerce mr-ecommerce is offline
Junior Member
Join Date: Jun 2006
Location: Sheffield, South Yorkshire, UK
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Putting files in a directory

PHP: Select all


<?php

        
// FILE:  generic/uploadfile.php
        // Desc: Generic Include for file Uploads
        // Date: 12 Dec 2005
        // written by David Wales of s10sys.com

        // this is the holder of your uploaded file.
        
global $_FILES;

        
// set this if your file is large so you don't time out on upload
        
set_time_limit(240);
        
        
// this is a linux example of the path variable.
        
$PATH '/your/destination/directory/';

        
// this would be windows...?
        // $PATH = 'c:\your\destination\directory\';

        // upload the file to a data directory.
        
if (is_uploaded_file($_FILES['file']['tmp_name']))
        {
            if(!
move_uploaded_file($_FILES['file']['tmp_name'], $PATH.$_FILES['file']['name']))
            {
                die( 
'file upload is complete...'."<br>".'Failed to move uploaded file... ScriptStopped!' );
            }
        }
        else
        {
            die( 
"<br>".'Security - possible file upload attack... ScriptStopped!' );
        }

        
// now drop out back to original include point.
?>
I always use the same bit of code to upload my files, this file above is my upload script and it works every time. Anything else you want to do with your upload should be part of a seperate script.

For example, save the above file as uploadfile.php and create a new file called myfooupload.html and make it look like the example below, point your form action at the myfooupload.html file and see what happens.

PHP: Select all


<?php

   
// test uploading of a file.
   
   
if(!isset($_FILE))
   {
       
// show your upload form.
       
include_once('./myuploadform.html');
   }
   else
   {
       
// include the generic upload script.
       
include_once('./uploadfile.php');

       
// include a seperate file that now does stuff to the uploaded file.
       
include_once('./modifyfooupload.php');
   }

?>
By doing things in this manner you can check that the uploadfile.php has been successfull and be happy that it will work for all uploads, then all you need worry about is what your modifyfooupload.php file does to the uploaded file.

On a side note it is worth changing your fopen statments don't make any sense at all and don't actually do anything other than maybe mess the files up? Remove these lines from your code.

PHP: Select all


$fp 
fopen($upfile"r");
$fp fopen($upfile"w");
fwrite($fp);
fclose($fp); 
Also when using an fopen you should always check the return as below:-

PHP: Select all

<?php

$fp 
fopen($filename,'r');
if(!
is_resource($fp))
{
    die(
'could not open/create '.$filename.' for writting/reading');
}

$filecontents '';

while(
$buff fread($fp,1024))
{
   
$filecontents.= $buff;
}

fclose($fp);

echo 
'<pre>'.$filecontents.'</pre>';

?>
Hope this helps
Reply With Quote