While uploading the file I have to check "if file already exists in that directory". If so, I have to popup a confirmation message whether to overwrite the file or not. I am trying to do this using javascript in-between. But the file is overwritten even if I say cancel.
Can anyone help me out in this regard. Please let me know if I am not clear.
Here is the code I have used:
<?php
if(isset($_FILES['uploadedfile']))
{
if(!empty($_REQUEST['dirname']))
{
$path = $_REQUEST['dirname'];
$dirs = explode("/" , $path);
$count = count($dirs);
$path = '.';
for ($i = 0; $i < $count; ++$i) {
$path .= "/" . $dirs[$i];
if (!is_dir($path) && !mkdir($path,0777)) {
echo "Problem creating directory";
}
}
$newpath = $_REQUEST['dirname'];
$destFolder = $newpath."/";
$newpath = $newpath."/";
}
else
{
@mkdir(upload);
$newpath = "upload/";
$destFolder = "upload/";
}
foreach ($_FILES["uploadedfile"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$srcpath = $_FILES['uploadedfile']['tmp_name'][$key];
$destFile = $_FILES['uploadedfile']['name'][$key];
$destpath = $destFolder.$destFile;
if(is_file($destpath))
{
echo "dp=".$destpath; ?>
<script language='javascript' type='text/javascript'>
if(confirm('File already exits. Do you want to overwrite it?'))
{
document.write("<? $copy = copy($srcpath,$destpath); echo 'File overwritten at '.$destpath; ?>");
}else
{
document.write("<? echo "File not overwritten"; ?>");
}
</script>
<?php }
}
}
}
else
{
echo "please upload the file";
}
?>
problem uploading a file
Moderator: General Moderators
-
andychamberlainuk
- Forum Newbie
- Posts: 6
- Joined: Mon Jul 28, 2008 7:15 pm
Re: problem uploading a file
You're problem is that PHP is server-side code and javascript is client-side code. you can only use them seperately.
In PHP, you would have to have an intermediate page with the confirmation or place a checkbox on the form to say "overwrite existing files".
AJAX would be an ideal solution as then you could show a pop-up with the confirmation on!
Hope this helps
In PHP, you would have to have an intermediate page with the confirmation or place a checkbox on the form to say "overwrite existing files".
AJAX would be an ideal solution as then you could show a pop-up with the confirmation on!
Hope this helps