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>';
}
?>
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.
Comments