2016-10-21 2 views
0

Я использую приведенный ниже код для загрузки изображения через php-форму в свежий файл, его рабочий режим.изображение не сохраняется в папке

<!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."; 
} 
?> 

но когда я использовал тот же код в уже существовал вид страницы, как показано ниже кода [имя, адрес электронной почты .... и т.д.], его сохранения в пути к папке, выглядит как часть коды противоречива, я на самом деле только начал изучать php, поэтому, пожалуйста, любезно помогите мне.

<?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."; 
} 
?> 

когда я удалить ниже кода сверху страницы, изображение загрузки тонкой, я попытался удалить много кода и сделать его коротким, так что его будет полезно для читателей, чтобы решить проблему легко, но я в конечном итоге в получении много различных видов ошибок, поэтому я отправил полный код, я очень сожалею о том, что ....

<?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 */ 
?> 
+0

у вас возникли какие-либо ошибки? –

+2

Было бы неплохо подумать о разумном отступлении кода. Это помогает нам читать код и, что более важно, это поможет ** вы отлаживаете свой код ** [Взгляните на стандарт кодирования] (http://www.php-fig.org/psr/psr-2/) для вашей собственной выгоды. Вас могут попросить внести поправку в этот код через несколько недель/месяцев, и вы в конце концов поблагодарите меня. – RiggsFolly

+0

У меня была какая-то ошибка ..... @KamleshGupta –

ответ

2

Это не будет делать то, что вы ожидаете, как есть в ; Неправильное место здесь

// 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(); 
} 
+0

... и он ';' будучи допустимым символом (в php), не должен был бы вызывать ошибку ;-) даже с набором отчетов об ошибках. –