Upload file to a web folder and store its path into Mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lleoun
Forum Newbie
Posts: 8
Joined: Mon Feb 25, 2008 5:37 am

Upload file to a web folder and store its path into Mysql

Post by lleoun »

Dear all,

The following script is meant to upload files into a folder and their path into a Mysql table.

I don't know whay it's not working, please help!

Thanks a lot in advance!!

Code: Select all

 
<html>
<head>
<title>Upload File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    border: 1px solid #000000;
}
-->
</style>
</head>
 
<body>
<?
include ("connection.php");
 
$uploadDir = 'upload_folder';
 
 
if(isset($_POST['upload']))
{
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
 
    // the files will be saved in filePath 
    $filePath = $uploadDir . $fileName;
 
 
    $result    = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
        echo "Error uploading file";
        exit;
    }
    
    include ("library/config.php");
    include ("library/opendb.php");
 
    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);
    }  
 
    $query = "INSERT INTO upload2 (name, size, type, path ) ".
             "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
 
    mysql_query($query) or die('Error, query failed : ' . mysql_error());                    
 
    include ("library/closedb.php");
    
    echo "<br>File uploaded<br>";
}       
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
         </td>
      <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Subelo  "></td>
    </tr>
  </table>
</form>
</body>
</html>
 
 
lleoun
Forum Newbie
Posts: 8
Joined: Mon Feb 25, 2008 5:37 am

Re: Upload file to a web folder and store its path into Mysql

Post by lleoun »

Ok, the upload is working.

Now the problem is when I try to download the files, they are corrupted.. it doesn't matter what type of file it is.

I tried with a .txt file and when I open it after downloading, it's empty... what's wrong??
Thanks in advance again!!

The upload code that is working:

Code: Select all

 
<?php
 
    $name = $_FILES['uploaded']['name'];
    $tmp_name  = $_FILES['uploaded']['tmp_name'];
    
    
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
 
  $filePath = $target . $name;
 
$ok=1;
 
if ($uploaded_size > 35000000)
{
echo "The file is too big.<br>";
 
$ok=0;
}
if ($uploaded_type =="mov/wmv/txt")
{
echo "Type not allowed <br>";
$ok=0;
}
if ($ok==0)
{
echo “Sorry the file was not upload";
}
else
 
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
 
    $uploaded_size = $_FILES['uploaded']['size'];
    $uploaded_type = $_FILES['uploaded']['type'];
 
 
    include ("co.php");
    include ("library/config.php");
    include ("library/opendb.php");
 
    if(!get_magic_quotes_gpc())
    {
        //$fileName  = addslasshes($name);
        $fileName= mysql_real_escape_string($name);
 
       // $filePath  = addslashes($filePath);
        $filePath= mysql_real_escape_string($filePath);
    }  
 
    $query = "INSERT INTO upload2 (name, size, type, path ) ".
             "VALUES ('$fileName', '$uploaded_size', '$uploaded_type', '$filePath')";
 
    mysql_query($query) or die('Error, query failed : ' . mysql_error());  
 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry there was a problem uploading the file.";
}
}
?>
 
The code I'm using to download the files:

Code: Select all

 
<?php
error_reporting(E_ALL);
 
         ob_start(); 
include ("connection.php");             
ob_end_clean();
    include ("library/config.php");
    include ("library/opendb.php");
 
//  $id      = $_GET['id']; // I'll do this later
    $query   = "SELECT name, type, size, path FROM upload2 WHERE id = 12";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $filePath) = mysql_fetch_array($result);
 
    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type"); 
    
    readfile($filePath);
 
    include ("library/closedb.php");    
    exit;
 
 
?>
 
<html>
<head>
<title>DOWNLOAD</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<?php
     ob_start(); 
include ("connection.php");             
ob_end_clean();
 
include ("library/config.php");
include ("library/opendb.php");
 
$query  = "SELECT id, name FROM upload2";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
} 
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <a href="d.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php       
    }
}
include ("library/closedb.php");
?>
</body>
</html>
 
 
Post Reply