Assalamu'alaikum wr. wb
Hello Sahabat Pelangi semunya . . . :)
Semoga dalam kesempatan kali ini Sahabat Pelangi semua dalam keadan sehat dan slalu dalam lindungan-Nya. Amin Ya Rabb . . . :)
Semoga dalam kesempatan kali ini Sahabat Pelangi semua dalam keadan sehat dan slalu dalam lindungan-Nya. Amin Ya Rabb . . . :)
Udah lama ya saya nggak ngeblog . . . kemaren menyibukkan diri dengan sedikit kegiatan . . . heheheeh.
Kali ini saya ingin berbagi dengan sahabat pelangi bagaimana membuat upload file tanpa merefresh halaman web. Sebelumnya saya juga kesulitan dengan hal ini, namun selagi kita masih terkoneksi ke internet jangan bingung, langsung saja browsing dan mengembara di dunia lain heheheh . . . Insha allah sahabat pelangi akan temukan apa yang sahabat inginkan . . . Apa sih yang nggak ada di internet?? Hampir semuanya ada di internet. Eiiiitssss . . . Jangan cari nggak-nggak ya . . . hehehe . . .
Nah, sahabat pelangi semua, berikut coding program untuk membuat Upload File tanpa refresh halaman web.
Code PHP :
Kali ini saya ingin berbagi dengan sahabat pelangi bagaimana membuat upload file tanpa merefresh halaman web. Sebelumnya saya juga kesulitan dengan hal ini, namun selagi kita masih terkoneksi ke internet jangan bingung, langsung saja browsing dan mengembara di dunia lain heheheh . . . Insha allah sahabat pelangi akan temukan apa yang sahabat inginkan . . . Apa sih yang nggak ada di internet?? Hampir semuanya ada di internet. Eiiiitssss . . . Jangan cari nggak-nggak ya . . . hehehe . . .
Nah, sahabat pelangi semua, berikut coding program untuk membuat Upload File tanpa refresh halaman web.
Code PHP :
<?php
$upload_location = "upload_folder/";
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['vasPhoto_uploads']['name'];
$size = $_FILES['vasPhoto_uploads']['size'];
$allowedExtensions = array("jpg","jpeg","gif","png");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '' && strlen($name)) {
if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {
echo '<div class="notice" style="width:500px;">Sorry, you attempted to upload an invalid file format. <br>Only jpg, jpeg, gif and png image files are allowed. Thanks.</div><br clear="all" />';
}
else {
if($size<(1024*1024))
{
$actual_image_name = "default".'.gif'; // This could be a random name such as rand(125678,098754).'.gif'; but i have used a default name just for demo purpose
$dirnameas = $upload_location.$actual_image_name; //Get and delete old image file before uploading a new one if necessary by you otherwise, remove this
@chmod($dirnameas,0777);
@unlink($dirnameas);
if(move_uploaded_file($_FILES['vasPhoto_uploads']['tmp_name'],$upload_location.$actual_image_name)) {
//Run your SQL Query here to insert the new image file named $actual_image_name if you deem it necessary
echo '<span class="uploadeFileWrapper"><img src="'.$upload_location.$actual_image_name.'" width="150" height="100"></span><br clear="all" /><br clear="all" />';
}
else {
echo "<div class='notice' style='width:500px;'>Sorry, Your Image File could not be uploaded at the moment. <br>Please try again or contact the site admin if this problem persist. Thanks.</div><br clear='all' />";
}
}
else {
echo "<div class='notice' style='width:400px;'>File exceeded 1MB max allowed file size. <br>Please upload a file at 1MB in size to proceed. Thanks.</div><br clear='all' />";
}
}
}
else {
echo "<div class='notice' style='width:400px;'>You have just canceled your file upload process. Thanks.</div><br clear='all' />";
}
}
}
$upload_location = "upload_folder/";
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['vasPhoto_uploads']['name'];
$size = $_FILES['vasPhoto_uploads']['size'];
$allowedExtensions = array("jpg","jpeg","gif","png");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '' && strlen($name)) {
if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {
echo '<div class="notice" style="width:500px;">Sorry, you attempted to upload an invalid file format. <br>Only jpg, jpeg, gif and png image files are allowed. Thanks.</div><br clear="all" />';
}
else {
if($size<(1024*1024))
{
$actual_image_name = "default".'.gif'; // This could be a random name such as rand(125678,098754).'.gif'; but i have used a default name just for demo purpose
$dirnameas = $upload_location.$actual_image_name; //Get and delete old image file before uploading a new one if necessary by you otherwise, remove this
@chmod($dirnameas,0777);
@unlink($dirnameas);
if(move_uploaded_file($_FILES['vasPhoto_uploads']['tmp_name'],$upload_location.$actual_image_name)) {
//Run your SQL Query here to insert the new image file named $actual_image_name if you deem it necessary
echo '<span class="uploadeFileWrapper"><img src="'.$upload_location.$actual_image_name.'" width="150" height="100"></span><br clear="all" /><br clear="all" />';
}
else {
echo "<div class='notice' style='width:500px;'>Sorry, Your Image File could not be uploaded at the moment. <br>Please try again or contact the site admin if this problem persist. Thanks.</div><br clear='all' />";
}
}
else {
echo "<div class='notice' style='width:400px;'>File exceeded 1MB max allowed file size. <br>Please upload a file at 1MB in size to proceed. Thanks.</div><br clear='all' />";
}
}
}
else {
echo "<div class='notice' style='width:400px;'>You have just canceled your file upload process. Thanks.</div><br clear='all' />";
}
}
}
?>