SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP File Function

PHP file upload Script :: online picture upload

File upload is very common requirement of many web sites. We may require to upload pictures online to websites. Image upload is very common requirement and many sites are using this to allow members or visitors to upload files. Picture rating, picture gallery site uses this feature to allow multiple file uploads. What we need to allow file upload or online picture upload to a site? This file upload script explained in a step by step way to accomplish this online uploading to a web server. We can even create thumbnail images of the uploaded image file by php. The main requirement is to allow the members to browse the local computer and point to the file required for uploading.
  • Check the file if it is allowed for upload or not. We can check for file size, file extension, file name etc.
  • Copy the file to server.
  • Place the file in required directory and then give necessary file permission.
 Before starting we must add file permission to the directory where we plan to store the files after uploading it. Let us give the directory name upload and give write permission to it. If it is a Window server nothing is required and by default window gives all permissions. For Linux and Uinx we have to give write (Chmod) permission to allow uploaded files to store.

If you are uploading large files then read about maximum file execution time allowed and its adjuestments.

 Let us add the html code to show the browse button for the visitors to point to file required for uploading. Here is the code

<FORM ENCTYPE="multipart/form-data" ACTION="uploadck.php" METHOD=POST>
Upload this file: <INPUT NAME="file_up" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>

This html code will display a text box with file upload button. The form tag is bit different than the normal form tag used ( see the encrypte =).

In PHP 5 and above as the global variable is kept as off we can't access the uploaded file and related information by the way we handled in PHP 4. All uploaded file and its connected information will be available in an array.

$_FILES[file_up];

So to get the file size we have to use

$_FILES[file_up][size]

Note that in the form we have used the input filed name as file_up. Same way all other information can be collected.

$_FILES[file_up][type]
$_FILES[file_up][tmp_name]
$_FILES[file_up][error]

Maximum allowed file upload size.

In PHP installation there is a file size limit for uploading files. You can check your php info to know about this upper limit. You can also run this code to read the maximum file size setting of your server.

<?Php
echo " Maximum allowed file size is : ".ini_get('upload_max_filesize');
?>

From this you can add a check in the script and display message to the user saying about the error. Here is the code for that.

if ($_FILES[file_up][size]>250000){
$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";
}

Here is the full code to handle file upload.

<?Php
$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
echo $_FILES[file_up][name];
if ($_FILES[file_up][size]>250000){$msg=$msg."Your uploaded file size is more than 250KB
so please reduce the file size and then upload.<BR>";
$file_upload="false";}

if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/gif"))
{$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";}

$file_name=$_FILES[file_up][name];
$add="upload/$file_name"; // the path with the file name where the file will be stored

if($file_upload=="true"){

if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
// do your coding here to give a thanks message or any other thing.
}else{echo "Failed to upload file Contact Site admin to fix the problem";}

}else{
echo $msg;
} ?>

Upload Multiple file using single form

Thumbnail Image from uploaded picture

Upload and create a profile picture of a member

Number of User Comments : 14


Google+
shabeer02-09-2009
how di move the uploaded images from the server to my file in c:drive
smo03-09-2009
After uploading you can display the hyper link by adding the file name. Once the link is clicked then a new window will ask you to save or open the file. That time you browse and show the location of C drive.
Robert30-09-2009
I understand the pointing to the php file in the particular directory...but when it is online it opens a window with just the code in it?? help
lana29-10-2009
i have added to my site and it works excellently except for large files... it seems to take forever... any tips - or is php inefficent at large scale images? 2ndly is there a way to provide the error messages underneath the form itself instead of on a new page? thanks.
sandeep08-02-2010
thnk the discription above is very good nd easily understandble i learn very well thanks again
Jayrajsinh Radadiya20-07-2010
this is very useful for the upload image
mohamed ashaf23-02-2011
thks the script above is very useful for me.
none07-10-2011
how to i make it custom extension, for example the file can only be an mu1 file?
yogesh25-04-2012
1-How to upload images in PHP all the process with code and step wise give answers pls help.. 2-How to remove or Modified image in PHP.
ramesh21-06-2012
thnk the discription above is very good nd easily understandble i learn very well thanks again
jarold30-01-2013
thanks it gives me an idea....recently we are trying to make a program to upload a photo but we need to make it as our profile picture...can anyone help me or give code for this
Jothi20-06-2014
how to use this coding i am so confussed.
prince kumar07-09-2014
how to upload multiple images and store in the folder
subhendu07-09-2014
There is a link at the end to next tutorial on multiple file upload. You can upload and give them a unique file name to store in a directory.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer