PHP File Upload

PHP File Upload

PHP File Upload : -

File upload and download in php using mysqli and simple user define function for connection this is done in two phase for file uploading we use php move_uploaded_file and mysqli for storing database for downloading we use header function

step first create database phpcooker_script and import this :-



--
-- Database: `phpcooker_script`
--

-- --------------------------------------------------------

--
-- Table structure for table `up_fs`
--

CREATE TABLE IF NOT EXISTS `up_fs` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `filename` varchar(150) NOT NULL,
  `filetype` varchar(150) NOT NULL,
  `filesize` varchar(150) NOT NULL,
  `filepath` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `up_fs`
--

INSERT INTO `up_fs` (`id`, `filename`, `filetype`, `filesize`, `filepath`) VALUES
(1, 'Desert.jpg', 'image/jpeg', '845941', 'E:\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\day18fileupload/abc/Desert.jpg'),
(2, 'Jellyfish.jpg', 'image/jpeg', '775702', 'E:\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\day18fileupload/abc/Jellyfish.jpg'),
(3, 'Koala.jpg', 'image/jpeg', '780831', 'E:\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\day18fileupload/abc/Koala.jpg');
 

make dbconfig.php



<?php
function connection(){
    $dbserver="127.0.0.1";
    $dbuser="root";
    $dbpwd="";
    $dbname="phpcooker_script";
    $cid=mysqli_connect($dbserver,$dbuser,$dbpwd,$dbname);
    return $cid;

}

function my_iud($query)
{
$cid = connection();
mysqli_query($cid,$query);
$n=mysqli_affected_rows($cid);
mysqli_close($cid);
return $n;

}


function my_select($query)
{
$cid = connection();
$rs=mysqli_query($cid,$query);
mysqli_close($cid);
return $rs;
}

function my_one($query)
{
$cid = connection();
$rs=mysqli_query($cid,$query);
$row=mysqli_fetch_array($rs);
mysqli_close($cid);
return $row[0];
}
?>

 


make  index.php 

<?php include_once "dbconfig.php"; ?> <html> <head> <title>Upload file to file system</title> </head> <body> <h2>Upload file to file system</h2> <form method='post' enctype='multipart/form-data'> select a file to upload <input type='file' name='myfile' id='myfile' /> <br /> <br /> <input type='submit' name='upload' id='upload' value='upload' /> </form> <?php if(isset($_REQUEST['upload'])) {           $error=$_FILES['myfile']['error']; if($error !=0)     {     echo "<br />File upload failed";     echo "<br />To tmp folder";     }     else     {     $fname=$_FILES['myfile']['name'];     $ftype=$_FILES['myfile']['type'];     $fsize=$_FILES['myfile']['size'];     $ftname=$_FILES['myfile']['tmp_name'];     $source=$ftname;     $target=__DIR__."/abc/$fname";     $ans=move_uploaded_file($source,$target);     if($ans)     {     $target=addslashes($target); /*     c:\abc\new\delta     c:\\abc\\new\\delta     c:\abc\new\delta     hello nisha     printf("hello nisha");     hello  \nisha     printf("hello \\nisha"); */     //to suppress escape sequences      $query="insert into up_fs (filename,filetype,filesize,filepath)      values ('$fname','$ftype',$fsize,'$target')";     $n=my_iud($query);         if($n==1)     echo "<br />File uploaded successfully";     else     echo "<br />File upload failed";          }     else     {     echo "<br />File upload failed";     echo "<br />To target folder";     }     } } $query="select id,filename,filetype,filesize from up_fs"; $rs=my_select($query); $n=mysqli_num_rows($rs); if($n<=0) { echo "<br />No files uploaded"; } else { echo "<br />$n files found"; echo "<table border='1' align='center' width='550' cellspacing='0' cellpadding='5'>"; echo "<tr>"; echo "<th width='100'>file id</th>"; echo "<th width='50'>&nbsp;</th>"; echo "<th width='100'>file name</th>"; echo "<th width='100'>file type</th>"; echo "<th width='100'>file size</th>"; echo "<th width='100'>Preview</th>"; echo "</tr>";     while($row=mysqli_fetch_array($rs))     {     echo "<tr>";     echo "<td>$row[0]</td>";     echo "<td>     <a href='download_fs.php?fid=$row[0]&down=yes' >     <img src='download.gif' />     </a></td>";     echo "<td>$row[1]</td>";     echo "<td>$row[2]</td>";     echo "<td>$row[3]</td>";     echo "<td><img src='download_fs.php?fid=$row[0]&down=yes' width='80'    /></td>";     echo "</tr>";     } echo "</table>"; } ?> </body> </html>

and last download_fs.php



<?php 
if(isset($_REQUEST['down']))
{
$fid=$_REQUEST['fid'];
include_once "dbconfig.php";
$query="select * from up_fs where id=$fid";
$rs=my_select($query);
$row=mysqli_fetch_array($rs);
//print_r($row);
$fname=$row[1];
$ftype=$row[2];
$fsize=$row[3];
$fpath=$row[4];
$fp=fopen($fpath,'r');
$fdata=fread($fp,$fsize);
header("Content-type:$ftype");
header("Content-disposition:attachment;filename=$fname");
echo $fdata;
}
?>

 

smileysmileysmileysmiley

 

Download Here

Total Coments (0)