Jump to content

Upload process resulting in errors


R0xxy

Recommended Posts

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);
} 
}
?>
Link to comment
https://forums.phpfreaks.com/topic/288340-upload-process-resulting-in-errors/
Share on other sites

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']);

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.