Function Oriented File Upload PHP
Put the Following Function here:
<?php
function uploadimgfile($index,$folder="other",$prefix="other")
{
$target_dir = "upload_directory_path/"; // try to put full physical path
// identity accstatement address advtimg other
$uploadOk = 1;
$senddata = array();
$senddata['data'] = "NILL";
$notallowed = array("php","js","css","html"); // defined here the extesion not to upload
$shownotallowed = "PHP, JS, CSS, HTML fie is not allowed to upload.";
$extension = explode(".",basename($_FILES[$index]["name"]));
$extension = end($extension);
ECHO "<hr/>" . $realfilnename = basename($_FILES[$index]["name"]);
$datetofolder = date("Y/M/d");
$datetofolder = strtolower($datetofolder);
$checkdirectory = $target_dir."$folder/$datetofolder";
if (!file_exists($checkdirectory))
{
mkdir($checkdirectory, 0777, true);
}
$filnename = "$folder/$datetofolder/$prefix".md5(time().rand()).".$extension";
$target_file = $target_dir . $filnename;
if (in_array($extension, $notallowed))
{
$uploadOk = 0;
$senddata['status'] = 0;
$senddata['message'] = $shownotallowed;
return $senddata;
}
// Check file size
if ($_FILES[$index]["size"] > 10485760)
{
$uploadOk = 0;
$senddata['status'] = 0;
$senddata['message'] = "Maximum File Upload size is 10MB.";
return $senddata;
// echo "Sorry, your file is too large.";
// $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
$senddata['status'] = 0;
$senddata['message'] = "<b>Sorry!</b> There was an error uploading your file.2";
return $senddata;
// echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[$index]["tmp_name"], $target_file))
{
// echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$senddata['status'] = 1;
$tempdata = array();
$tempdata['name'] = $filnename;
$tempdata['realname'] = $realfilnename;
$senddata['data'] = $tempdata;
$senddata['message'] = "File Uploaded successfully.";
return $senddata;
} else {
// echo "Sorry, there was an error uploading your file.";
$senddata['status'] = 0;
$senddata['message'] = "<b>Sorry!</b> There was an error uploading your file.";
return $senddata;
}
}
}
?>
How to use the function:
<?php
if(isset($_POST['uploadfile']))
{
$filetoupload1 = uploadimgfile("filetoupload1",$folder="folder1",$prefix="file1_");
$filetoupload2 = uploadimgfile("filetoupload2",$folder="folder2",$prefix="file2_");
echo "<pre>";
print_r($filetoupload1);
echo "</pre>";
echo "<pre>";
print_r($filetoupload2);
echo "</pre>";
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="uploadfile" value="1" />
<input type="file" name="filetoupload1" />
<input type="file" name="filetoupload2" />
<input type="submit" name="submit" />
</form>
<?php
function uploadimgfile($index,$folder="other",$prefix="other")
{
$target_dir = "upload_directory_path/"; // try to put full physical path
// identity accstatement address advtimg other
$uploadOk = 1;
$senddata = array();
$senddata['data'] = "NILL";
$notallowed = array("php","js","css","html"); // defined here the extesion not to upload
$shownotallowed = "PHP, JS, CSS, HTML fie is not allowed to upload.";
$extension = explode(".",basename($_FILES[$index]["name"]));
$extension = end($extension);
ECHO "<hr/>" . $realfilnename = basename($_FILES[$index]["name"]);
$datetofolder = date("Y/M/d");
$datetofolder = strtolower($datetofolder);
$checkdirectory = $target_dir."$folder/$datetofolder";
if (!file_exists($checkdirectory))
{
mkdir($checkdirectory, 0777, true);
}
$filnename = "$folder/$datetofolder/$prefix".md5(time().rand()).".$extension";
$target_file = $target_dir . $filnename;
if (in_array($extension, $notallowed))
{
$uploadOk = 0;
$senddata['status'] = 0;
$senddata['message'] = $shownotallowed;
return $senddata;
}
// Check file size
if ($_FILES[$index]["size"] > 10485760)
{
$uploadOk = 0;
$senddata['status'] = 0;
$senddata['message'] = "Maximum File Upload size is 10MB.";
return $senddata;
// echo "Sorry, your file is too large.";
// $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
$senddata['status'] = 0;
$senddata['message'] = "<b>Sorry!</b> There was an error uploading your file.2";
return $senddata;
// echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[$index]["tmp_name"], $target_file))
{
// echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$senddata['status'] = 1;
$tempdata = array();
$tempdata['name'] = $filnename;
$tempdata['realname'] = $realfilnename;
$senddata['data'] = $tempdata;
$senddata['message'] = "File Uploaded successfully.";
return $senddata;
} else {
// echo "Sorry, there was an error uploading your file.";
$senddata['status'] = 0;
$senddata['message'] = "<b>Sorry!</b> There was an error uploading your file.";
return $senddata;
}
}
}
?>
How to use the function:
<?php
if(isset($_POST['uploadfile']))
{
$filetoupload1 = uploadimgfile("filetoupload1",$folder="folder1",$prefix="file1_");
$filetoupload2 = uploadimgfile("filetoupload2",$folder="folder2",$prefix="file2_");
echo "<pre>";
print_r($filetoupload1);
echo "</pre>";
echo "<pre>";
print_r($filetoupload2);
echo "</pre>";
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="uploadfile" value="1" />
<input type="file" name="filetoupload1" />
<input type="file" name="filetoupload2" />
<input type="submit" name="submit" />
</form>
Comments
Post a Comment