im making a user profile edit page and i have added a profile pic upload option but it does not show error message when file size is above limit
if(isset($_POST["submits"]))
{
$target_dir = "uploads/";
$target_maxsize=2000000;
if(!($_FILES['fileToUpload']['name']==""))
{
$target_file = $target_dir.basename($_FILES['fileToUpload']['name']);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg")
{
$_SESSION['uperr']="Please select an image file (.jpg, .jpeg & .png)";
header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
else
{
if($_FILES['fileToUpload']['size'] < $target_maxsize)
{
if(file_exists($target_file))
{
$_SESSION['uperr']="File Already Exists!";
header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
else
{
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file))
{
$_SESSION['uperr']="File has been uploaded successfully!";
header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
else
{
$_SESSION['uperr']="File could not be uploaded!";
header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
}
}
else
{
$_SESSION['uperr']="File is too large!";
header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
}
}
else
{
$_SESSION['uperr']="Please select a file first!";
header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
}
this is what i use for displaying error message if any
<body>
<?php
if(isset($_SESSION['uperr'])/*$sesfilerr!=""*/)
{
?><script>
$( document ).ready(function() {
alert("<?php echo $_SESSION['uperr']; ?>");
});
</script><?php
unset($_SESSION['uperr']);
}
?>
</body>
help anybody?
also when i refresh the page the error message is displayed again. anything to prevent this?
Please try this below code
if(isset($_SESSION['uperr']))
{
?><script>
$( document ).ready(function() {
alert("<?php echo $_SESSION['uperr']; ?>");
});
</script><?php
unset($_SESSION['uperr']);
$_SESSION['sett']=1;
}
?>
Related
I have the following code. On failure i need to get the alert Already exists and redirect to te registration page. But on success i need to check the session and based on the role id i need to redirect the page but the alert and redirection is not happening in this case ... can you please tell where i am wrong
if ($referral_patient_id != NULL) {
echo "<script language='javascript'>
alert('Successfully Added');
if ($_SESSION['role_id'] != 6) {
echo 'window.location.href="../Laboratory_Home"';
} else {
echo 'window.location.href="../Billing_Home"';
} </script>";
} else {
echo "<script language='javascript'>
alert('Already Exists');
window.location.href = '../../LabAdd_Patients';
</script>";
}
<?php if ($referral_patient_id != NULL) { ?>
<script type='javascript'>
alert('Successfully Added');
<?php if ($_SESSION['role_id'] != 6) { ?>
window.location.href="../Laboratory_Home";
<?php } else { ?>
window.location.href="../Billing_Home";
<?php } ?>
</script>
<?php }else { ?>
<script type='javascript'>
alert('Already Exists');
window.location.href = '../../LabAdd_Patients';
</script>
<?php } ?>
Try this one dude.
Try this javascript code :
<script language='javascript'>
var referral_patient_id = '<?php echo $referral_patient_id; ?>';
if(referral_patient_id != null)
{
alert('Successfully Added');
var role_id = '<?php echo $_SESSION["role_id"]; ?>';
if (role_id != '6') {
window.location.href="../Laboratory_Home";
} else {
window.location.href="../Billing_Home";
}
}
else
{
alert('Already Exists');
window.location.href = '../../LabAdd_Patients';
}
</script>
You have mixed content. PHP and Javascript. Try this one
echo "<script language='javascript'>
alert('Successfully Added');
</script>";
if($_SESSION['role_id']!=6){
echo "<script language='javascript'>window.location.href='../Laboratory_Home'</script>";
} else {
echo "<script language='javascript'>window.location.href='../Billing_Home'</script>";
}
Or this one
echo "<script language='javascript'>
alert('Successfully Added');
if( ".$_SESSION['role_id']." !=6) {
window.location.href='../Laboratory_Home';
} else {
window.location.href='../Billing_Home';
}
</script>";
I am trying to create a form where when the user chooses a file, the file chosen will automatically upload to the database by going to the action in form. I used the onchange function and it just sends me to my php file in which that file contains my uploading system. What i think my problem is about my $_POST['asdasd'], but i cant really think of any other solutions to this.
Here is my form:
<form action="includes/profile_picture_inc.php" method="POST" enctype="multipart/form-data" id="form">
<input type="file" name="file" id="upload" onchange="document.getElementById('form').submit();">
</form>
The PHP File:
if (isset($_POST['submit'])) {
$target_dir = "../users/".$_SESSION['u_id']."/image/";
$str = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$rand = substr(str_shuffle($str), 0, 10);
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $rand . '.' . end($temp);
$target_file = $target_dir . $newfilename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ". <br/>";
$uploadOk = 1;
} else {
echo "File is not an image. <br/>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists. <br/>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 5000000) {
echo "Sorry, your file is too large. <br/>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed. <br/>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded. <br/>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
require 'database_inc.php';
$sql = "UPDATE `users` SET `user_profile` = '$newfilename' WHERE 1";
if (mysqli_query($conn,$sql)) {
$_SESSION['profile_picture'] = $newfilename;
header("Location: ../profile.php");
} else {
echo "The query has not been updated. <br/>";
}
} else {
echo "Sorry, there was an error uploading your file. <br/>";
}
}
}
It is not saving because in the php file you have the line
if (isset($_POST['submit'])) {
add in the form a hidden field
<form action="includes/profile_picture_inc.php" method="POST" enctype="multipart/form-data" id="form">
<input type="file" name="file" id="upload" onchange="document.getElementById('form').submit();">
<input type="hidden" name="submited" value="1" />
</form>
And in php change the line
if (isset($_POST['submit'])) {
to
if (isset($_POST['submited'])) {
That should do it
Instead of doing the $_POST['submit'] validation, try to check if the request is a $_POST.
To do that you can use something like
if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST') { … }
or
if ($_SERVER['REQUEST_METHOD'] === 'POST') {…}
i recommend you to use the first one.
PHP doc: http://php.net/manual/pt_BR/function.filter-input.php
$image= addslashes(file_get_contents(basename($_FILES['image']['tmp_name'])));
if(!empty($_FILES['image']['tmp_name']) && file_exists($_FILES['image']['tmp_name'])) //image is selected
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext == 'png' ||$ext == 'jpeg'||$ext == 'jpg')
{
//$insertQuery
}
else
{
echo '<script language="javascript">';
echo 'alert("Please select correct picture format")';
echo '</script>';
}
The problem is that I can't get/validate the file extension. Every time I upload a photo that matches the condition, it will only show me the "select correct picture" message.
What's wrong with my code?
you can use php built-in pathinfo function.
<?php
$supported_image = array(
'gif',
'jpg',
'jpeg',
'png'
);
$src_file_name = 'abskwlfd.PNG';
$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION)); // Using strtolower to overcome case sensitive
if (in_array($ext, $supported_image)) {
echo "it's image";
} else {
echo 'not image';
}
?>
I am trying to set an alert message if the message has been sent successfully or if an error occurs.
This is the following error when I run and click the "Send Mail" button in my html page.
Error: Microsoft JScript runtime error: Unable to get value of the
property 'addClass': object is null or undefined
This is my PHP code snippet:
if(#mail($to,$subject,$message,$header)) $send = true; else $send = false;
if(isset($_POST['email'])){
if ($send)
{
echo '<script language="javascript">';
echo 'alert("Mail has been Sent Successfully")';
echo '</script>';
exit;
}
else
{
echo '<script language="javascript">';
echo 'alert("ERROR")';
echo '</script>';
exit;
}
This is my jQuery snippet:
function tfuse_custom_form(){
var my_error;
var url = jQuery("input[name=temp_url]").attr('value');
jQuery("#send").bind("click", function(){
my_error = false;
jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").each(function(i)
{
var surrounding_element = jQuery(this);
var value = jQuery(this).attr("value");
var check_for = jQuery(this).attr("id");
var required = jQuery(this).hasClass("required");
if(check_for == "email")
{
surrounding_element.removeClass("error valid");
baseclases = surrounding_element.attr("class");
if(!value.match(/^\w[\w|\.|\-]+#\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/))
{
surrounding_element.attr("class",baseclases).addClass("error");
my_error = true;
}
else
{
surrounding_element.attr("class",baseclases).addClass("valid");
}
}
if(check_for == "message")
{
surrounding_element.removeClass("error valid");
baseclases = surrounding_element.attr("class");
if(value == "" || value == "Write your message...")
{
surrounding_element.attr("class",baseclases).addClass("error");
my_error = true;
}
else
{
surrounding_element.attr("class",baseclases).addClass("valid");
}
}
if(required && check_for != "email" && check_for != "message")
{
surrounding_element.removeClass("error valid");
baseclases = surrounding_element.attr("class");
if(value == "")
{
surrounding_element.attr("class",baseclases).addClass("error");
my_error = true;
}
else
{
surrounding_element.attr("class",baseclases).addClass("valid");
}
}
}
First compare this code and then replace it simply if you want to jquery in php
add this code in php file.
1).
<?php echo '<script language="javascript">
alert("Mail has been Sent Successfully");
</script>';
?>
2).
if ($send)
{
echo '<script language="javascript">
alert("Mail has been Sent Successfully");
</script>';
exit;
}
else
{
echo '<script language="javascript">
alert("ERROR")'
</script>';
exit;
}
I have this:
<input class="inline" id="imgInput" name='imgInput' type="file" ng-file-select="saveImage($files)" accept="image/*" data-ng-model="inputImage">
It works in Mozilla and Chrome, but in Safari I can choose any kind of file. How can I get the input to accept only image files in Safari?
This is a part of a code I use to check if file is a picture.
<?php
if($_FILES['picture'] == '' OR empty($_FILES['picture']))
{
header("Location: picture.php?error=file");
}
elseif ($_FILES['picture']['error'] > 0)
{
if($_FILES['picture']['error'] == UPLOAD_ERR_NO_FILE)
{ header("Location: picture.php?error=file");}
elseif($_FILES['picture']['error'] == UPLOAD_ERR_INI_SIZE)
{ header("Location: picture.php?error=size");}
elseif($_FILES['picture']['error'] == UPLOAD_ERR_FORM_SIZE)
{ header("Location: picture.php?error=size");}
elseif($_FILES['picture']['error'] == UPLOAD_ERR_PARTIAL)
{ header("Location: picture.php?error=partial");}
else {
header("Location: picture.php?error=file");
}
}
$imageSize = getimagesize($_FILES['picture']['tmp_name']);
if($imageSize['mime'] == 'image/jpeg' OR $imageSize['mime'] == 'image/png' OR $imageSize['mime'] == 'image/gif')
{
$validExtensions = array('jpg','jpeg','gif','png');
$extension = strtolower(substr(strrchr($_FILES['picture']['name'], '.'),1));
if (!in_array($extension,$validExtensions))
{ header("Location: picture.php?error=extension");}
if ($_FILES['picture']['size'] > 5242880)
{ header("Location: picture.php?error=size");}
if ($imageSize[0] > 5000 OR $imageSize[1] > 5000)
{ header("Location: picture.php?error=size");}
//DO OTHER THINGS
}
else
{
header("Location: picture.php?error=type");
}
?>