Upload_File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Upload_File

Post by negblo »

Im not sure if php has an upload file command set, or if I have to do it another way.. either way it goes, i don't know how it's done... any suggestions ????


Thanx-a-mil

negblo
:?:
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post by corlando »

User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

look for snipplets of code on various php sites, try the Phuse searchto find them.
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

i read somewhere that php was capable of doing what i need... all i need is the code to tell my button that it is supposed to let me select a file from my harddrive (this will be my browse button)

and then i need to know what I am supposed to tell my upload button so that it uploads the file that i have selected...

thank you for the link, but i tried to search through the phuse search thingy and couldnt make heads from tails there... i got lost and was unable to find anything remotley like what i am looking for... any help will be greatly appriciated...


Thanx-a-mil

negblo
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

okay heres what i got....


<form enctype='multipart/form-data' action='http://www.mydomain.com/images/' method='post'>
<input type='hidden' name='MAX_FILE_SIZE' value='30000'>
<input name='userfile' type='file'>
<input type='submit' value='Send'>
</form>

now i go to my page, select the Browse button and insert an image, then i click on the send button... the page acts like it did something, yet no pictures were placed in the correct folder...

i read a little bit about the move_uploaded_file command but was unsuccessful at making it work... (possible dew to the fact that i am way to new at this)...

am i supposed to have a second page (something.php) to send this image to and then put the code for checking wether or not the image was uploaded successfully and then send it to the correct folder... or am i so lost that there is no hope for me now!!!??!!!!

i appriciate your patience with me, and for your replies..


Thanx-a-mil

negblo
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post by corlando »

this is a class that i use for uploading files. using it is very simple. your form action tag should point to the file with the php script that processes the upload, NOT the directory where you want to upload to.

Code: Select all

<?php 

/* DESCRIPTION 

FILE_UPLOADER( long max_size , string accepted_types , int save_mode , string upload_path , array post_file ) 

max_size      maximum file size allowed. 2MB is default 
do not use quotes 

accepted_types   string of mime-types allowed (ie: image/jpeg,image/gif) 


save_mode   FILE_UPLOADER_OVERWRITE 
no quotes   overwrite feil if a file already exists in upload_path 

      FILE_UPLOADER_INCREMENT 
      if a file by the same name already exists in the 
      upload_path increment the file using _# 

      FILE_UPLOADER_PROTECT 
      do nothing if a file by the same name already exists 

upload_path   destination of file (relative paths only) 

post_file      array file field name (ie $userfile) 
*/ 

define("FILE_UPLOADER_OVERWRITE", 1, 1); 
define("FILE_UPLOADER_INCREMENT", 2, 1); 
define("FILE_UPLOADER_PROTECT", 3, 1); 

class FILE_UPLOADER { 
   var $accepted_types; 
   var $save_mode;    
   var $max_filesize;    
   var $errors; 
   var $upload_path;    
   var $new_filename; 
   var $tmp_filename;        
   var $file_extension; 
   var $new_file_path; 
   var $orginal_filename;    
   var $fileArray; 

   function set_max_filesize($max_size)    {   $this->max_filesize = $max_size; } 
   function set_accepted($accepted_types)    {   $this->accepted_types = $accepted_types; } 
   function set_savemode($save_mode)        {   $this->save_mode = $save_mode; } 
   function set_path($upload_path)       {   $this->upload_path = $upload_path; } 

   function FILE_UPLOADER ($max_size = 2097152, $accepted_types = "", $save_mode = UPLOADER_PROTECT, $upload_path = "", $file = "") { 
      $this->upload_path = $upload_path; 
      $this->save_mode = $save_mode; 
      $this->accepted_types = $accepted_types; 
      $this->max_filesize = $max_size; 
      $this->fileArray = $file;         
   } 

   function upload ($file = "") { 
      if ($file == "" ) { 
         $file = $this->fileArray; 
      } 
        
      if ($this->fileArray == "" ) { 
         $this->errors = 'No file was uploaded.'; 
         return false; 
      } 
	  
	  if ($file['size'] == 0) {
	  	$this->errors = 'File is larger than allowed MAX_FILE_SIZE';
		return false;
	  }
        
      $this->temp_filename = $file['tmp_name']; 
      $this->original_filename = $file['name'];        

      if ($this->accepted_types != "" && strstr($this->accepted_types, $file['type'] ) === false ) { 
         $this->errors = 'This file type ''' .$file['type']. ''' is not allowed. Allowed types are ' .$this->accepted_types. '.'; 
         return false; 
      } 
      
	  
	  if ( $file['size'] > $this->max_filesize) { 
         $this->errors = 'Maximum file size of exceeded. File may be no larger than ' .($this->max_filesize / 1024). ' KB.'; 
         return false; 
      } 

      $this->new_filename = ereg_replace("[^a-z0-9._]","", ereg_replace(" ", "_", 
                        ereg_replace("%20","_", strtolower($this->original_filename) )));        

      $pos = strrpos($this->new_filename, ".");        
      $this->file_extension = substr( $this->new_filename, $pos, strlen($this->new_filename) - $pos); 
      $this->new_filename = substr($this->new_filename, 0 , $pos); 

      $copy = ""; 

      switch($this->save_mode) { 
         case 1: // overwrite mode 
            $this->new_file_path = $this->upload_path . $this->new_filename . $this->file_extension;              
            break; 

         case 2: // create new file with incremental extention          
            $n = 1;              
              
            while( file_exists($this->upload_path . $this->new_filename . $copy . $this->file_extension) ) { 
               $copy = "_" . $n++;                
            } 
            $this->new_file_path = $this->upload_path . $this->new_filename . $copy . $this->file_extension;              
            break; 

         default: // do nothing if exists, highest protection 
            if( file_exists($this->upload_path . $this->new_filename . $this->file_extension) ) { 
               $this->errors = $this->errors . "File "" . $this->new_filename . $this->file_extension . "" already exists"; 
               return false; 
            } 
            else { 
               $this->new_file_path = $this->upload_path . $this->new_filename . $this->file_extension;                
            } 
            break; 
      } 
                
      copy( $this->temp_filename, $this->new_file_path ); 
        
      if (! file_exists( $this->new_file_path ) ) { 
         $this->errors =  "Could not move file from temp directory to '". $this->upload_path ."'"; 
         return false; 
      }          
        
      return $this->new_filename . $copy . $this->file_extension; 
   }    
} 
?> 
<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 
<form enctype='multipart/form-data' action='' method='post'> 
   <input type='hidden' name='MAX_FILE_SIZE' value='300000'> 
   <input name='userfile' type='file'> 
   <input type='submit' value='Send'> 
</form> 

<?php 
// this code processes the uploaded file 

if ( isset($_FILES['userfile']) ) { 

   $uploader = new FILE_UPLOADER( $_POST['MAX_FILE_SIZE'], '', FILE_UPLOADER_INCREMENT, 'images/', $_FILES['userfile']); 

   if (! ($newfilename = $uploader->upload() ) ) { 
      echo 'ERROR: ' .$uploader->errors; 
   } 
   else {
      echo 'Your file has been uploaded to ' .$uploader->upload_path . $newfilename; 
   } 
} 
?> 

</body> 
</html>
Last edited by corlando on Wed Jun 18, 2003 1:24 pm, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There is an example here:
http://php.net/manual/en/features.file-upload.php

which was a page you were given the link to originally - it will hopefully show you why what you were trying to do would not work.

Mac
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

thank you max, i thought i saw the part where it said 'should point to a php file' but wasn't sure were i had seen it in the first place... thank you!!! :wink:


corlando:: props to you dude!!! :D I cannot thank you enough!!

however, there is but one more thing, and that is to set a username and password for the images directory so that i can enable write permission to only those specified to write to this directory... any clues as to how i can do this with just the uploading only?? or do i have to do it on the actual admin page itself???

thank you to everyone for you help!!!


Thanx-a-mil

negblo
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

wait theres something else to...

1.) I need a login page... can someone point me to lessons on usernames and passwords?

2.) I need my login page to disappear after I login...(ie, i will be opening a new window and i don't want the login widow to be open in the background.

3.) this one is off the wall -- but is php capable of making a browser 'window-less' ?? i used to have a java code for 'chrome' which allowed the new window to open with only a minimize and exit options on the browser window, but i lost it...



You all have been so patient and so much more helpful, i greatly appriciate it all!!! cudos to everyone.. :D



Thanx-a-mil

negblo
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Re: Question 3: I believe they call them chromeless windows, and you can find them at http://www.microbians.com
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

or if i new how to name a page, that work just as well..

right now i am fixing to make the login page, and then assign a target value to the admin page, this way when i get done editing a department from another page, it can post back to the admin page...

right i get errors on my edit page from the 'header' tag that i placed in it to redirect me back to the admin page...

any suggestions...

heres the header ---


If ($DeptResult){
header("Location: http://www.myplace.com/admin/admin.php? did=".$Did."&cid=".$Cid."&pid=0&olddid=".$Did."&oldcid=".$Cid."&oldpid=0");
}



and here is the error that i get at the top of the page after hitting the submit button ---



Update Department SET Department = 'Dept2bbbb' WHERE DepartmentID = 2

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\Commercial\myplace\admin\dept.php:38 ) in C:\Inetpub\Commercial\myplace\admin\dept.php on line 41


and line 41 is the header above...any suggestions???


Thanx-a-mil

negblo
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post by corlando »

you have to make sure that nothing is being outputed to the client before your header statement.

your problem lies in this file C:\Inetpub\Commercial\myplace\admin\dept.php on line 38
i'm guessing your displaying your query string for testing and that's probably what is screwing you up. comment out that line.

No output can be sent to browser before a header call. that means no html markup, echo statements or print statements.

and put 'exit();' after every header redirects, so you stop php from parsing the rest of the document.

Code: Select all

<?php
If ($DeptResult){ 
  header("Location: http://www.myplace.com/admin/admin.php? did=".$Did."&cid=".$Cid."&pid=0&olddid=".$Did."&oldcid=".$Cid."&oldpid=0");

  exit(); // put this here
} 

?>
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post by corlando »

negblo wrote: 2.) I need my login page to disappear after I login...(ie, i will be opening a new window and i don't want the login widow to be open in the background.

Code: Select all

<script>window.close();</script>
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

corlando :: again you are the man... i think its finally done(minus the cosmic makeover)... where do i actually place the close window script???


releasedj :: question, how do i get the chromeless window to actually open in a window the size i specify???? i changed to width and height in the java scripting placed on the page in the <HEAD> tag, but it ignores my request and opens full screen... doesn't make much since to me... if you, or anyone, could help me with that...paleeeasse...


if you would like to see what all have done with all of your blessings on being patient with me, just send me an email

scorpion@lcisp.com



Thanx-a-mil

negblo
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Sorry negblo, I haven't used it before. Don't they have a userguide?
Post Reply