This code is running well for upload and extension check but not when file already exists

punkz

The page starts from index but other things are running well so the page starts from upload here. The code is only deleting the file in the case of file already exists.i'm new to php. Is there something wrong with the code??

upload.php

<?php
        if($action=="upload"){
        echo '<script language="javascript">';
        echo 'alert("File Uploaded")';
        echo '</script>';
        }
        ?>
        <?php
        if($action=="error"){
        echo '<script language="javascript">';
        echo 'alert("Please Upload only (xls,pdf,doc)files.")';
        echo '</script>';
        }
        ?>
        <?php
        if($action=="error_dct"){
        //var_dump($name);
        echo '<script language="javascript">';
        echo 'var x=confirm("Selected file already exists.");';
        echo 'if(x==true)';
        echo '{';
        $path="uploads_masterdata/".$name;
        unlink($path);
        //var_dump($tmp);
        move_uploaded_file($tmp, $path);
        echo '}';
        echo '</script>';
        } 
        ?>



    <?php
            define('ROOT_DIR', dirname(__FILE__));
            define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
            session_start();
            //$errors= array();
            $expensions= array(".xls",".xlsx",".doc",".docx",".pdf"); 
             //$file_ext=strtolower(end(explode('.',$_FILES['userFile']['name'])));
             $file_name = $_FILES['userFile']['name'];
              $file_ext=substr($file_name, strrpos($file_name, '.'));
              var_dump($file_ext);
            $info = pathinfo($_FILES['userFile']['name']);
            $tmp=$_FILES['userFile']['tmp_name'];
            $ext = $info['extension']; // get the extension of the file
            $newname = $file_name; 
            if(in_array($file_ext,$expensions)=== false){
                     $errors[]="extension not allowed, please choose a .xls file.";
                     header("location: masterdata.php?action=error&name=$newname&fe=$file_ext");
                  }

             if(file_exists(ROOT_DIR."/uploads/uploads_masterdata/".$_FILES['userFile']['name']))
            {
            $errors[]="file already exists";
            header("location: masterdata.php?action=error_dct&tmp=$tmp&name=$newname");
            }    
                  if(empty($errors)==true){

             $target = ROOT_DIR.'/uploads/uploads_masterdata/'.$newname;
             //$target = 'uploads_masterdata/'.$newname;
             //move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
             move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
            header("location: masterdata.php?action=upload");
                  }
                  else{
                     //print_r($errors);
                     //header("location: masterdata.php");

                  }
            ?>

index.php

<?php
if($action=="upload"){
echo '<script language="javascript">';
echo 'alert("File Uploaded")';
echo '</script>';
}
?>
<?php
if($action=="error"){
echo '<script language="javascript">';
echo 'alert("Please Upload only (xls,pdf,doc)files.")';
echo '</script>';
}
?>
<?php
if($action=="error_dct"){
//var_dump($name);
echo '<script language="javascript">';
echo 'var x=confirm("Selected file already exists.");';
echo 'if(x==true)';
echo '{';
$path="uploads_masterdata/".$name;
unlink($path);
//var_dump($tmp);
move_uploaded_file($tmp, $path);
echo '}';
echo '</script>';
} 
?>
Saty

In php use use file_exists to check file is exist or not

 if (file_exists($path)) {// check file exist or not
    unlink($path);// if exist then delete it
 }
 move_uploaded_file($tmp, $path);// upload file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

This code is running well for upload and extension check but not when file already exists

From Dev

Check if file exists not knowing the extension

From Dev

Upload error in Launchpad - source file already exists

From Dev

How to check if the file already exists in the LinkItemCollection?

From Dev

How to check if file name already exists?

From Dev

Check if a generated key already exists in a file

From Dev

How to check if file already exists, if not, download on Python?

From Dev

how to check if value already exists in txt file

From Dev

Google App - check if file exists already?

From Dev

Check if file exists in the upload file array

From Dev

File Already Exists When Moving File

From Dev

Check if a php file command is already running on cron

From Dev

How to check if a proc is already running when called?

From Dev

How to check if an e-mail address already exists in a file database?

From Dev

How to check if a certain file already exists in symfony2?

From Dev

Ajax call not working, when trying to check if username already exists in the database

From Dev

How to check if an item already exists in table when inserting from a XML

From Dev

Cannot create a file when it already exists using File.Move

From Dev

mklink error: Cannot create a file when that file already exists

From Dev

Warning message if file name already exists when creating a pdf file

From Dev

If File Already Exists Rename it

From Dev

Error in file already exists

From Dev

What is an appropriate HTTP response code for a file that already exists?

From Dev

What is an appropriate HTTP response code for a file that already exists?

From Dev

HTTP Status Code for username already exists when registering new account

From Java

HTTP response code for POST when resource already exists

From Dev

Table already exists error when trying to import sql file

From Dev

Open returns (-1) with O_CREAT when file already exists

From Dev

Open returns (-1) with O_CREAT when file already exists

Related Related

  1. 1

    This code is running well for upload and extension check but not when file already exists

  2. 2

    Check if file exists not knowing the extension

  3. 3

    Upload error in Launchpad - source file already exists

  4. 4

    How to check if the file already exists in the LinkItemCollection?

  5. 5

    How to check if file name already exists?

  6. 6

    Check if a generated key already exists in a file

  7. 7

    How to check if file already exists, if not, download on Python?

  8. 8

    how to check if value already exists in txt file

  9. 9

    Google App - check if file exists already?

  10. 10

    Check if file exists in the upload file array

  11. 11

    File Already Exists When Moving File

  12. 12

    Check if a php file command is already running on cron

  13. 13

    How to check if a proc is already running when called?

  14. 14

    How to check if an e-mail address already exists in a file database?

  15. 15

    How to check if a certain file already exists in symfony2?

  16. 16

    Ajax call not working, when trying to check if username already exists in the database

  17. 17

    How to check if an item already exists in table when inserting from a XML

  18. 18

    Cannot create a file when it already exists using File.Move

  19. 19

    mklink error: Cannot create a file when that file already exists

  20. 20

    Warning message if file name already exists when creating a pdf file

  21. 21

    If File Already Exists Rename it

  22. 22

    Error in file already exists

  23. 23

    What is an appropriate HTTP response code for a file that already exists?

  24. 24

    What is an appropriate HTTP response code for a file that already exists?

  25. 25

    HTTP Status Code for username already exists when registering new account

  26. 26

    HTTP response code for POST when resource already exists

  27. 27

    Table already exists error when trying to import sql file

  28. 28

    Open returns (-1) with O_CREAT when file already exists

  29. 29

    Open returns (-1) with O_CREAT when file already exists

HotTag

Archive