T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 61
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 200
  Visitor Online : 3
  Today Visit: 216
  Total Visitor : 229493
  Most Online : 31
::: Sponsored Links :::

PR 4 For This Webpage
Home Today :

Simple Upload Page


with this code, you can create an upload page that combines HTML and PHP easily. this Source code has been writen form T4VN Team. so, if you want to develop it, please contact us. Thanks for your interested in this code

Step 1 : Create one page with HTML content

PHP Tutorials :

<html> 
<head> 
<title>Upload</title> 
</head> 
<body> 
<h1>Upload</h1> 
<form enctype="multipart/form-data" action="upload.php" method="post"> 
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> File: 
<input name="userfile" type="file"> 
<input type="submit" value="Upload"> 
</form> 
</body> 
</html> 



Step 2 : Create file upload.php with content :

PHP Tutorials :

<?php 
// $userfile is where file went on webserver 
$userfile $HTTP_POST_FILES['userfile']['tmp_name']; 
// $userfile_name is original file name 
$userfile_name $HTTP_POST_FILES['userfile']['name']; 
// $userfile_size is size in bytes 
$userfile_size $HTTP_POST_FILES['userfile']['size']; 
// $userfile_type is mime type e.g. image/gif 
$userfile_type $HTTP_POST_FILES['userfile']['type']; 
// $userfile_error is any error encountered 
$userfile_error $HTTP_POST_FILES['userfile']['error']; 

// userfile_error was introduced at PHP 4.2.0 
// use this code with newer versions 

if ($userfile_error 0) { 
echo 
'Problem: '
switch (
$userfile_error
{ case 
1
echo 
'File exceeded upload_max_filesize'
break; 
case 
2
echo 
'File exceeded max_file_size'
break; 
case 
3
echo 
'File only partially uploaded'
break; 
case 
4
echo 
'No file uploaded'
break; 

exit; 


// put the file where we'd like it 
$upfile '/uploads/'.$userfile_name

// is_uploaded_file and move_uploaded_file 
if (is_uploaded_file($userfile)) 

if (!
move_uploaded_file($userfile$upfile)) 

echo 
'Problem: Could not move file to destination directory'
exit; 

} else { 
echo 
'Problem: Possible file upload attack. Filename: '.$userfile_name
exit; 

echo 
'File uploaded successfully<br /><br />'

// show what was uploaded 
echo 'Preview of uploaded file contents:<br /><hr />'
echo 
$contents
echo 
'<br /><hr />'
?>


Note :line: $upfile = '/uploads/'.$userfile_name; Folder of files have been stored, you can change it depending on your ideas


Another form of Upload file, you can choose file that you want to upload optionally

Create file upload.php with contents :

PHP Tutorials :

<?php 
  
//Define some variables 
      
$dir "path/where/you/want/to/upload/files/"//B&#7841;n nên thay đ&#7893;i đư&#7901;ng d&#7851;n cho phù h&#7907;p 
    //Ki&#7873;u file, Gif, jpeg, zip ::b&#7841;n có th&#7875; s&#7917;a đ&#7893;i n&#7871;u thích 
      
$types = array("image/gif","image/pjpeg","application/x-zip-compressed"); 
     
//Check to determine if the submit button has been pressed 
    
if(isset($_POST['submit'])){ 

//Shorten Variables 
     
$tmp_name $_FILES['upload']['tmp_name']; 
     
$new_name $_FILES['upload']['name']; 

//Check MIME Type 
    
if (in_array($_FILES['upload']['type'], $types)){ 
                    
         
//Move file from tmp dir to new location 

        
move_uploaded_file($tmp_name,$dir $new_name); 
           
        echo 
"{$_FILES['upload']['name']} was uploaded sucessfully!";                                         
           
    }else{ 
                
    
//Print Error Message 

     
echo "<small>File <strong><em>{$_FILES['upload']['name']}</em></strong> Was Not Uploaded!</small><br />"
      
    
//Debug 
   
$name =  $_FILES['upload']['name']; 
   
$type =    $_FILES['upload']['type']; 
   
$size =    $_FILES['upload']['size']; 
   
$tmp =     $_FILES['upload']['name']; 
     
   echo 
"Name: $name<br/ >Type: $type<br />Size: $size<br />Tmp: $tmp"
              
    } 
       
    }       
       
else{       
       
    echo 
'Could Not Upload Files'
       

        
?> 
           
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> 
       
      <fieldset> 
              <legend>Upload Files</legend> 
                                           
          <input type="file" name="upload" /> 
</fieldset> 
<input type="submit" name="submit" value="Upload Files" /> 
</form>



You are done.Good luck

Author By T4VN Team

Other Tutorials
Create Simple ShoutBox
Sending Simple Text Emails
Simple Hit Counter
Building a Counter

::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007