I'm using WampServer for running PHP scripts offline. This code is to upload an image and set it as profile photo of the user. So, to store the data I've created a directory "account" & "account/users/" to store my php scripts and user's data and information.
PHP:
<?php
session_start();
if (isset($_POST["file"]) || isset($_GET["file"]) || isset($_GET[md5("upload")])) {
$allowedExts = array("gif", "jpg", "jpeg", "png");
$temp = explode(".", $_FILES['file']['name']);
$extension = end($temp);
$email = "";
if (isset($_SESSION["email"]))
$email = $_SESSION["email"];
$f = "/php/account/users/$email/profile/";
if ((($_FILES['file']['type'] == "image/gif") || ($_FILES['file']['type'] == "image/jpg") || ($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/png") || ($_FILES['file']['type'] == "image/pjpeg") || ($_FILES['file']['type'] == "image/x-png")) && in_array($extension, $allowedExts)) {
if ($_FILES['file']['error'] > 0) {
echo "Error: " . $_FILES['file']['error'] . '<br>';
} else {
$f = "/php/account/users/$email/profile/photo_name";
$fid = "/php/account/users/total_photos";
$d = 0;
if (file_exists($fid)) {
$o = fopen($fid, "r");
$d = fread($o, filesize($fid));
fclose($o);
$d+=1;
} else {
$d+=1;
}
move_uploaded_file($_FILES['file']['tmp_name'], "users/photos/" . $_FILES['file']['name']);
$old = "/php/account/users/photos/" . $_FILES['file']['name'];
$new = "/php/account/users/photos/$d";
rename($old, $new);
$o = fopen($f, "w+");
fwrite($o, $d);
fclose($o);
if (file_exists($new)) {
$o = fopen($fid, "c");
fwrite($o, $d);
fclose($o);
die(header("location:http://localhost/php/account/upload.php?upload=" . md5("true")));
}
else
die(header("location:http://localhost/php/account/upload.php?upload=" . md5("false")));
}
} else {
$f = "/php/account/users/$email/profile/";
if (!file_exists($f))
mkdir($f);
$f.="photo_name";
$o = fopen($f, "w+");
fwrite($o, "profile");
fclose($o);
}
} else {
echo '<html><head><script src="http://localhost/php/account/upload.js"></script><link href="http://localhost/php/account/upload.css" rel="stylesheet"/></head><body>';
if (isset($_GET["upload"])) {
if ($_GET["upload"] == md5("true")) {
//echo'<marquee behavior="slide" direction="down" width="100%" style="text-align: center;" id="pop-up"><span width="100%" style="text-align: center;">File Uploaded Successfully!</span><span style="float: right"><button id="close" title="Click to close this Pop-up" onclick="pop();">X</button></span></marquee>';
if (isset($_COOKIE[md5("signup")])) {
$time = -1;
setcookie(md5("first_name"), "", $time);
setcookie(md5("last_name"), "", $time);
setcookie(md5("year"), "", $time);
setcookie(md5("month"), "", $time);
setcookie(md5("date"), "", $time);
setcookie(md5("gender"), "", $time);
die(header("location:http://localhost/php/account/home.php?" . md5("new")));
} else
die(header("location:http://localhost/php/account/home.php"));
}
if ($_GET["upload"] == md5("false"))
echo'<marquee behavior="slide" direction="down" width="100%" style="text-align: center;" id="pop-up"><span width="100%" style="text-align: center;">File Uploaded Unsuccessfully!</span><span style="float: right"><button id="close" title="Click to close this Pop-up" onclick="pop();">X</button></span></marquee>';
}
echo'<form action="?' . md5("upload") . '" enctype="multipart/form-data" method="post"><table width="100%"><tr><th colspan="3"><label for="pro_upload">Set Profile Photo</label></th><tr><th colspan="3"><img class="img" src="http://localhost/php/account/photo.php?default=1&id=0&view=0" id="set_pro"/></th></tr><tr><th colspan="3"><input type="file" accept="image/*" id="pro_upload" name="file" class="file_upload" onchange="readURL(this);"></th></tr><tr><th colspan="3"><button type="submit" title="Upload this photo">Upload</button></th></tr></table></form></body></html>';
}
?>
CSS:
.file_upload{
width: 550px;
appearance: push-button;
color: rgb(153, 217, 234);
}
.img{
width: 160px;
height:160px;
}
body{
background-color: #00A2E8;
color: white;
line-height: 1.5em;
margin: 0;
}
input[type="file"]{
border: solid medium;
border-color: rgb(153, 217, 234);
}
button{
border-color: #009CE8;
background-color: #009CE8;
appearance: push-button;
color: white;
font-size: xx-large;
height: 55px;
width: 350px;
font-weight: bolder;
}
table{
background-color: whitesmoke;
border-color: whitesmoke;
color: #00A2E8;
margin: 0;
}
#pop-up{
background-color: #00A2E8;
font-size: small;
margin: 0;
color: white;
cursor: pointer;
}
#close{
border: #009CEB;
width: 15px;
height: 15px;
font-size: xx-small;
}
marquee{
height: 20px;
}
JavaScript:
function readURL(input){
if(input.files && input.files[0]){
var reader=new FileReader();
reader.onload=function(e){
var pro=document.getElementById('set_pro');
pro.src=e.target.result;
}
reader.readAsDataURL(input.files[0]);
}
}
function pop(){
document.getElementsByTagName("marquee")[0].direction="up";
document.getElementsByTagName("marquee")[0].style.width="0px";
document.getElementsByTagName("table")[0].style.marginTop="-20px";
}
PHP:
<?php
session_start();
if (isset($_POST["file"]) || isset($_GET["file"]) || isset($_GET[md5("upload")])) {
$allowedExts = array("gif", "jpg", "jpeg", "png");
$temp = explode(".", $_FILES['file']['name']);
$extension = end($temp);
$email = "";
if (isset($_SESSION["email"]))
$email = $_SESSION["email"];
$f = "/php/account/users/$email/profile/";
if ((($_FILES['file']['type'] == "image/gif") || ($_FILES['file']['type'] == "image/jpg") || ($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/png") || ($_FILES['file']['type'] == "image/pjpeg") || ($_FILES['file']['type'] == "image/x-png")) && in_array($extension, $allowedExts)) {
if ($_FILES['file']['error'] > 0) {
echo "Error: " . $_FILES['file']['error'] . '<br>';
} else {
$f = "/php/account/users/$email/profile/photo_name";
$fid = "/php/account/users/total_photos";
$d = 0;
if (file_exists($fid)) {
$o = fopen($fid, "r");
$d = fread($o, filesize($fid));
fclose($o);
$d+=1;
} else {
$d+=1;
}
move_uploaded_file($_FILES['file']['tmp_name'], "users/photos/" . $_FILES['file']['name']);
$old = "/php/account/users/photos/" . $_FILES['file']['name'];
$new = "/php/account/users/photos/$d";
rename($old, $new);
$o = fopen($f, "w+");
fwrite($o, $d);
fclose($o);
if (file_exists($new)) {
$o = fopen($fid, "c");
fwrite($o, $d);
fclose($o);
die(header("location:http://localhost/php/account/upload.php?upload=" . md5("true")));
}
else
die(header("location:http://localhost/php/account/upload.php?upload=" . md5("false")));
}
} else {
$f = "/php/account/users/$email/profile/";
if (!file_exists($f))
mkdir($f);
$f.="photo_name";
$o = fopen($f, "w+");
fwrite($o, "profile");
fclose($o);
}
} else {
echo '<html><head><script src="http://localhost/php/account/upload.js"></script><link href="http://localhost/php/account/upload.css" rel="stylesheet"/></head><body>';
if (isset($_GET["upload"])) {
if ($_GET["upload"] == md5("true")) {
//echo'<marquee behavior="slide" direction="down" width="100%" style="text-align: center;" id="pop-up"><span width="100%" style="text-align: center;">File Uploaded Successfully!</span><span style="float: right"><button id="close" title="Click to close this Pop-up" onclick="pop();">X</button></span></marquee>';
if (isset($_COOKIE[md5("signup")])) {
$time = -1;
setcookie(md5("first_name"), "", $time);
setcookie(md5("last_name"), "", $time);
setcookie(md5("year"), "", $time);
setcookie(md5("month"), "", $time);
setcookie(md5("date"), "", $time);
setcookie(md5("gender"), "", $time);
die(header("location:http://localhost/php/account/home.php?" . md5("new")));
} else
die(header("location:http://localhost/php/account/home.php"));
}
if ($_GET["upload"] == md5("false"))
echo'<marquee behavior="slide" direction="down" width="100%" style="text-align: center;" id="pop-up"><span width="100%" style="text-align: center;">File Uploaded Unsuccessfully!</span><span style="float: right"><button id="close" title="Click to close this Pop-up" onclick="pop();">X</button></span></marquee>';
}
echo'<form action="?' . md5("upload") . '" enctype="multipart/form-data" method="post"><table width="100%"><tr><th colspan="3"><label for="pro_upload">Set Profile Photo</label></th><tr><th colspan="3"><img class="img" src="http://localhost/php/account/photo.php?default=1&id=0&view=0" id="set_pro"/></th></tr><tr><th colspan="3"><input type="file" accept="image/*" id="pro_upload" name="file" class="file_upload" onchange="readURL(this);"></th></tr><tr><th colspan="3"><button type="submit" title="Upload this photo">Upload</button></th></tr></table></form></body></html>';
}
?>
CSS:
.file_upload{
width: 550px;
appearance: push-button;
color: rgb(153, 217, 234);
}
.img{
width: 160px;
height:160px;
}
body{
background-color: #00A2E8;
color: white;
line-height: 1.5em;
margin: 0;
}
input[type="file"]{
border: solid medium;
border-color: rgb(153, 217, 234);
}
button{
border-color: #009CE8;
background-color: #009CE8;
appearance: push-button;
color: white;
font-size: xx-large;
height: 55px;
width: 350px;
font-weight: bolder;
}
table{
background-color: whitesmoke;
border-color: whitesmoke;
color: #00A2E8;
margin: 0;
}
#pop-up{
background-color: #00A2E8;
font-size: small;
margin: 0;
color: white;
cursor: pointer;
}
#close{
border: #009CEB;
width: 15px;
height: 15px;
font-size: xx-small;
}
marquee{
height: 20px;
}
JavaScript:
function readURL(input){
if(input.files && input.files[0]){
var reader=new FileReader();
reader.onload=function(e){
var pro=document.getElementById('set_pro');
pro.src=e.target.result;
}
reader.readAsDataURL(input.files[0]);
}
}
function pop(){
document.getElementsByTagName("marquee")[0].direction="up";
document.getElementsByTagName("marquee")[0].style.width="0px";
document.getElementsByTagName("table")[0].style.marginTop="-20px";
}
Comments