Jump to content

Need help with file uploading


shikhartandon

Recommended Posts

Hi,

I recently switched from PHP4 to PHP5.3

 

The code which used to upload a pic into a data base and retrieve it do not work here.. Can any one help debug this..

 

TO UPLOAD

$data = addslashes(fread(fopen($userfile, "r"), filesize($userfile)));

 

    $result=MYSQL_QUERY("INSERT INTO ppicture (Email,titletext,bin_data,filename,filesize,filetype) ".

        "VALUES ('$Email','$title','$data','$userfile_name','$userfile_size','$userfile_type')");

    if(!$result)

{

$result=MYSQL_QUERY("UPDATE ppicture set titletext='$title',bin_data='$data',filename='$userfile_name',filesize='$userfile_size',filetype='$userfile_type' WHERE Email='$Email' " );

 

 

 

TO RETRIEVE

 

$query = "select bin_data,filetype from ppicture where Email='$Email'";

    $result = MYSQL_QUERY($query);

    $data = MYSQL_RESULT($result,0,"bin_data");

    $type = MYSQL_RESULT($result,0,"filetype");

    Header( "Content-type: $type");

    print $data;

 

 

Is there something i should change here?

 

Link to comment
Share on other sites

Try This:

 

 

To Upload:

 

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_SESSION['username'] ;
$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);




$query = "INSERT INTO upload 
(name, size, type, content, post_no) 
VALUES 
('$fileName', '$fileSize', '$fileType', '$content', '$post_no')";
mysql_query($query) or die("oh dear  " .mysql_error());

 

 

To Download

 

<?php
include('../include/dbconnect.php');

$id=$_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE post_no = '$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;

?>

 

Obviously you will have to change various bits like your file name and database info but that should do the trick!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.