how to seperate function?

original code


function upload_file () {
document.forms[0].action="storagela.php";
document.forms[0].to_do.value="upload";
document.forms[0].download.value="";
document.forms[0].submit();
}

function download_file () {
document.forms[0].action="download.php";
document.forms[0].to_do.value="download";
document.forms[0].upload.value="";
document.forms[0].submit();
}

<?php
include("connect_login.php");
if (isset($_POST))
{
if ($_POST["to_do"]=="upload") {
$target_path = "files/".basename($_FILES["upload"]["tmp_name"]);
if (move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path)) {
$SQL="insert into files values(null,'".basename($_FILES["upload"]["name"])."','".basename($_FILES["upload"]["tmp_name"])."',now())";
mysql_query($SQL);
echo "<script language='JavaScript'> alert('The file ".basename($_FILES["upload"]["name"])." has been uploaded'); ";
} else {
echo " alert('There was an error uploading the file ".basename($_FILES["upload"]["name"]).", please try again') ";
}
}
}
?>

File Library



File to upload:
 
Upload
 


-- Select a file --
<?php
$SQL="select * from files order by time";
$cursor=mysql_query($SQL);
while ($data=mysql_fetch_array($cursor))
echo "<option value='".$data["scrap"]."' original='".$data["topic"]."'>".$data["topic"]." (".$data["time"].")
";
?>
 
Download








then, seperate function from above source code into 2 files;
- download
- upload

here is upload function

function upload_file () {
document.forms[0].action="storagela.php";
document.forms[0].to_do.value="upload";
document.forms[0].download.value="";
document.forms[0].submit();
}

function download_file () {
document.forms[0].action="download.php";
document.forms[0].to_do.value="download";
document.forms[0].upload.value="";
document.forms[0].submit();
}

<?php
include("connect_login.php");
if (isset($_POST))
{
if ($_POST["to_do"]=="upload") {
$target_path = "files/".basename($_FILES["upload"]["tmp_name"]);
if (move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path)) {
$SQL="insert into files values(null,'".basename($_FILES["upload"]["name"])."','".basename($_FILES["upload"]["tmp_name"])."',now())";
mysql_query($SQL);
echo "<script language='JavaScript'> alert('The file ".basename($_FILES["upload"]["name"])." has been uploaded'); ";
} else {
echo " alert('There was an error uploading the file ".basename($_FILES["upload"]["name"]).", please try again') ";
}
}
}
?>




File to upload:
 
Upload


<?
$SQL="select * from files order by time";
$cursor=mysql_query($SQL);
while ($data=mysql_fetch_array($cursor))

?>
 



here for download function


function upload_file () {
document.forms[0].action="un.php";
document.forms[0].to_do.value="upload";
document.forms[0].download.value="";
document.forms[0].submit();
}

function download_file () {
document.forms[0].action="download.php";
document.forms[0].to_do.value="download";
document.forms[0].upload.value="";
document.forms[0].submit();
}

<?
include("connect_login.php");
if (isset($_POST))
{
if ($_POST["to_do"]=="upload") {
$target_path = "http://localhost/q/files/".basename($_FILES["upload"]["tmp_name"]);
if (move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path)) {
$SQL="insert into files values(null,'".basename($_FILES["upload"]["name"])."','".basename($_FILES["upload"]["tmp_name"])."',now())";
mysql_query($SQL);
echo "<script language='JavaScript'> alert('The file ".basename($_FILES["upload"]["name"])." has been uploaded'); ";
} else {
echo " alert('There was an error uploading the file ".basename($_FILES["upload"]["name"]).", please try again') ";
}
}
}
?>

 






-- Select a file --
<?
$SQL="select * from files order by time";
$cursor=mysql_query($SQL);
while ($data=mysql_fetch_array($cursor))
echo "<option value='".$data["scrap"]."' original='".$data["topic"]."'>".$data["topic"]." (".$data["time"].")
";
?>
 
Download

the problem is how to run upload file work as same as the top (original code)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories