- Home
- Registration
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Miscellaneous
- Affordable Hosting
- PHP Code Examples
- Learn PHP
- Virtual Server
- Apache:OS 10048
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- PHP and MS Access
- Authentication
- PHP App Example
- Display Image PHP
- Image Collection
- Block Spam Post
- Upload File PHP
- $_REQUEST,$_SESSION
- Site map
- Advertise here
- LaptopForLess
- Geeks´ Stuff
- Nerds´ Stuff
- Remote Control Toys
Upload File PHP code example
By Sergey Skudaev
![]()
![]()
![]()
![]()
![]()
By Sergey Skudaev
First time, when I tried to write PHP code for uploading file from my PC to my Web Server, I spent significant amount of time. You can take this PHP code example and modify it for your needs.
To prevent overwriting files uploaded earlier I use current date and time for a new file names. The PHP code example comprised of two PHP files. The first PHP file represent a form, that allows a user to browse local file system, select a file for uploading and submit the form. . I named it "upload_file.php". The second PHP file is an action template that executes uploading the selected file. I named it "act_upload_file.php".
On my Windows PC, I created a folder "uploadfile" inside the htdocs folder and place both files inside uploadfile folder. Besides, I created "upload" folder inside the "uploadfile" folder.
On the hosting web server you have to make sure that user has write permission for "upload" folder. If you use this code example on your local Windows PC you don't have to care about it.
CSS layout for PC and mobile device. $2.95
By S.Skudaev, (an eBay Excellent seller)
Contents:
1. How to install Pocket PC emulator on your Windows PC
2. Three columns layout CSS styles for regular PC browsers
3. One column layout CSS styles for mobile device

Pay via PayPal. You should not be concern about my web site security, because when you click the link, you will be redirected to PayPal site and it can be trusted. You will have your financial transaction with PayPal, not with me. So, please do not afraid to buy from my site. Credit cards are accepted. After payment is done, on the PayPal thank you page
Three columns layout template created in such a way that the middle column content coming first in the HTML code. This way, Google spider, while visiting your page, will see your main content first. Then the right and the left columns are placed.
In mobile device, web page has only one column. The same web page is used for regular browsers and a mobile device. It is very important because if you create two pages with the same content, Google will punish you for duplicate content.My tutorials are different from all other tutorials and books because I do not skip initial steps that a reader needed to start learning. Some times, you buy a book and start to read it. You understand everything from the point the author starts, but you do not know how to get to that starting point. I never miss the beginning!
Listing 1. "upload_file.php"
<?php
print('<html>');
print('<head><title>');
print('Upload File Example</title>');
print("</head>");
print('<body>');
print('<form name=myform ENCTYPE="multipart/form-data" method="post" action="act_upload_file.php">');
print('<table class="aaa" align="center" width=80% border=0>');
print('<tr><th align="center">Upload File Exaple</th></tr>');
print('<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">');
print('<tr><td align="center">Upload File: <INPUT
NAME="uploaded_file" TYPE="file"></td></tr>');
print('<tr><td> </td></tr>');
print('<tr><td align="center" ><input type=submit name=submit value="Upload"></td></tr>');
print('</table></form>');
print('<body></html>');
?>
Listing 2. "act_upload_file.php"
<?php
$uploaded=0;
$ext="";
//generate unique file name using time:
$newfilename= md5(rand() * time());
//do we have a file?
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
$filename =strtolower(basename($_FILES['uploaded_file']['name']));
$ext = substr($filename, strrpos($filename, '.') + 1);
if ((($ext == "jpg")||($ext == "JPG")) && ($_FILES["uploaded_file"]["size"] < 500000)&&(($_FILES["uploaded_file"]["type"] == "image/jpeg")||($_FILES["uploaded_file"]["type"] == "image/pjpeg")))
{
//Determine the path to which we want to save this file
$ext=".".$ext;
$newname = dirname(__FILE__).'/upload/'.$newfilename.$ext;
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)))
{
echo "File uploaded successfully!";
$uploaded=1;
}
else
{
echo "Error:!";
print('<p><a href="upload_file.php?">Back</a></p>');
}
} else {
echo "Error: Only .jpg files is allowed less than 500Kb";
print('<p><a href="upload_file.php">Back</a></p>');
}
} else {
echo "Error! File is not uploaded!";
print('<p><a href="upload_file.php">Back</a></p>');
}
?>
If you want to limit image width and height use getimagesize function:
list($width, $height, $type, $attr) =
getimagesize($_FILES['uploaded_file']['tmp_name']);
Try to modify the PHP code example. Provide user an input field for a new file name. Think, how to check if this file already exists on the server and alert the user.
Did you find information useful? Send to your friend a link to this page
Please rate the tutorial
| Comments |
|---|
Web programming Tips