Sunday, April 25, 2010

PHP Upload Single File

This is a simple PHP uploading file tutorial. You can upload files on your server through this script.


Overview


In this tutorial create 2 files
1. upload.php
2. upload_ac.php


Step
1. Create file upload.php.
2. Create file upload_ac.php.
3. Create folder "upload" for store uploaded files.
4. CHMOD your upload folder to "777" by using your ftp software(change permission).


Step1:


Create file upload.php



|---|--------------------- code --------------------|---|

{code type=codetype}
<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Single File Upload </strong></td>
</tr>
<tr>
<td>Select file
<input name="ufile" type="file" id="ufile" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>


{/code}


Step2:


Create file upload_ac.php


|---|--------------------- code --------------------|---|
{code type=codetype}

<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";


//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
}
else
{
echo "Error";
}
}
?>


{/code}

Step3:


CHMOD upload folder to 777 (change permission)


This step, do it when you upload to real server. This example, I use CuteFTP, right click at upload folder > FTP Commands > CHMOD


10 comments:

  1. Excellent tutorial, I like the simple ones.

    ReplyDelete
  2. Great useful tip. Thanks a lot for sharing

    ReplyDelete
  3. ultrasound technicianMay 2, 2010 at 2:48 PM

    Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

    ReplyDelete
  4. financial aid for collegeJune 3, 2010 at 12:24 PM

    Great information! I’ve been looking for something like this for a while now. Thanks!

    ReplyDelete
  5. found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later

    ReplyDelete
  6. limited liability companyDecember 22, 2010 at 3:07 AM

    Nice site, nice and easy on the eyes and great content too.

    ReplyDelete