abcd abcd - 1 month ago 5
PHP Question

image is not saving in folder

i am using below code to upload an image through php form in fresh file, its working fine.

<!DOCTYPE html>

<html lang="en">
<meta charset="UTF-8">

</html>

<?php
if (isset($_FILES["photo"]["error"])) {
if ($_FILES["photo"]["error"] > 0) {
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
} else {
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];

// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!array_key_exists($ext, $allowed))
die("Error: Please select a valid file format.");

// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if ($filesize > $maxsize)
die("Error: File size is larger than the allowed limit.");

// Verify MYME type of the file
if (in_array($filetype, $allowed)) {
// Check whether file exists before uploading it
if (file_exists("upload/" . $_FILES["photo"]["name"])) {
echo $_FILES["photo"]["name"] . " is already exists.";
} else {
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]);

echo "Your file was uploaded successfully.";
}
} else {

echo "Error: There was a problem uploading your file - please try again.";
}
}
} else {
echo "Error: Invalid parameters - please contact your server administrator.";
}
?>


but when i used same code in already existed form page as below code [ name, email....etc ], its not saving in folder path , looks like part of code is conflicting, i am really just started to learn php, so please kindly help me.

<?php
include 'home.php';

$userID = ""; //Initialization value; Examples
//"" When you want to append stuff later
//0 When you want to add numbers later
//isset()
$userID = isset($_POST['userID']) ? $_POST['userID'] : '';

//empty()
$userID = !empty($_POST['userID']) ? $_POST['userID'] : '';


// session_start();
require_once 'class.user.php';
$user_home = new USER();

if (!$user_home->is_logged_in()) {
header("Location: index.php");
die();
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid" => $_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>


<?php
/* php codde */
$FORM['uname'] = "";
$FORM['txtuname'] = "";
if (isset($_POST['submit'])) {
// new data
$uname = $_POST['txtuname'];
$email = $_POST['txtemail'];
$phone = $_POST['phone'];
$street_address = $_POST['street_address'];
$street_address_2 = trim($_POST['street_address_2']);
$city = trim($_POST['city']);
$state = trim($_POST['state']);
$zip_code = trim($_POST['zip_code']);
$country = trim($_POST['country']);
$sold_by = trim($_POST['sold_by']);
$portfolio = trim($_POST['portfolio']);
$paypal_email_id = trim($_POST['paypal_email_id']);
$account_holder_name = trim($_POST['account_holder_name']);
$account_number = trim($_POST['account_number']);
$branch_name = trim($_POST['branch_name']);
$bank_name = trim($_POST['bank_name']);
$ifsc_code = trim($_POST['ifsc_code']);
$uid = (isset($_GET['userID']) ? intval($_GET['userID']) : -1);

// query
if ($user_home->update($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by, $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code, $uid))
; {
header("Location: profile.php");
die();
}
}

/* php codde end */
?>

<!DOCTYPE html>
<html class="no-js">
<h2> Welcome to profile page</h2>

<head>
<title><?php echo $row['userEmail']; ?></title>
<a href="user.php?<?php print($userRow['user_name']); ?>"></a> <title><?php echo $row['userEmail']; ?></title>
<!-- Bootstrap -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<link href="assets/styles.css" rel="stylesheet" media="screen">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->


<form action="profile1.php" method="POST" enctype="multipart/form-data">
<h3>Personal details</h3>
Name :
<input type="text" name="txtuname" value="<?php echo $row['userName'] ?>" /><br/>
Email :
<input type="text" name="txtemail" value="<?php echo $row['userEmail'] ?>" /><br>
Sold_by :
<input type="text" name="sold_by" value="<?php echo $row['sold_by'] ?>" /><br/>
Portfolio :
<input type="text" name="portfolio" value="<?php echo $row['portfolio'] ?>" /><br>



<h3>ADDRESS</h3>

Phone :
<input type="text" name="phone" value="<?php echo $row['phone'] ?>" /><br>
street address :
<input type="text" name="street_address" value="<?php echo $row['street_address'] ?>" /><br>
street address 2 :
<input type="text" name="street_address_2" value="<?php echo $row['street_address_2'] ?>" /><br>
city :
<input type="text" name="city" value="<?php echo $row['city'] ?>" /><br>
state :
<input type="text" name="state" value="<?php echo $row['state'] ?>" /><br>
country :
<input type="text" name="country" value="<?php echo $row['country'] ?>" /><br>
zip :
<input type="text" name="zip_code" value="<?php echo $row['zip_code'] ?>" /><br>



<h3>Payment details</h3>

<input type="text" name="paypal_email_id" value="<?php echo $row['paypal_email_id'] ?>" /><br>

<h4>Wiretransfer </h4><br/>

Account holder name :
<input type="text" name="account_holder_name" value="<?php echo $row['account_holder_name'] ?>" /><br>

account number :
<input type="text" name="account_number" value="<?php echo $row['account_number'] ?>" /><br>

Branch name :
<input type="text" name="branch_name" value="<?php echo $row['branch_name'] ?>" /><br>

Bank name :

<input type="text" name="bank_name" value="<?php echo $row['bank_name'] ?>" /><br>
IFSC CODE :
<input type="text" name="ifsc_code" value="<?php echo $row['ifsc_code'] ?>" /><br>

<h3>Other information : </h3>

<h2>Upload File</h2>
<label for="fileSelect">Filename1:</label>
<input type="file" name="photo" id="fileSelect"><br>
<input type="submit" name="submit" value="Save" />
</form>
</html>


<!DOCTYPE html>

<html lang="en">
<meta charset="UTF-8">

</html>

<?php
if (isset($_FILES["photo"]["error"])) {
if ($_FILES["photo"]["error"] > 0) {
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
} else {
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];

// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!array_key_exists($ext, $allowed))
die("Error: Please select a valid file format.");

// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if ($filesize > $maxsize)
die("Error: File size is larger than the allowed limit.");

// Verify MYME type of the file
if (in_array($filetype, $allowed)) {
// Check whether file exists before uploading it
if (file_exists("upload/" . $_FILES["photo"]["name"])) {
echo $_FILES["photo"]["name"] . " is already exists.";
} else {
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]);

echo "Your file was uploaded successfully.";
}
} else {

echo "Error: There was a problem uploading your file - please try again.";
}
}
} else {
echo "Error: Invalid parameters - please contact your server administrator.";
}
?>


when i remove below code from above page, image is uploading fine, i tried to remove lot of code and make it short, so that its going to be helpfull for readers to solve issue easily, but i end up in getting lot of different kinds of errors, so i posted full code, i am extremely sorry for that....

<?php

/* php codde */
$FORM['uname'] = "";
$FORM['txtuname'] = "";
if (isset($_POST['submit'])) {
// new data
$uname = $_POST['txtuname'];
$email = $_POST['txtemail'];
$phone = $_POST['phone'];
$street_address = $_POST['street_address'];
$street_address_2 = trim($_POST['street_address_2']);
$city = trim($_POST['city']);
$state = trim($_POST['state']);
$zip_code = trim($_POST['zip_code']);
$country = trim($_POST['country']);
$sold_by = trim($_POST['sold_by']);
$portfolio = trim($_POST['portfolio']);
$paypal_email_id = trim($_POST['paypal_email_id']);
$account_holder_name = trim($_POST['account_holder_name']);
$account_number = trim($_POST['account_number']);
$branch_name = trim($_POST['branch_name']);
$bank_name = trim($_POST['bank_name']);
$ifsc_code = trim($_POST['ifsc_code']);
$uid = (isset($_GET['userID']) ? intval($_GET['userID']) : -1);

// query
if ($user_home->update($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by, $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code, $uid))
; {
header("Location: profile.php");
die();
}
}

/* php codde end */
?>

Answer

This is not going to do what you expect as there is a ; in the wrong place here

// query
if ($user_home->update($uname, $email, $phone, $street_address, $street_address_2, $city, $state, $zip_code, $country, $sold_by, $portfolio, $paypal_email_id, $account_holder_name, $account_number, $branch_name, $bank_name, $ifsc_code, $uid))

    ; // <-- remove this

{
    header("Location: profile.php");
    die();
}