R0xxy Posted May 8, 2014 Share Posted May 8, 2014 Hi i am working on a file upload process that limits the size of the file, checks if it exists within the database and either saves or stores it. I keep getting this error Parse error: syntax error, unexpected T_ELSE could anyone detect why this is happening <?php session_start(); $usid=$_SESSION["usersid"]; include "conninfo.php"; $fileName = $_FILES["myfile"]["name"]; $fileTmpLoc = $_FILES["myfile"]["tmp_name"]; $fileType = $_FILES["myfile"]["type"]; $fileSize = $_FILES["myfile"]["size"]; $path = "fileupload/"; $filename=basename($_FILES['myfile']['name']); $path = $path . basename($_FILES['myfile']['name']); if (file_exists("fileupload/" . $newfilename)) { echo "You have already uploaded this file."; } else { move_uploaded_file($_FILES['myfile']['tmp_name'], $path . $newfilename); { echo "$newfilename has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } $query2 = ("INSERT INTO files (filename,usid,subfolder)VALUES ('$filename', $usid, 0)"); $result2=mysql_query($query2); if ($handle = opendir('fileupload/')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { echo "$entry<br>"; } } closedir($handle); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/ Share on other sites More sharing options...
Ch0cu3r Posted May 8, 2014 Share Posted May 8, 2014 an else statement can only be used after an an if statement. move_uploaded_file() on line 25 should be used as part of an if condition if(move_uploaded_file($_FILES['myfile']['tmp_name'], $path . $newfilename)) { echo "$newfilename has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } Also where is the variable $newfilename defined? You only have a variable called $filename defined $filename=basename($_FILES['myfile']['name']); Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478757 Share on other sites More sharing options...
R0xxy Posted May 8, 2014 Author Share Posted May 8, 2014 Hi the $newfilename was so that the name of the file is changed once it is uploaded i got this from a tutorial also i made the changes you suggested but when i test it by uploading a file the first echo appears "you have already uploaded this file" which i have not because it is not within the database Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478762 Share on other sites More sharing options...
Ch0cu3r Posted May 8, 2014 Share Posted May 8, 2014 Hi the $newfilename was so that the name of the file is changed once it is uploaded How can the file name change when you have not defined this variable in your code. Do you know how to define and use variables? Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478763 Share on other sites More sharing options...
R0xxy Posted May 8, 2014 Author Share Posted May 8, 2014 not really sorry to seem dumb i'm fairly new to php?? Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478764 Share on other sites More sharing options...
Ch0cu3r Posted May 8, 2014 Share Posted May 8, 2014 You might want to start here then http://php.net/manual/en/language.variables.php Before doing anything you need to have a good understanding of the basics of the language such as know what variables, data types (strings, arrays, booleans, etc), operators (=, ==, <, > etc) and control structures (if, if/else, switch, for, while, foreach, etc) are. If read post #2 again I have hinted to you what your need to change the variable name to. Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478766 Share on other sites More sharing options...
R0xxy Posted May 8, 2014 Author Share Posted May 8, 2014 okay so instead of $filename=basename($_FILES['myfile']['name']); $newfilename=basename($_FILES['myfile']['name']); Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478767 Share on other sites More sharing options...
Solution Ch0cu3r Posted May 8, 2014 Solution Share Posted May 8, 2014 Yes. You will want to delete the following vars from your script as they are not used $fileName = $_FILES["myfile"]["name"]; $fileTmpLoc = $_FILES["myfile"]["tmp_name"]; $fileType = $_FILES["myfile"]["type"]; $fileSize = $_FILES["myfile"]["size"]; Also this remove . $newfilename from here if(move_uploaded_file($_FILES['myfile']['tmp_name'], $path . $newfilename)) You will end up with the filename being appended twice to $path. This is because you have already appended the filename to $path on this line $path = $path . basename($_FILES['myfile']['name']); // apoends the filename to $path Quote Link to comment https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/#findComment-1478768 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.