Jump to content

Upload process resulting in errors


Go to solution Solved by Ch0cu3r,

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.

  • Solution

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.