图像未保存在文件夹中

时间:2016-10-21 13:16:39

标签: php image-uploading

我使用下面的代码在新文件中通过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.";
}
?>

但是当我在已经存在的表单页面中使用相同的代码,如下面的代码[name,email .... etc],它没有保存在文件夹路径中,看起来像代码的一部分是冲突的,我真的刚开始学习php,所以请帮助我。

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

当我从上面的页面中移除下面的代码时,图片正在上传,我试图删除大量代码并缩短代码,以便它有助​​于读者轻松解决问题,但我最终得到了很多不同类型的错误,所以我发布了完整的代码,我非常抱歉....

$newArray = $nonpremium + $premium

1 个答案:

答案 0 :(得分: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();
}