Search code examples
phphtmlmysqlfile-uploadserver-side

PHP File Upload: Syntax?


I am currently creating a system where a user can upload both information a corresponding picture to a db. However, when I call the move_upload_file() function, it is not liking my syntax when specifying the new destination.

The line of code I am referring to looks like this:

if(move_uploaded_file($_FILES['upload']['tmp_name'], "images/{$_FILES['upload']['name']")){

And the error I'm getting is

Parse error: syntax error, unexpected '")){' (T_CONSTANT_ENCAPSED_STRING), expecting '}' in /Applications/XAMPP/xamppfiles/htdocs/serverside/phptut/addbyform.php on line 27

I am also using Sublime Text 2 and ['upload'] highlights in bright pink.

For further context this is my entire script thus far:

<?php
printForm();
//when "submit" tie together values and variables
if($_POST['submit']=="Submit"){
    $email = cleanData($_POST['email']);
    $first = cleanData($_POST['first']);
    $last = cleanData($_POST['last']);
    $status = cleanData($_POST['status']);
    //$image = cleanData($_POST['image']);
    //echo "Data cleaned";
    addData($email, $first, $last, $status);
}
else{
    //printForm();
}


function checkUpload(){
    //check for uploaded files
    if(isset($_FILES['upload'])){ //upload refers to form element "upload"
        $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/GIF');
        if(in_array($_FILES['upload']['type'], $allowed)){//if upload if in the allowed file types
            echo "uploading files...";
            //move the file over
            if(move_uploaded_file($_FILES['upload']['tmp_name'], "images/{$_FILES['upload']['name']")){
                //moveuf method moves to tmp folder then moves to final location
                echo "<p>The file has been uploaded 'dude'</p>";
                $image="{$_FILES['upload']['name']}";
                print "$image";
            }//end of moving DAT IMG :3
            else{
                echo '<p>Please upload a  JPEG, GIF or PNG image.<p>';


                if($FILES['upload']['error'] > 0){

                }
            }
        }
    }
}





//cleans information
function cleanData($data){
    if(!$status){ //everything except for status take out spaces
        $data = trim($data);
    }
    $data = stripslashes($data);//no slashes
    $data = htmlspecialchars($data);//no special characters
    $data = strip_tags($data);//no html tags
    return $data;
}

//inserts data into db
function addData($email, $first, $last, $status){
    //echo "Ready to add data";
    include("dbinfo.php");//access db
    $image = checkUpload();
    $sql = "INSERT INTO contacts VALUES(null, '$email', '$first', '$last', '$status', '$image')";
    //null because of ID aka primary key automatically incremented:3
    $result = mysql_query($sql) or die(mysql_error());
    //takes sql arugment for query OR if it can't you get a BUMMER DUDE
    echo <<<HERE
    <b>The following has been added:</b>
    <ul>
    <li>E-mail: $email</li>
    <li>First: $first</li>
    <li>Last: $last</li>
    <li>Status: $status</li>
    <li>Image File:<br/> <img src="images/$image" /></li>
    </ul>

HERE;
}

function printForm(){
    $pageTitle ="Add a Contact";
    include("header.php");
    echo <<<EOD

        <b>Add a Contact</b>
        <form method = "POST" enctype="multipart/form-data">
        <div>
            <label for="email">Email*:</label>
            <input type="text" name="email" id="email" required="required">
        </div>

        <div>
            <label for="first">First Name*:</label>
            <input type="text" name="first" id="first" required="required">
        </div>

        <div>
            <label for="last">Last Name*:</label>
            <input type="text" name="last" id="last" required="required">
        </div>

        <div>
            <label for="status">Status*:</label>
            <input type="text" name="status" id="status" required="required">
        </div>

        <div>
            <label for="image">Image*:</label>
            <input type="file" name="upload" size="30" id="upload" required="required"><br/>
            <small>Must be less than 512kb. Only JPG, GIF, and PNG files</small>
        </div>

        <div id="mySubmit">
            <input type="submit" name="submit" value="Submit">
        </div>
        </form>
EOD;
}

include("footer.php");
?>

Would anyone be able to let me know what I am doing wrong? Thanks.


Solution

  • You missed one bracket !

    if(move_uploaded_file($_FILES['upload']['tmp_name'], "images/{$_FILES['upload']['name']}")){
    

    The one after ['name'] ...