Skip to: site menu |main content

Welcome to my personal site. If You have any question or suggestion please feel free to contact me from contact page.

Advertisements

Contact me to place your Ads Here..

Powered by Drupal, an open source content management system

Syndicate

Syndicate content
 
 

File Uploading Example Code for PHP

1 reply [Last post]
inder's picture
Offline
Joined: 06/23/2009
File Uploading Example Code for PHP

PHP function move_uploaded_file — Moves an uploaded file to a new location. This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination .

This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system. This function returns TRUE on success, or FALSE on failure.
Syntax
move_uploaded_file(file,newloc);

Parameter Description
file Required. Specifies the file to be moved
newloc Required. Specifies the new location for the file

<?php
if(isset($_FILES['pictures'])){
   
$uploads_dir = '/img/uploads';
    foreach (
$_FILES["pictures"]["error"] as $key => $error) {//I hop u have used name "pictures" for upload control
       
if ($error == UPLOAD_ERR_OK) {
           
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];//Temp name from file
           
$name = preg_replace('/[^a-z0-9_\-\.]/i', '_', $_FILES["file"]["name"][$key];// Remove special characters from file name to store in datbaes and dir
           
           
if(move_uploaded_file($tmp_name, "$uploads_dir/$name")){
               
mysql_query("INSERT into posts ( image) values ('%s')","$uploads_dir/$name");//Insert path  into database.
           
}
        }
    }
}
?>

Guest
reply

iz nice one it help me a lot..
have a nice day.

Back to top

Related Pictures

Tags for File Uploading Example Code for PHP

Recent comments

InderSingh.com v1.2
(Feb 05, 2012)