Taking Web Applications Offline, to the Desktop, and beyond

Ryan Stewart of Adobe and I got to give a joint talk this morning that covered Adobe AIR, Gears, and how you can build offline and desktop applications right now. Obviously, Ryan gave an overview of AIR, and I did the same for Gears. We also discus...

Simple Upload Page Print E-mail
User Rating: / 12
PoorBest 
With this code, you can create an upload page that combines HTML and PHP easily. This Source code has been writen by T4VN Team. So, if you want to develop it, please contact them. Step 1: Create one page with HTML content

<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  
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES[&#39;userfile&#39;][&#39;tmp_name&#39;];
// $userfile_name is original file name  
$userfile_name = $HTTP_POST_FILES[&#39;userfile&#39;][&#39;name&#39;];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES[&#39;userfile&#39;][&#39;size&#39;];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES[&#39;userfile&#39;][&#39;type&#39;];
// $userfile_error is any error encountered  
$userfile_error = $HTTP_POST_FILES[&#39;userfile&#39;][&#39;error&#39;];

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

if ($userfile_error > 0) {
echo &#39;Problem: &#39;;
switch ($userfile_error)
{ case 1:
echo &#39;File exceeded upload_max_filesize&#39;;
break;  
case 2:
echo &#39;File exceeded max_file_size&#39;;
break;
case 3:
echo &#39;File only partially uploaded&#39;;
break;
case 4:
echo &#39;No file uploaded&#39;;
break;
}
exit;  
}

// put the file where we&#39;d like it
$upfile = &#39;/uploads/&#39;.$userfile_name;

// is_uploaded_file and move_uploaded_file
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo &#39;Problem: Could not move file to destination directory&#39;;
exit;  
}
} else {
echo &#39;Problem: Possible file upload attack. Filename: &#39;.$userfile_name;
exit;
}
echo &#39;File uploaded successfully&#39;;

// show what was uploaded
echo &#39;Preview of uploaded file contents:<hr>&#39;;
echo $contents;
echo &#39;<hr>&#39;;  
<br>?>

 

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  
//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[&#39;submit&#39;])){

//Shorten Variables
$tmp_name = $_FILES[&#39;upload&#39;][&#39;tmp_name&#39;];
$new_name = $_FILES[&#39;upload&#39;][&#39;name&#39;];

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

move_uploaded_file($tmp_name,$dir . $new_name);

echo "{$_FILES[&#39;upload&#39;][&#39;name&#39;]} was uploaded sucessfully!";

}else{

    //Print Error Message  

echo "<small>File <strong><em>{$_FILES[&#39;upload&#39;][&#39;name&#39;]}</em></strong> Was Not Uploaded!</small><br />";

//Debug
  $name =  $_FILES[&#39;upload&#39;][&#39;name&#39;];  
$type = $_FILES[&#39;upload&#39;][&#39;type&#39;];
$size = $_FILES[&#39;upload&#39;][&#39;size&#39;];
   $tmp =     $_FILES[&#39;upload&#39;][&#39;name&#39;];  

echo "Name: $name<br>Type: $type<br>Size: $size<br>Tmp: $tmp";

}

}
        
else{

echo &#39;Could Not Upload Files&#39;;

}
?>

<form action="<?php echo $_SERVER[&#39;PHP_SELF&#39;]; ?>" 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

About Author :
T4VN.NET is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support,coupon code for hosting,download free template.
Visit : http://www.t4vn.net you can get more information with PHP Code,PHP Example,Download free script,.....

 

 
< Prev   Next >