PHP file upload, core php file upload
<?php
error_reporting(E_ALL);
function check_file_is_image($fileName){
//you need to send $_FILES["fileToUpload"]["name"]
$imageFileType = pathinfo($fileName,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
return FALSE;
}else return TRUE;
}
function check_file_size($fileName, $maxSize=500){
if ($_FILES["fileToUpload"]["size"] > $maxSize) {
return false;
}return true;
}
function check_file_exists($target_file){
if (file_exists($target_file))
return false;
return true;
}
if(isset($_POST['sub'])){
$target_dir = "upload/";
if(isset($_FILES['fileFieldName']['tmp_name'])){
$filename = $_FILES["fileFieldName"]["name"];
if(check_file_is_image($filename)==false){
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}else if(check_file_size($_FILES["fileFieldName"]["size"])==false){
echo "Sorry, your file is too large.";
}else if(check_file_exists($target_dir.$_FILES["fileFieldName"]["name"])==false){
echo "Sorry, file already exists.";
}else if(move_uploaded_file($_FILES["fileFieldName"]["tmp_name"], $target_dir.$_FILES["fileFieldName"]["name"])){
echo "file uploaded successfully..";
}else{
echo "Sorry..! Something went wrong..";
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileFieldName">
<input type="submit" name="sub"/>
</form>
<?php
error_reporting(E_ALL);
function check_file_is_image($fileName){
//you need to send $_FILES["fileToUpload"]["name"]
$imageFileType = pathinfo($fileName,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
return FALSE;
}else return TRUE;
}
function check_file_size($fileName, $maxSize=500){
if ($_FILES["fileToUpload"]["size"] > $maxSize) {
return false;
}return true;
}
function check_file_exists($target_file){
if (file_exists($target_file))
return false;
return true;
}
if(isset($_POST['sub'])){
$target_dir = "upload/";
if(isset($_FILES['fileFieldName']['tmp_name'])){
$filename = $_FILES["fileFieldName"]["name"];
if(check_file_is_image($filename)==false){
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}else if(check_file_size($_FILES["fileFieldName"]["size"])==false){
echo "Sorry, your file is too large.";
}else if(check_file_exists($target_dir.$_FILES["fileFieldName"]["name"])==false){
echo "Sorry, file already exists.";
}else if(move_uploaded_file($_FILES["fileFieldName"]["tmp_name"], $target_dir.$_FILES["fileFieldName"]["name"])){
echo "file uploaded successfully..";
}else{
echo "Sorry..! Something went wrong..";
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileFieldName">
<input type="submit" name="sub"/>
</form>
0 comments:
Post a Comment