Jump to content

Unexpected T_VARIABLE on File Upload


Recommended Posts

I have followed the PHP File Upload instructions at http://www.w3schools.com/php/php_file_upload.asp

 

When I go to up load the photo I get: Parse error: syntax error, unexpected '$check' (T_VARIABLE) in C:\xampp\htdocs\upload.php on line 8

 

AGAIN! Just to confirm. I have done a copy and paste of what's on the w3schools website for PHP File Upload, I have a fresh install of xampp on my windows 10 machine, I have checked the PHP.ini to make sure file_uploads is set to ON, I have created a UPLOADS folder in the same place as my html file and upload.php.

 

Can someone please help.

Link to post
Share on other sites

can you please post the exact code you are using?

 

<!DOCTYPE html>

<html>

<body>

 

<form action="upload.php" method="post" enctype="multipart/form-data">

Select image to upload:

<input type="file" name="fileToUpload" id="fileToUpload">

<input type="submit" value="Upload Image" name="submit">

</form>

 

</body>

</html>

 

Then for the upload.php:

<?php

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

if($check !== false) {

echo "File is an image - " . $check["mime"] . ".";

$uploadOk = 1;

} else {

echo "File is not an image.";

$uploadOk = 0;

}

}

// Check if file already exists

if (file_exists($target_file)) {

echo "Sorry, file already exists.";

$uploadOk = 0;

}

// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

echo "Sorry, your file is too large.";

$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.";

$uploadOk = 0;

}

// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file

} else {

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

} else {

echo "Sorry, there was an error uploading your file.";

}

}

?>

 

I have a folder called uploads in the same place as the above files in xampp|htdocs.

I've even tried a simple example from the php.net website which is as follows:

 

<!DOCTYPE html>

<html>

<body>

<!-- The data encoding type, enctype, MUST be specified as below -->

<form enctype="multipart/form-data" action="upload1.php" method="POST">

<!-- MAX_FILE_SIZE must precede the file input field -->

<input type="hidden" name="MAX_FILE_SIZE" value="500000" />

<!-- Name of input element determines name in $_FILES array -->

Send this file: <input name="userfile" type="file" />

<input type="submit" value="Send File" />

</form>

</body>

</html>

 

<?php

// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead

// of $_FILES.

$uploaddir = "uploads";

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

echo "File is valid, and was successfully uploaded.\n";

} else {

echo "Possible file upload attack!\n";

}

echo 'Here is some more debugging info:';

print_r($_FILES);

print "</pre>";

?>

The above gives me an error of : Parse error: syntax error, unexpected '"uploads"' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\upload1.php on line 8

 

I've used all the above examples in the past with no problems but now it just keeps throwing Parse errors.

Link to post
Share on other sites

I've looked in the apache error logs and keep getting the following:

 

[Fri Sep 02 18:30:30.650622 2016] [ssl:warn] : www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Fri Sep 02 18:30:30.688379 2016] [mpm_winnt:notice]: Child: Starting 150 worker threads.
[Fri Sep 02 18:30:58.097000 2016] [:error] [pid:tid ] [client ::] PHP Parse error: syntax error, unexpected '$check' (T_VARIABLE) in C:\\xampp\\htdocs\\upload.php on line 8, referer: http://localhost/PicTest.html

 

I run the XAMPP control panel with admin rights and have clicked on the red x's.

 

It's so annoying. It used to just work but now it just wont let me do what I want. It works with just basic examples from the w3schools PHP tutorial like:

 

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>

Link to post
Share on other sites

if I change upload.php to:

 

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

 

I get an error: Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\upload.php on line 9

Link to post
Share on other sites

That code doesn't have any syntax errors, you can prove it here:

 

https://www.piliapp.com/php-syntax-check/

 

I get an error: Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\upload.php on line 9

That code has the echo statement on line 8, not line 9. There's got to be something wrong with how you're saving the file, or how things are being copied and pasted. When I copy the code you pasted above and check it on that other site, there are no syntax errors.
Link to post
Share on other sites

That code doesn't have any syntax errors, you can prove it here:

 

https://www.piliapp.com/php-syntax-check/

 

That code has the echo statement on line 8, not line 9. There's got to be something wrong with how you're saving the file, or how things are being copied and pasted. When I copy the code you pasted above and check it on that other site, there are no syntax errors.

 

I just checked on http://phpcodechecker.com/

 

It gives the same error I'm getting for the upload.php code I copied from the w3schools website http://www.w3schools.com/php/php_file_upload.asp

 

It gives the same error: Parse error: syntax error, unexpected '$check' (T_VARIABLE) in C:\xampp\htdocs\upload.php on line 8

 

It also gives the same Parse error for the code I copied from the PHP.net website

Link to post
Share on other sites

I copied the same code on that page and tested it on that site, and I got no errors. Sounds like there's an issue with you copying the code, for whatever reason. Do you have browser extensions that affect that?

 

Nevertheless, it's always best to type the code out yourself, that's the best way to learn and understand how everything works.

  • Like 1
Link to post
Share on other sites

I copied the same code on that page and tested it on that site, and I got no errors. Sounds like there's an issue with you copying the code, for whatever reason. Do you have browser extensions that affect that?

 

Nevertheless, it's always best to type the code out yourself, that's the best way to learn and understand how everything works.

 

I don't have any extensions.

Link to post
Share on other sites

I copied the same code on that page and tested it on that site, and I got no errors. Sounds like there's an issue with you copying the code, for whatever reason. Do you have browser extensions that affect that?

 

Nevertheless, it's always best to type the code out yourself, that's the best way to learn and understand how everything works.

 

Just wrote the example from PHP.net out by hand and now it works! Oh my god. All that bother because it didn't copy and paste properly. I shall have to copy the one from w3schools out by hand now.

Thank you very much for your help! I was just about to chuck my laptop out of my window.

Edited by rich_web_development
Link to post
Share on other sites

If you want to figure out exactly what's going on, save the file that doesn't run and then use a hex editor to open it so that you can see the actual bytes in the file. There's going to be a strange byte (maybe a null byte) probably at the end of the line right before the error message.

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...