Monday, September 16, 2013

PHP FILE UPLOADING

Hiiiii, some people find it difficult to upload a file in PHP . So, here in this post I bring to you a code to upload a file from via form browse option from any where in your computer to a directory "uploaded files " as specified in your project.
PHP FILE UPLOADING

So, Here's the code below to upload a file :




For File Uploading , you first required a from to be multipart , just take  a look :
<form action=”#” method=”post” enctype=”multipart/form-data”>
<label for=”file”>Filename:</label>
<input type=”file” name=”file” id=”file” >
<input type=”submit” name=”submit” value=”Submit”>
</form>
Then after submitting a form you require a code given below to upload the file to the desired location :
if($_FILES['file']['name']!=”"){
$ext_allowed = array (“gif”, “jpg”, “jpeg”, “png”);     //File extensions allowed to be uploaded
$img_prefix  = date(‘Ymdhis’).”PR_”;//str_replace(” “, “_”, $product_name).”_”;
$file_name  = $_FILES['file']['name'];
$pos   = strrpos($file_name, “.”);
$len   = strlen($file_name);
$ext   = substr($file_name ,$pos+1, $len-1);
$ext   = strtolower($ext);
if (in_array ($ext, $ext_allowed)) {
if (is_uploaded_file($_FILES['file']['tmp_name'])){
$image_real  = str_replace(‘ ‘, ‘_’, $img_prefix.$file_name);
@copy($_FILES['file']['tmp_name'], “./uploaded_files/”.$image_real);
}
}
}
Final image name will be  : $image_real .
This code is default  uploading to the folder name – “uploaded_files” in your ‘root’ directory .

Hope you like this post……..Please Comment…………!!!!!!!!!
Originally posted at Mycodestock.


Tags:

0 Responses to “PHP FILE UPLOADING”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks