0

This is the code for the upload form to enable me to upload an image to the database

<html>
<head>
<title>File Uploading Form</title>
</head>

<body>
<h3>File Upload:</h3>
Select a file to upload: <br />

<form action="file_uploader.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

This is the code that I have in a in a file called file_uploader.php. When trying to complete this I get the error Could not copy file!

<?php
if( $_FILES['file']['name'] != "" )
{
   copy( $_FILES['file']['name'], "databasehostdetails" ) or 
           die( "Could not copy file!");
}
else
{
    die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>

<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name'];  ?>
<li>File size: <?php echo $_FILES['file']['size'];  ?> bytes
<li>File type: <?php echo $_FILES['file']['type'];  ?>
</ul>
</body>
</html>
user2326279
  • 15
  • 1
  • 3

2 Answers2

0

You need to use tmp_name. Also, what is databasehostdetails? The second parameter is the destination (where you want to copy the file to).

copy($_FILES['file']['tmp_name'], DESTINATION_PATH);
SeanWM
  • 16,789
  • 7
  • 51
  • 83
0

Here is a simple way to do this. I am giving you my script index.html But be carefull:

  1. It is not recommended since it is to costly for your database to store files
  2. Fix any sql injection vurnelabilities. I my self don't know how to do this. You can make a research and fix it.
  3. Also check out here Storing Images in DB - Yea or Nay?

However the code works pretty good if it not a large scale application and no sql injection attacks could happen.

<html>
<body>
 <form method="post" enctype="multipart/form-data" action="doupload.php">
 <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
 <input name="userfile" type="file" id="userfile">
 <input name="upload" type="submit" id="upload" value=" Upload ">
 </form>

</body>
</html>

doupload.php

<?php         
 include("config.php");

 $fileName = $_FILES['userfile']['name'];
 $tmpName =  $_FILES['userfile']['tmp_name'];
 $fileSize = $_FILES['userfile']['size'];
 $fileType = $_FILES['userfile']['type'];
 $fp = fopen($tmpName, 'r');
 $content = fread($fp, filesize($tmpName));
 $content = addslashes($content);
 fclose($fp);

 if(!get_magic_quotes_gpc())
 {
 $fileName = addslashes($fileName);
 }

 $query = "INSERT INTO upload set name='".$fileName."', size='".$fileSize."', type='".$fileType."', content='".$content."'";        
 mysql_query($query) ;    
?>     

getuploaded.php

<?php
// select records from database if exists to display
include("config.php");
$query1 = "SELECT id, name FROM upload";
$result1 = mysql_query($query1) or die('Error, query failed');


if(mysql_num_rows($result1)>0)
{
 while(list($id, $name) = mysql_fetch_array($result1))
 {
 ?>
 <a href="download.php?id=<?php echo $id;?>"><?php echo $name;?></a> <br>

 <?php
 }
}
?>

download.php

<?php
//header("Content-type: $type");
include("config.php");
$id    = $_GET['id'];
 $query = "SELECT name, type, size, content " .
 "FROM upload WHERE id = '$id'";

 $result = mysql_query($query) or die('Error, query failed');
 list($name, $type, $size, $content) =  mysql_fetch_array($result);

 header("Content-length: $size");
 header("Content-type: $type");
 header("Content-Disposition: attachment; filename=$name");
 echo $content;

?>

config.php

<?php
 $dbhost = 'localhost';
 $dbuser = 'root';
 $dbpass = '';
 $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
 $dbname = 'uploadfile';
 mysql_select_db($dbname);
?>

Createing the table

CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);
Community
  • 1
  • 1
themhz
  • 8,335
  • 21
  • 84
  • 109